"""Jinja2 template engine for the admin panel. Templates ship inside the package (``src/contract_check/api/admin/templates``) so the Docker image picks them up via the wheel install. A ``dt`` filter keeps datetime rendering consistent. """ from __future__ import annotations import datetime as dt import json from pathlib import Path from fastapi import Response from fastapi.templating import Jinja2Templates _TEMPLATES_DIR = Path(__file__).resolve().parent / "templates" templates = Jinja2Templates(directory=str(_TEMPLATES_DIR)) def _format_dt(value: object, fmt: str = "%Y-%m-%d %H:%M UTC") -> str: if isinstance(value, dt.datetime): return value.strftime(fmt) if value is None: return "—" return str(value) def _yes_no_none(value: object) -> str: if value is True: return "да" if value is False: return "нет" return "—" def attach_toast(response: Response, toast: str | None) -> None: """Attach an HTMX toast trigger to any response that supports headers.""" if toast: response.headers["HX-Trigger"] = json.dumps({"showToast": toast}) templates.env.filters["dt"] = _format_dt templates.env.filters["yn"] = _yes_no_none