app.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. _, token = m.User.login(self.request_data(r.User))
  28. return {"token": token}
  29. @endpoint("/login")
  30. class Login(Endpoint):
  31. def html(self):
  32. return render("main", title="login", content=render("login"), user=self.user)
  33. def api_post(self):
  34. req = self.request_data(r.User)
  35. u, token = m.User.login(req)
  36. flask.session["auth"] = token
  37. raise e.LCRedirect(u.base_url())
  38. @endpoint("/logout")
  39. class Logout(Endpoint):
  40. def html(self):
  41. if "auth" in flask.session:
  42. del flask.session["auth"]
  43. raise e.LCRedirect("/")
  44. def api_post(self):
  45. if "auth" in flask.session:
  46. del flask.session["auth"]
  47. raise e.LCRedirect("/")
  48. @endpoint("/u")
  49. class CreateUser(Endpoint):
  50. def html(self):
  51. if self.user:
  52. raise e.LCRedirect(f"/u/{self.user.name}")
  53. token = flask.request.args.get("token")
  54. if not token:
  55. raise e.LCRedirect("/")
  56. return render(
  57. "main",
  58. title="add user",
  59. user=self.user,
  60. content=render("add_user", token=token),
  61. )
  62. def api_post(self):
  63. token = flask.request.args["token"]
  64. req = self.request_data(r.NewUser).to_user_request()
  65. u = m.User.from_invite(req, token)
  66. flask.session["auth"] = req.to_token()
  67. raise e.LCRedirect(u.base_url())
  68. @endpoint("/u/<string:slug>")
  69. class GetUser(Endpoint):
  70. def html(self, slug: str):
  71. u = m.User.by_slug(slug)
  72. pg = int(flask.request.args.get("page", 1))
  73. links, pages = u.get_links(page=pg)
  74. return render(
  75. "main",
  76. title=f"user {u.name}",
  77. content=render("linklist", links=links, pages=pages),
  78. user=self.user,
  79. )
  80. def api_get(self, slug: str):
  81. return m.User.by_slug(slug).to_dict()
  82. @endpoint("/u/<string:user>/config")
  83. class UserConfig(Endpoint):
  84. def html(self, user: str):
  85. u = self.require_authentication(user)
  86. return render(
  87. "main",
  88. title="configuration",
  89. content=render("config", **u.get_config()),
  90. user=self.user,
  91. )
  92. @endpoint("/u/<string:user>/invite")
  93. class CreateInvite(Endpoint):
  94. def api_post(self, user: str):
  95. u = self.require_authentication(user)
  96. m.UserInvite.manufacture(u)
  97. raise e.LCRedirect(f"/u/{user}/config")
  98. @endpoint("/u/<string:user>/l")
  99. class CreateLink(Endpoint):
  100. def html(self, user: str):
  101. return render("main", title="login", content=render("add_link"), user=self.user)
  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. raise e.LCRedirect(l.link_url())
  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 html(self, user: str, link: str):
  112. l = m.User.by_slug(user).get_link(int(link))
  113. return render(
  114. "main",
  115. title=f"link {l.name}",
  116. content=render("linklist", links=[l]),
  117. user=self.user,
  118. )
  119. pass
  120. @endpoint("/u/<string:user>/t/<path:tag>")
  121. class GetTaggedLinks(Endpoint):
  122. def html(self, user: str, tag: str):
  123. u = m.User.by_slug(user)
  124. pg = int(flask.request.args.get("page", 0))
  125. t = u.get_tag(tag)
  126. links, pages = t.get_links(page=pg)
  127. return render(
  128. "main",
  129. title=f"tag {tag}",
  130. content=render("linklist", links=links, pages=pages),
  131. user=self.user,
  132. )