Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

4.0 KB
Raw Blame
"""
Template rendering helpers for FastAPI.

Provides render_template() that injects request-specific context
(session, CSRF token, flash messages) into every template render,
matching Flask's behavior.
"""

from fastapi import Request
from loguru import logger
from starlette.responses import HTMLResponse

from ..template_config import templates
from .csrf import generate_csrf_token
from .flash import get_flashed_messages


def render_template(
    request: Request,
    name: str,
    context: dict | None = None,
    status_code: int = 200,
) -> HTMLResponse:
    """Render a Jinja2 template with request-specific context injected.

    This is the FastAPI equivalent of Flask's render_template(). It
    automatically adds session, CSRF token, and flash messages to the
    template context so templates work identically to Flask.

    Args:
        request: The current request.
        name: Template name (e.g. "auth/login.html").
        context: Template context variables.
        status_code: HTTP status code.

    Returns:
        HTMLResponse with rendered template.
    """
    ctx = context or {}

    # Inject request-specific globals that templates expect
    ctx.setdefault("request", request)
    ctx.setdefault("session", request.session)

    # CSRF token callable — templates call {{ csrf_token() }}. Preserve an
    # explicit route-provided callable, matching _LDRTemplates.TemplateResponse.
    if "csrf_token" not in ctx:
        token = generate_csrf_token(request)  # gitleaks:allow
        ctx["csrf_token"] = lambda: token

    # Flash messages — templates call {{ get_flashed_messages(with_categories=true) }}
    if "get_flashed_messages" not in ctx:
        flashes = get_flashed_messages(request, with_categories=True)
        ctx["get_flashed_messages"] = lambda with_categories=False: (
            flashes if with_categories else [msg for _, msg in flashes]
        )

    # Make the active egress scope available to every template so base.html
    # can render it onto <body data-scope=…>. Scope-aware CSS in styles.css
    # picks it up to color the research card, chat input, etc. Falls back
    # to the registered default scope if anything goes wrong — fail open
    # visually, never crash a page render over a styling cue.
    from ...security.egress.policy import DEFAULT_EGRESS_SCOPE

    scope = DEFAULT_EGRESS_SCOPE
    try:
        from ...database.session_context import get_user_db_session
        from ...utilities.db_utils import get_settings_manager

        username = request.session.get("username") if request.session else None
        if username:
            with get_user_db_session(username) as db_session:
                if db_session:
                    sm = get_settings_manager(db_session, username)
                    scope = (
                        sm.get_setting(
                            "policy.egress_scope", DEFAULT_EGRESS_SCOPE
                        )
                        or DEFAULT_EGRESS_SCOPE
                    )
                    # Canonicalise before it reaches the template: this collapses case
                    # and whitespace and maps invalid / retired values to the
                    # protective default. Without it a stored "STRICT" renders
                    # data-scope="STRICT", and base.html's body[data-scope="strict"]
                    # selector -- attribute values are case-sensitive -- silently stops
                    # matching, so the strict-scope visual cue disappears. Lost in the
                    # port; main applied it at app_factory.py:585.
                    from ...security.egress.policy import (
                        effective_scope_for_display,
                    )

                    scope = effective_scope_for_display(scope)
    except Exception:
        logger.debug(
            "Failed to read egress scope for template context",
            exc_info=True,
        )
    ctx.setdefault("egress_scope", scope)

    return templates.TemplateResponse(
        request=request,
        name=name,
        context=ctx,
        status_code=status_code,
    )