app.py 4.7 KB

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