feat: jinja template for cards
This commit is contained in:
+35
@@ -0,0 +1,35 @@
|
||||
from collections.abc import MutableMapping
|
||||
from json import load
|
||||
from pathlib import Path
|
||||
|
||||
LANG_DIR = Path(__file__).parent / "lang"
|
||||
FALLBACK_LANG = "en"
|
||||
LANG_CACHE: dict[str, dict[str, str]] = {}
|
||||
|
||||
|
||||
# https://stackoverflow.com/questions/6027558/flatten-nested-dictionaries-compressing-keys
|
||||
def __flatten(dictionary: dict, parent_key: str = "") -> dict[str, str]:
|
||||
items = []
|
||||
for key, value in dictionary.items():
|
||||
new_key = parent_key + "." + key if parent_key else key
|
||||
if isinstance(value, MutableMapping):
|
||||
items.extend(__flatten(value, new_key).items())
|
||||
else:
|
||||
items.append((new_key, value))
|
||||
return dict(items)
|
||||
|
||||
|
||||
def __get_lang_data(lang: str) -> dict[str, str]:
|
||||
if lang not in LANG_CACHE:
|
||||
file = LANG_DIR / f"{lang}.json"
|
||||
if not file.exists():
|
||||
return __get_lang_data(FALLBACK_LANG)
|
||||
with file.open() as f:
|
||||
raw_data: dict = load(f)
|
||||
LANG_CACHE[lang] = __flatten(raw_data)
|
||||
return LANG_CACHE[lang]
|
||||
|
||||
|
||||
def get_lang(lang: str, key: str) -> str:
|
||||
data = __get_lang_data(lang)
|
||||
return data.get(key, key)
|
||||
@@ -13,6 +13,7 @@ class Parameters:
|
||||
env: str = "production"
|
||||
bind: str = "0.0.0.0"
|
||||
port: int = 5000
|
||||
app_url: str = "http://localhost:5000"
|
||||
workers: int = 4
|
||||
timeout: int = 120
|
||||
db_uri: str = "sqlite://db.sqlite"
|
||||
@@ -87,6 +88,13 @@ def parse_parameters(args: typing.Sequence[str]) -> Parameters:
|
||||
default=default_values.env,
|
||||
help_txt="environnement",
|
||||
)
|
||||
__add_arg_str(
|
||||
parser,
|
||||
"--app-url",
|
||||
env_var="APP_URL",
|
||||
default=default_values.app_url,
|
||||
help_txt="app external url",
|
||||
)
|
||||
__add_arg_str(
|
||||
parser,
|
||||
"--bind",
|
||||
|
||||
+18
-4
@@ -8,6 +8,7 @@ import gunicorn.app.base
|
||||
import tortoise
|
||||
|
||||
from app import PKG_NAME
|
||||
from app.lang import get_lang
|
||||
from app.models import Task
|
||||
|
||||
if typing.TYPE_CHECKING:
|
||||
@@ -22,6 +23,7 @@ class Server(gunicorn.app.base.BaseApplication):
|
||||
self.debug = params.debug
|
||||
self.workers = params.workers
|
||||
self.timeout = params.timeout
|
||||
self.app_url = params.app_url
|
||||
self.app = flask.Flask(
|
||||
PKG_NAME, static_folder="dist/assets", template_folder="dist"
|
||||
)
|
||||
@@ -36,11 +38,23 @@ class Server(gunicorn.app.base.BaseApplication):
|
||||
super().__init__()
|
||||
|
||||
def register_routes(self) -> typing.Self:
|
||||
@self.app.route("/", defaults={"_": ""})
|
||||
@self.app.route("/<_>")
|
||||
@self.app.route("/", defaults={"path": ""})
|
||||
@self.app.route("/<path>")
|
||||
@self.limiter.exempt
|
||||
def index(_: str) -> str:
|
||||
return flask.render_template("index.html")
|
||||
def index(path: str) -> str:
|
||||
locale = "en"
|
||||
if "lang" in flask.request.cookies:
|
||||
locale = flask.request.cookies["lang"]
|
||||
if "lang" in flask.request.args:
|
||||
locale = flask.request.args["lang"]
|
||||
return flask.render_template(
|
||||
"index.html",
|
||||
title=("Tout Doux" if not len(path) else f"Tout Doux - {path}"),
|
||||
app_url=self.app_url,
|
||||
path=path,
|
||||
locale=locale,
|
||||
lang=lambda key: get_lang(locale, key),
|
||||
)
|
||||
|
||||
@self.app.route("/api/lists/<list_name>/tasks", methods=["GET"])
|
||||
async def get_tasks(list_name: str) -> list:
|
||||
|
||||
Reference in New Issue
Block a user