app.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import contextlib
  2. import os
  3. import flask
  4. import sys
  5. import lc.config as c
  6. import lc.error as e
  7. import lc.model as m
  8. import lc.request as r
  9. import lc.view as v
  10. from lc.web import Endpoint, endpoint, render
  11. app = c.app
  12. @endpoint("/")
  13. class Index(Endpoint):
  14. def html(self):
  15. return render(
  16. "main",
  17. v.Page(
  18. title="main",
  19. content=render(
  20. "message",
  21. v.Message(
  22. title="Lament Configuration",
  23. message="Bookmark organizing for real pinheads.",
  24. ),
  25. ),
  26. user=self.user,
  27. ),
  28. )
  29. @endpoint("/auth")
  30. class Auth(Endpoint):
  31. def api_post(self):
  32. u, token = m.User.login(self.request_data(r.User))
  33. flask.session["auth"] = token
  34. return self.api_ok(u.base_url(), {"token": token})
  35. @endpoint("/login")
  36. class Login(Endpoint):
  37. def html(self):
  38. return render(
  39. "main", v.Page(title="login", content=render("login"), user=self.user,)
  40. )
  41. @endpoint("/logout")
  42. class Logout(Endpoint):
  43. def html(self):
  44. if "auth" in flask.session:
  45. del flask.session["auth"]
  46. raise e.LCRedirect("/")
  47. def api_post(self):
  48. if "auth" in flask.session:
  49. del flask.session["auth"]
  50. return self.api_ok("/")
  51. @endpoint("/u")
  52. class CreateUser(Endpoint):
  53. def html(self):
  54. if self.user:
  55. raise e.LCRedirect(f"/u/{self.user.name}")
  56. token = flask.request.args.get("token")
  57. if not token:
  58. raise e.LCRedirect("/")
  59. return render(
  60. "main",
  61. v.Page(title="add user", user=self.user, content=render("add_user"),),
  62. )
  63. def api_post(self):
  64. token = flask.request.args["token"]
  65. req = self.request_data(r.NewUser).to_user_request()
  66. u = m.User.from_invite(req, token)
  67. flask.session["auth"] = req.to_token()
  68. return self.api_ok(u.base_url(), u.to_dict())
  69. @endpoint("/u/<string:slug>")
  70. class GetUser(Endpoint):
  71. def html(self, slug: str):
  72. u = m.User.by_slug(slug)
  73. pg = int(flask.request.args.get("page", 1))
  74. links, pages = u.get_links(as_user=self.user, page=pg)
  75. return render(
  76. "main",
  77. v.Page(
  78. title=f"user {u.name}",
  79. content=render("linklist", v.LinkList(links=links, pages=pages)),
  80. user=self.user,
  81. ),
  82. )
  83. def api_get(self, slug: str):
  84. return m.User.by_slug(slug).to_dict()
  85. @endpoint("/u/<string:user>/config")
  86. class GetUserConfig(Endpoint):
  87. def html(self, user: str):
  88. u = self.require_authentication(user)
  89. return render(
  90. "main",
  91. v.Page(
  92. title="configuration",
  93. content=render("config", u.get_config()),
  94. user=self.user,
  95. ),
  96. )
  97. @endpoint("/u/<string:user>/invite")
  98. class CreateInvite(Endpoint):
  99. def api_post(self, user: str):
  100. u = self.require_authentication(user)
  101. invite = m.UserInvite.manufacture(u)
  102. return self.api_ok(f"/u/{user}/config", {"invite": invite.token})
  103. @endpoint("/u/<string:user>/l")
  104. class CreateLink(Endpoint):
  105. def html(self, user: str):
  106. url = flask.request.args.get("url", "")
  107. name = flask.request.args.get("name", "")
  108. defaults = v.AddLinkDefaults(name=name, url=url,)
  109. return render(
  110. "main",
  111. v.Page(
  112. title="login", content=render("add_link", defaults), user=self.user,
  113. ),
  114. )
  115. def api_post(self, user: str):
  116. u = self.require_authentication(user)
  117. req = self.request_data(r.Link)
  118. l = m.Link.from_request(u, req)
  119. return self.api_ok(l.link_url(), l.to_dict())
  120. @endpoint("/u/<string:user>/l/<string:link>")
  121. class GetLink(Endpoint):
  122. def api_get(self, user: str, link: str):
  123. u = self.require_authentication(user)
  124. l = u.get_link(int(link))
  125. return self.api_ok(l.link_url(), l.to_dict())
  126. def api_post(self, user: str, link: str):
  127. u = self.require_authentication(user)
  128. l = u.get_link(int(link))
  129. req = self.request_data(r.Link)
  130. l.update_from_request(u, req)
  131. raise e.LCRedirect(l.link_url())
  132. def api_delete(self, user: str, link: str):
  133. u = self.require_authentication(user)
  134. u.get_link(int(link)).delete_instance()
  135. return self.api_ok(u.base_url())
  136. def html(self, user: str, link: str):
  137. l = m.User.by_slug(user).get_link(int(link))
  138. return render(
  139. "main",
  140. v.Page(
  141. title=f"link {l.name}",
  142. content=render("linklist", v.LinkList([l.to_view(self.user)])),
  143. user=self.user,
  144. ),
  145. )
  146. @endpoint("/u/<string:slug>/l/<string:link>/edit")
  147. class EditLink(Endpoint):
  148. def html(self, slug: str, link: str):
  149. u = self.require_authentication(slug)
  150. l = u.get_link(int(link))
  151. return render(
  152. "main",
  153. v.Page(
  154. title="login",
  155. content=render("edit_link", v.SingleLink(l)),
  156. user=self.user,
  157. ),
  158. )
  159. @endpoint("/u/<string:user>/t/<path:tag>")
  160. class GetTaggedLinks(Endpoint):
  161. def html(self, user: str, tag: str):
  162. u = m.User.by_slug(user)
  163. pg = int(flask.request.args.get("page", 0))
  164. t = u.get_tag(tag)
  165. links, pages = t.get_links(as_user=self.user, page=pg)
  166. return render(
  167. "main",
  168. v.Page(
  169. title=f"tag {tag}",
  170. content=render("linklist", v.LinkList(links=links, pages=pages,)),
  171. user=self.user,
  172. ),
  173. )