Files
Onyscribe-Admin/app/templating.py

44 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from __future__ import annotations
from datetime import datetime
from zoneinfo import ZoneInfo
from fastapi.templating import Jinja2Templates
from app.settings import APP_NAME, ROOT
templates = Jinja2Templates(directory=str(ROOT / "app" / "templates"))
BERLIN = ZoneInfo("Europe/Berlin")
def dt(value: datetime | None, fmt: str = "%d.%m.%Y %H:%M") -> str:
if not value:
return "–"
if value.tzinfo is None:
from datetime import timezone
value = value.replace(tzinfo=timezone.utc)
return value.astimezone(BERLIN).strftime(fmt)
templates.env.filters["dt"] = dt
templates.env.globals["APP_NAME"] = APP_NAME
def _live() -> bool:
from app.services import deploy
return deploy.live_enabled()
templates.env.globals["deploy_live"] = _live
def _static_v(name: str) -> str:
"""Cache-Busting: Änderungszeit der Datei als Query-Parameter, damit Browser nach einem Deploy nie veraltete JS/CSS-Dateien nutzen."""
try:
return str(int((ROOT / "app" / "static" / name).stat().st_mtime))
except OSError:
return "0"
templates.env.globals["static_v"] = _static_v