app.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 = flask.Flask(__name__)
  11. @app.route("/")
  12. @endpoint
  13. class Index(Endpoint):
  14. def html(self):
  15. return render("main", title="main", content="whoo", user=self.user)
  16. @app.route("/auth", methods=["GET", "POST"])
  17. @endpoint
  18. class Auth(Endpoint):
  19. def api_post(self):
  20. return m.User.login(r.User.from_json(flask.request.data))
  21. @app.route("/u", methods=["GET", "POST"])
  22. @endpoint
  23. class CreateUser(Endpoint):
  24. def api_post(self):
  25. u = m.User.from_request(r.User.from_json(flask.request.data))
  26. return flask.redirect(u.base_url())
  27. @app.route("/u/<string:slug>")
  28. @endpoint
  29. class GetUser(Endpoint):
  30. def html(self, slug: str):
  31. u = m.User.by_slug(slug)
  32. pg = int(flask.request.args.get("page", 0))
  33. links = u.get_links(page=pg)
  34. return render(
  35. "main",
  36. title=f"user {u.name}",
  37. content=render("linklist", links=links),
  38. user=self.user,
  39. )
  40. def api_get(self, slug: str):
  41. return m.User.by_slug(slug).to_dict()
  42. @app.route("/u/<string:user>/l")
  43. def create_link(user: str):
  44. pass
  45. @app.route("/u/<string:user>/l/<string:link>")
  46. def link(user: str, link: str):
  47. pass
  48. @app.route("/u/<string:user>/t/<path:tag>")
  49. def get_tagged_links(user: str, tag: str):
  50. u = m.User.by_slug(user)
  51. pg = int(flask.request.args.get("page", 0))
  52. t = u.get_tag(tag)
  53. links = t.get_links(page=pg)
  54. return render(
  55. "main", title=f"tag {tag}", content=render("linklist", links=links), user=u,
  56. )