app.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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)
  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 UserConfig(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. return render(
  107. "main", v.Page(title="login", content=render("add_link"), user=self.user,)
  108. )
  109. def api_post(self, user: str):
  110. u = self.require_authentication(user)
  111. req = self.request_data(r.Link)
  112. l = m.Link.from_request(u, req)
  113. return self.api_ok(l.link_url(), l.to_dict())
  114. @endpoint("/u/<string:user>/l/<string:link>")
  115. class GetLink(Endpoint):
  116. def api_get(self, user: str, link: str):
  117. pass
  118. def api_post(self, user: str, link: str):
  119. u = self.require_authentication(user)
  120. l = u.get_link(int(link))
  121. req = self.request_data(r.Link)
  122. l.update_from_request(u, req)
  123. raise e.LCRedirect(l.link_url())
  124. def html(self, user: str, link: str):
  125. l = m.User.by_slug(user).get_link(int(link))
  126. return render(
  127. "main",
  128. v.Page(
  129. title=f"link {l.name}",
  130. content=render("linklist", v.LinkList([l.to_view(self.user)])),
  131. user=self.user,
  132. ),
  133. )
  134. @endpoint("/u/<string:slug>/l/<string:link>/edit")
  135. class EditLink(Endpoint):
  136. def html(self, slug: str, link: str):
  137. u = self.require_authentication(slug)
  138. l = u.get_link(int(link))
  139. return render(
  140. "main",
  141. v.Page(
  142. title="login",
  143. content=render("edit_link", v.SingleLink(l)),
  144. user=self.user,
  145. ),
  146. )
  147. @endpoint("/u/<string:user>/t/<path:tag>")
  148. class GetTaggedLinks(Endpoint):
  149. def html(self, user: str, tag: str):
  150. u = m.User.by_slug(user)
  151. pg = int(flask.request.args.get("page", 0))
  152. t = u.get_tag(tag)
  153. links, pages = t.get_links(as_user=self.user, page=pg)
  154. return render(
  155. "main",
  156. v.Page(
  157. title=f"tag {tag}",
  158. content=render("linklist", v.LinkList(links=links, pages=pages,)),
  159. user=self.user,
  160. ),
  161. )