routes.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import os
  2. import flask
  3. import pystache
  4. import sys
  5. import lc.config
  6. import lc.model as m
  7. import lc.request as r
  8. app = flask.Flask(__name__)
  9. loader = pystache.loader.Loader(extension="mustache", search_dirs=["templates"])
  10. def render(name, **kwargs):
  11. """Load and use a Mustache template from the project root"""
  12. template = loader.load_name(name)
  13. renderer = pystache.Renderer(missing_tags="strict", search_dirs=["templates"])
  14. return renderer.render(template, kwargs)
  15. @app.route("/")
  16. def index():
  17. return render("main", title="main", content="whoo")
  18. @app.route("/u", methods=["POST"])
  19. def create_user():
  20. print(flask.request.data)
  21. u = m.User.from_request(r.User.from_json(flask.request.data))
  22. return flask.redirect(u.base_url())
  23. @app.route("/u/<string:user>", methods=["GET", "POST"])
  24. def get_user(user: str):
  25. u = m.User.by_slug(user)
  26. pg = int(flask.request.args.get("page", 0))
  27. links = u.get_links(page=pg)
  28. return render(
  29. "main", title=f"user {u.name}", content=render("linklist", links=links),
  30. )
  31. @app.route("/u/<string:user>/l", methods=["POST"])
  32. def create_link(user: str):
  33. pass
  34. @app.route("/u/<string:user>/l/<string:link>", methods=["GET", "POST"])
  35. def link(user: str, link: str):
  36. pass
  37. @app.route("/u/<string:user>/t/<path:tag>")
  38. def get_tagged_links(user: str, tag: str):
  39. u = m.User.by_slug(user)
  40. pg = int(flask.request.args.get("page", 0))
  41. t = u.get_tag(tag)
  42. links = t.get_links(page=pg)
  43. return render("main", title=f"tag {tag}", content=render("linklist", links=links),)