app.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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(r.User.from_json(flask.request.data))
  28. return 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 api_post(self):
  51. u = m.User.from_request(r.User.from_json(flask.request.data))
  52. return flask.redirect(u.base_url())
  53. @endpoint("/u/<string:slug>")
  54. class GetUser(Endpoint):
  55. def html(self, slug: str):
  56. u = m.User.by_slug(slug)
  57. pg = int(flask.request.args.get("page", 1))
  58. links, pages = u.get_links(page=pg)
  59. return render(
  60. "main",
  61. title=f"user {u.name}",
  62. content=render("linklist", links=links, pages=pages),
  63. user=self.user,
  64. )
  65. def api_get(self, slug: str):
  66. return m.User.by_slug(slug).to_dict()
  67. @endpoint("/u/<string:user>/l")
  68. class CreateLink(Endpoint):
  69. def html(self, user: str):
  70. return render("main", title="login", content=render("add_link"), user=self.user)
  71. def api_post(self, user: str):
  72. u = self.require_authentication(user)
  73. req = self.request_data(r.Link)
  74. l = m.Link.from_request(u, req)
  75. raise e.LCRedirect(l.link_url())
  76. @endpoint("/u/<string:user>/l/<string:link>")
  77. class GetLink(Endpoint):
  78. def api_get(self, user: str, link: str):
  79. pass
  80. def html(self, user: str, link: str):
  81. l = m.User.by_slug(user).get_link(int(link))
  82. return render(
  83. "main",
  84. title=f"link {l.name}",
  85. content=render("linklist", links=[l]),
  86. user=self.user,
  87. )
  88. pass
  89. @endpoint("/u/<string:user>/t/<path:tag>")
  90. class GetTaggedLinks(Endpoint):
  91. def html(self, user: str, tag: str):
  92. u = m.User.by_slug(user)
  93. pg = int(flask.request.args.get("page", 0))
  94. t = u.get_tag(tag)
  95. links, pages = t.get_links(page=pg)
  96. return render(
  97. "main",
  98. title=f"tag {tag}",
  99. content=render("linklist", links=links, pages=pages),
  100. user=self.user,
  101. )