web.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import flask
  2. import pystache
  3. from typing import TypeVar, Type
  4. import lc.config as c
  5. import lc.error as e
  6. import lc.model as m
  7. import lc.request as r
  8. T = TypeVar("T", bound=r.Request)
  9. class Endpoint:
  10. def __init__(self):
  11. self.user = None
  12. # try finding the token
  13. token = None
  14. # first check the HTTP headers
  15. if (auth := flask.request.headers.get("Authorization", None)) :
  16. token = auth.split()[1]
  17. # if that fails, check the session
  18. elif flask.session.get("auth", None):
  19. token = flask.session["auth"]
  20. # if that exists and we can deserialize it, then make sure
  21. # it contains a valid user password, too
  22. if token and (payload := c.SERIALIZER.loads(token)):
  23. if "name" not in payload or "password" not in payload:
  24. return
  25. try:
  26. u = m.User.by_slug(payload["name"])
  27. except e.LCException:
  28. return
  29. if u.authenticate(payload["password"]):
  30. self.user = u
  31. def request_data(self, cls: Type[T]) -> T:
  32. """Construct a Request model from either a JSON payload or a urlencoded payload"""
  33. if flask.request.content_type == "application/json":
  34. return cls.from_json(flask.request.data)
  35. elif flask.request.content_type == "application/x-www-form-urlencoded":
  36. return cls.from_form(flask.request.form)
  37. else:
  38. raise e.BadContentType(flask.request.content_type or "unknown")
  39. def require_authentication(self, name: str) -> m.User:
  40. """
  41. Check that the currently logged-in user exists and is the
  42. same as the user whose username is given. Raises an exception
  43. otherwise.
  44. """
  45. if not self.user or name != self.user.name:
  46. raise e.BadPermissions()
  47. return self.user
  48. def route(self, *args, **kwargs):
  49. """Forward to the appropriate routing method"""
  50. try:
  51. if flask.request.method == "POST":
  52. # all POST methods are "API methods": if we want to
  53. # display information in response to a post, then we
  54. # should redirect to the page where that information
  55. # can be viewed instead of returning that
  56. # information. (I think.)
  57. return flask.jsonify(self.api_post(*args, **kwargs))
  58. elif (
  59. flask.request.method in ["GET", "HEAD"]
  60. and flask.request.content_type == "application/json"
  61. ):
  62. # Here we're distinguishing between an API GET (i.e. a
  63. # client trying to get JSON data about an endpoint)
  64. # versus a user-level GET (i.e. a user in a browser.)
  65. # I like using the HTTP headers to distinguish these
  66. # cases, while other APIs tend to have a separate /api
  67. # endpoint to do this.
  68. return flask.jsonify(self.api_get(*args, **kwargs))
  69. # if an exception arose from an "API method", then we should
  70. # report it as JSON
  71. except e.LCException as exn:
  72. return ({"status": exn.http_code(), "error": str(exn)}, exn.http_code())
  73. # also maybe we tried to redirect, so just do that
  74. except e.LCRedirect as exn:
  75. return flask.redirect(exn.to_path())
  76. # if we're here, it means we're just trying to get a typical
  77. # HTML request.
  78. try:
  79. return self.html(*args, **kwargs)
  80. except e.LCException as exn:
  81. page = render(
  82. "main", title="error", content=f"shit's fucked yo: {exn}", user=None,
  83. )
  84. return (page, exn.http_code())
  85. except e.LCRedirect as exn:
  86. return flask.redirect(exn.to_path())
  87. # Decorators result in some weird code in Python, especially 'cause it
  88. # doesn't make higher-order functions terse. Let's break this down a
  89. # bit. This out method, `endpoint`, takes the route...
  90. def endpoint(route: str):
  91. """Route an endpoint using our semi-smart routing machinery"""
  92. # but `endpoint` returns another function which is going to be
  93. # called with the result of the definition after it. The argument
  94. # to what we're calling `do_endpoint` here is going to be the
  95. # class object defined afterwards.
  96. def do_endpoint(endpoint_class):
  97. # we'll just make that explicit here
  98. assert Endpoint in endpoint_class.__bases__
  99. # finally, we need a function that we'll give to Flask in
  100. # order to actually dispatch to. This is the actual routing
  101. # function, which is why it just creates an instance of the
  102. # endpoint provided above and calls the `route` method on it
  103. def func(*args, **kwargs):
  104. return endpoint_class().route(*args, **kwargs)
  105. # use reflection over the methods defined by the endpoint
  106. # class to decide if it needs to accept POST requests or not.
  107. methods = ["GET"]
  108. if "api_post" in dir(endpoint_class):
  109. methods.append("POST")
  110. # this is just for making error messages nicer
  111. func.__name__ = endpoint_class.__name__
  112. # finally, use the Flask routing machinery to register our callback
  113. return c.app.route(route, methods=methods)(func)
  114. return do_endpoint
  115. LOADER = pystache.loader.Loader(extension="mustache", search_dirs=["templates"])
  116. def render(name, **kwargs):
  117. """Load and use a Mustache template from the project root"""
  118. template = LOADER.load_name(name)
  119. renderer = pystache.Renderer(missing_tags="strict", search_dirs=["templates"])
  120. return renderer.render(template, kwargs)