|
@@ -1,90 +1,15 @@
|
|
import contextlib
|
|
import contextlib
|
|
import os
|
|
import os
|
|
import flask
|
|
import flask
|
|
-import pystache
|
|
|
|
import sys
|
|
import sys
|
|
|
|
|
|
import lc.config as c
|
|
import lc.config as c
|
|
import lc.error as e
|
|
import lc.error as e
|
|
import lc.model as m
|
|
import lc.model as m
|
|
import lc.request as r
|
|
import lc.request as r
|
|
|
|
+from lc.web import Endpoint, endpoint, render
|
|
|
|
|
|
app = flask.Flask(__name__)
|
|
app = flask.Flask(__name__)
|
|
-loader = pystache.loader.Loader(extension="mustache", search_dirs=["templates"])
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-def render(name, **kwargs):
|
|
|
|
- """Load and use a Mustache template from the project root"""
|
|
|
|
- template = loader.load_name(name)
|
|
|
|
- renderer = pystache.Renderer(missing_tags="strict", search_dirs=["templates"])
|
|
|
|
- return renderer.render(template, kwargs)
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-class Endpoint:
|
|
|
|
- def __init__(self):
|
|
|
|
- self.user = None
|
|
|
|
-
|
|
|
|
- # try finding the token
|
|
|
|
- token = None
|
|
|
|
- if (auth := flask.request.headers["Authorization"]) :
|
|
|
|
- token = auth.split()[1]
|
|
|
|
- elif flask.session["auth"]:
|
|
|
|
- token = flask.session["auth"]
|
|
|
|
-
|
|
|
|
- if token and (payload := c.SERIALIZER.loads(token)):
|
|
|
|
- if "name" not in payload or "password" not in payload:
|
|
|
|
- return
|
|
|
|
-
|
|
|
|
- try:
|
|
|
|
- u = m.User.by_slug(payload["name"])
|
|
|
|
- except e.LCException:
|
|
|
|
- return
|
|
|
|
-
|
|
|
|
- if u.authenticate(payload["password"]):
|
|
|
|
- self.user = u
|
|
|
|
-
|
|
|
|
- def require_authentication(self, name: user):
|
|
|
|
- if name != self.user.name:
|
|
|
|
- raise e.BadPermissions()
|
|
|
|
-
|
|
|
|
- def api_post(self, *args, **kwargs) -> dict:
|
|
|
|
- raise e.NotImplemented()
|
|
|
|
-
|
|
|
|
- def api_get(self, *args, **kwargs) -> dict:
|
|
|
|
- raise e.NotImplemented()
|
|
|
|
-
|
|
|
|
- def html(self, *args, **kwargs):
|
|
|
|
- raise e.NotImplemented()
|
|
|
|
-
|
|
|
|
- def route(self, *args, **kwargs):
|
|
|
|
- try:
|
|
|
|
- if flask.request.method == "POST":
|
|
|
|
- require_authentication()
|
|
|
|
- return flask.jsonify(self.api_post(*args, **kwargs))
|
|
|
|
- elif (
|
|
|
|
- flask.request.method in ["GET", "HEAD"]
|
|
|
|
- and flask.request.content_type == "application/json"
|
|
|
|
- ):
|
|
|
|
- return flask.jsonify(self.api_get(*args, **kwargs))
|
|
|
|
- except e.LCException as exn:
|
|
|
|
- return ({"status": exn.http_code(), "error": str(exn)}, exn.http_code())
|
|
|
|
-
|
|
|
|
- try:
|
|
|
|
- return self.html(*args, **kwargs)
|
|
|
|
- except e.LCException as exn:
|
|
|
|
- page = render(
|
|
|
|
- "main", title="error", content=f"shit's fucked yo: {exn}", user=None,
|
|
|
|
- )
|
|
|
|
- return (page, exn.http_code())
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-def endpoint(cls):
|
|
|
|
- def func(*args, **kwargs):
|
|
|
|
- return cls().route(*args, **kwargs)
|
|
|
|
-
|
|
|
|
- func.__name__ = cls.__name__
|
|
|
|
- return func
|
|
|
|
-
|
|
|
|
|
|
|
|
@app.route("/")
|
|
@app.route("/")
|
|
@endpoint
|
|
@endpoint
|
|
@@ -122,7 +47,7 @@ class GetUser(Endpoint):
|
|
user=self.user,
|
|
user=self.user,
|
|
)
|
|
)
|
|
|
|
|
|
- def api_get(self, current_user, slug: str):
|
|
|
|
|
|
+ def api_get(self, slug: str):
|
|
return m.User.by_slug(slug).to_dict()
|
|
return m.User.by_slug(slug).to_dict()
|
|
|
|
|
|
|
|
|