DealDocumentScreening/src/contract_check/api/admin/templating.py
febux 0b2e7665f4 fixup(pipeline-hardening M4/M5): purge count, shared toast helper, clean imports
- Fix RabbitMQ purge_queue to report the pre-purge message count instead of
  the post-purge depth (which was always 0).
- Add shared attach_toast helper in admin templating; remove duplicated inline
  _attach_toast implementations in review.py and mq.py.
- Keep purge confirmation case-sensitive so the typed safeguard works as intended.
- Fix test imports to use known-first-party 'contract_check' per isort config.
2026-09-06 01:18:33 +03:00

45 lines
1.2 KiB
Python

"""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