app.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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("main", v.Page(
  16. title="main",
  17. content=render("message", v.Message(
  18. title="Lament Configuration",
  19. message="Bookmark organizing for real pinheads.",
  20. )),
  21. user=self.user,
  22. ))
  23. @endpoint("/auth")
  24. class Auth(Endpoint):
  25. def api_post(self):
  26. u, token = m.User.login(self.request_data(r.User))
  27. flask.session["auth"] = token
  28. return self.api_ok(u.base_url(), {"token": token})
  29. @endpoint("/login")
  30. class Login(Endpoint):
  31. def html(self):
  32. return render("main", v.Page(
  33. title="login",
  34. content=render("login"),
  35. user=self.user,
  36. ))
  37. @endpoint("/logout")
  38. class Logout(Endpoint):
  39. def html(self):
  40. if "auth" in flask.session:
  41. del flask.session["auth"]
  42. raise e.LCRedirect("/")
  43. def api_post(self):
  44. if "auth" in flask.session:
  45. del flask.session["auth"]
  46. return self.api_ok("/")
  47. @endpoint("/u")
  48. class CreateUser(Endpoint):
  49. def html(self):
  50. if self.user:
  51. raise e.LCRedirect(f"/u/{self.user.name}")
  52. token = flask.request.args.get("token")
  53. if not token:
  54. raise e.LCRedirect("/")
  55. return render("main", v.Page(
  56. title="add user",
  57. user=self.user,
  58. content=render("add_user"),
  59. ))
  60. def api_post(self):
  61. token = flask.request.args["token"]
  62. req = self.request_data(r.NewUser).to_user_request()
  63. u = m.User.from_invite(req, token)
  64. flask.session["auth"] = req.to_token()
  65. return self.api_ok(u.base_url(), u)
  66. @endpoint("/u/<string:slug>")
  67. class GetUser(Endpoint):
  68. def html(self, slug: str):
  69. u = m.User.by_slug(slug)
  70. pg = int(flask.request.args.get("page", 1))
  71. links, pages = u.get_links(as_user=self.user, page=pg)
  72. return render("main", v.Page(
  73. title=f"user {u.name}",
  74. content=render("linklist", v.LinkList(links=links, pages=pages)),
  75. user=self.user,
  76. ))
  77. def api_get(self, slug: str):
  78. return m.User.by_slug(slug).to_dict()
  79. @endpoint("/u/<string:user>/config")
  80. class UserConfig(Endpoint):
  81. def html(self, user: str):
  82. u = self.require_authentication(user)
  83. return render("main", v.Page(
  84. title="configuration",
  85. content=render("config", u.get_config()),
  86. user=self.user,
  87. ))
  88. @endpoint("/u/<string:user>/invite")
  89. class CreateInvite(Endpoint):
  90. def api_post(self, user: str):
  91. u = self.require_authentication(user)
  92. invite = m.UserInvite.manufacture(u)
  93. return self.api_ok(f"/u/{user}/config", {"invite": invite.token})
  94. @endpoint("/u/<string:user>/l")
  95. class CreateLink(Endpoint):
  96. def html(self, user: str):
  97. return render("main", v.Page(
  98. title="login",
  99. content=render("add_link"),
  100. user=self.user,
  101. ))
  102. def api_post(self, user: str):
  103. u = self.require_authentication(user)
  104. req = self.request_data(r.Link)
  105. l = m.Link.from_request(u, req)
  106. return self.api_ok(l.link_url(), l.to_dict())
  107. @endpoint("/u/<string:user>/l/<string:link>")
  108. class GetLink(Endpoint):
  109. def api_get(self, user: str, link: str):
  110. pass
  111. def api_post(self, user: str, link: str):
  112. u = self.require_authentication(user)
  113. l = u.get_link(int(link))
  114. req = self.request_data(r.Link)
  115. l.update_from_request(u, req)
  116. raise e.LCRedirect(l.link_url())
  117. def html(self, user: str, link: str):
  118. l = m.User.by_slug(user).get_link(int(link))
  119. return render("main", v.Page(
  120. title=f"link {l.name}",
  121. content=render("linklist", v.LinkList([l])),
  122. user=self.user,
  123. ))
  124. @endpoint("/u/<string:slug>/l/<string:link>/edit")
  125. class EditLink(Endpoint):
  126. def html(self, slug: str, link: str):
  127. u = self.require_authentication(slug)
  128. l = u.get_link(int(link))
  129. return render(
  130. "main", title="login", content=render("edit_link", link=l), user=self.user
  131. )
  132. @endpoint("/u/<string:user>/t/<path:tag>")
  133. class GetTaggedLinks(Endpoint):
  134. def html(self, user: str, tag: str):
  135. u = m.User.by_slug(user)
  136. pg = int(flask.request.args.get("page", 0))
  137. t = u.get_tag(tag)
  138. links, pages = t.get_links(as_user=self.user, page=pg)
  139. return render("main", v.Page(
  140. title=f"tag {tag}",
  141. content=render("linklist", v.LinkList(
  142. links=links,
  143. pages=pages,
  144. )),
  145. user=self.user,
  146. ))