app.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.app
  12. @endpoint("/")
  13. class Index(Endpoint):
  14. def html(self):
  15. pg = int(flask.request.args.get("page", 1))
  16. links, pages = m.Link.get_all(as_user=self.user, page=pg)
  17. linklist = v.LinkList(links=links, pages=pages, user="", tags=[])
  18. return render(
  19. "main",
  20. v.Page(title="main", content=render("linklist", linklist), user=self.user,),
  21. )
  22. @endpoint("/auth")
  23. class Auth(Endpoint):
  24. def api_post(self):
  25. u, token = m.User.login(self.request_data(r.User))
  26. flask.session["auth"] = token
  27. return self.api_ok(u.base_url(), {"token": token})
  28. @endpoint("/login")
  29. class Login(Endpoint):
  30. def html(self):
  31. return render(
  32. "main", v.Page(title="login", content=render("login"), user=self.user,)
  33. )
  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. add_user = v.AddUser(token=token)
  53. return render(
  54. "main",
  55. v.Page(
  56. title="add user", user=self.user, content=render("add_user", add_user),
  57. ),
  58. )
  59. def api_post(self):
  60. token = flask.request.args["token"]
  61. req = self.request_data(r.NewUser).to_user_request()
  62. u = m.User.from_invite(req, token)
  63. flask.session["auth"] = req.to_token()
  64. return self.api_ok(u.base_url(), u.to_dict())
  65. @endpoint("/u/<string:slug>")
  66. class GetUser(Endpoint):
  67. def html(self, slug: str):
  68. u = m.User.by_slug(slug)
  69. pg = int(flask.request.args.get("page", 1))
  70. tags = u.get_tags()
  71. links, pages = u.get_links(as_user=self.user, page=pg)
  72. linklist = v.LinkList(links=links, user=slug, pages=pages, tags=tags)
  73. return render(
  74. "main",
  75. v.Page(
  76. title=f"user {u.name}",
  77. content=render("linklist", linklist),
  78. user=self.user,
  79. ),
  80. )
  81. def api_get(self, slug: str):
  82. return m.User.by_slug(slug).to_dict()
  83. @endpoint("/u/<string:user>/config")
  84. class GetUserConfig(Endpoint):
  85. def html(self, user: str):
  86. u = self.require_authentication(user)
  87. status_msg = flask.request.args.get("m", None)
  88. if status_msg is not None:
  89. status_msg = int(status_msg)
  90. return render(
  91. "main",
  92. v.Page(
  93. title="configuration",
  94. content=render("config", u.get_config(status_msg)),
  95. user=self.user,
  96. ),
  97. )
  98. @endpoint("/u/<string:user>/invite")
  99. class CreateInvite(Endpoint):
  100. def api_post(self, user: str):
  101. u = self.require_authentication(user)
  102. invite = m.UserInvite.manufacture(u)
  103. return self.api_ok(f"/u/{user}/config", {"invite": invite.token})
  104. @endpoint("/u/<string:user>/password")
  105. class ChangePassword(Endpoint):
  106. def api_post(self, user: str):
  107. u = self.require_authentication(user)
  108. config_url = u.config_url()
  109. req = self.request_data(r.PasswordChange)
  110. try:
  111. req.require_match()
  112. except e.MismatchedPassword:
  113. raise e.LCRedirect(f"{config_url}?m=2")
  114. try:
  115. u.change_password(req)
  116. except e.BadPassword:
  117. raise e.LCRedirect(f"{config_url}?m=3")
  118. return self.api_ok(f"{config_url}?m=1")
  119. @endpoint("/u/<string:user>/l")
  120. class CreateLink(Endpoint):
  121. def html(self, user: str):
  122. url = flask.request.args.get("url", "")
  123. name = flask.request.args.get("name", "")
  124. defaults = v.AddLinkDefaults(name=name, url=url,)
  125. return render(
  126. "main",
  127. v.Page(
  128. title="login", content=render("add_link", defaults), user=self.user,
  129. ),
  130. )
  131. def api_post(self, user: str):
  132. u = self.require_authentication(user)
  133. req = self.request_data(r.Link)
  134. l = m.Link.from_request(u, req)
  135. return self.api_ok(l.link_url(), l.to_dict())
  136. @endpoint("/u/<string:user>/l/<string:link>")
  137. class GetLink(Endpoint):
  138. def api_get(self, user: str, link: str):
  139. u = self.require_authentication(user)
  140. l = u.get_link(int(link))
  141. return self.api_ok(l.link_url(), l.to_dict())
  142. def api_post(self, user: str, link: str):
  143. u = self.require_authentication(user)
  144. l = u.get_link(int(link))
  145. req = self.request_data(r.Link)
  146. l.update_from_request(u, req)
  147. raise e.LCRedirect(l.link_url())
  148. def api_delete(self, user: str, link: str):
  149. u = self.require_authentication(user)
  150. u.get_link(int(link)).delete_instance()
  151. return self.api_ok(u.base_url())
  152. def html(self, user: str, link: str):
  153. l = m.User.by_slug(user).get_link(int(link))
  154. return render(
  155. "main",
  156. v.Page(
  157. title=f"link {l.name}",
  158. content=render(
  159. "linklist", v.LinkList([l.to_view(self.user)], [], user=user)
  160. ),
  161. user=self.user,
  162. ),
  163. )
  164. @endpoint("/u/<string:slug>/l/<string:link>/edit")
  165. class EditLink(Endpoint):
  166. def html(self, slug: str, link: str):
  167. u = self.require_authentication(slug)
  168. l = u.get_link(int(link))
  169. return render(
  170. "main",
  171. v.Page(
  172. title="login",
  173. content=render("edit_link", v.SingleLink(l)),
  174. user=self.user,
  175. ),
  176. )
  177. @endpoint("/u/<string:user>/t/<path:tag>")
  178. class GetTaggedLinks(Endpoint):
  179. def html(self, user: str, tag: str):
  180. u = m.User.by_slug(user)
  181. pg = int(flask.request.args.get("page", 1))
  182. t = u.get_tag(tag)
  183. links, pages = t.get_links(as_user=self.user, page=pg)
  184. tags = u.get_related_tags(t)
  185. linklist = v.LinkList(links=links, pages=pages, tags=tags, user=user)
  186. return render(
  187. "main",
  188. v.Page(
  189. title=f"tag {tag}",
  190. content=render("linklist", linklist),
  191. user=self.user,
  192. ),
  193. )
  194. @endpoint("/u/<string:user>/import")
  195. class PinboardImport(Endpoint):
  196. def html(self, user: str):
  197. u = self.require_authentication(user)
  198. return render(
  199. "main",
  200. v.Page(
  201. title=f"import pinboard data", content=render("import"), user=self.user,
  202. ),
  203. )
  204. def api_post(self, user: str):
  205. u = self.require_authentication(user)
  206. if "file" not in flask.request.files:
  207. raise e.BadFileUpload("could not find attached file")
  208. file = flask.request.files["file"]
  209. if file.filename == "":
  210. raise e.BadFileUpload("no file selected")
  211. u.import_pinboard_data(file.stream)
  212. return self.api_ok(u.base_url())