From 41b6698c551bf4e5244e623dd00101dae31ee2b6 Mon Sep 17 00:00:00 2001 From: Sergey Vanyushkin Date: Sat, 2 May 2026 16:48:44 +0300 Subject: [PATCH] fix: add nl2br filter and fix TemplateResponse arguments - Add nl2br Jinja2 filter to convert newlines to
tags - Fix TemplateResponse argument order (request first) in error handlers - Fix type annotations for mypy - All 97 tests passing --- app/presentation/web/error_handlers.py | 12 +++++++++--- app/presentation/web/routes.py | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/app/presentation/web/error_handlers.py b/app/presentation/web/error_handlers.py index db1f162..61b9ef7 100644 --- a/app/presentation/web/error_handlers.py +++ b/app/presentation/web/error_handlers.py @@ -69,8 +69,10 @@ async def http_exception_handler(request: Request, exc: HTTPException) -> HTMLRe HTMLResponse with error page. """ # Handle redirects (307, 308) - if exc.status_code in (307, 308) and "Location" in exc.headers: - return RedirectResponse(url=exc.headers["Location"], status_code=exc.status_code) + if exc.status_code in (307, 308): + location = exc.headers.get("Location") if exc.headers else None + if location: + return RedirectResponse(url=location, status_code=exc.status_code) # type: ignore[return-value] error_pages = { 403: ("Access Denied", "You don't have permission to access this page."), @@ -92,6 +94,7 @@ async def http_exception_handler(request: Request, exc: HTTPException) -> HTMLRe ) return templates.TemplateResponse( + request, "pages/error.html", context, status_code=exc.status_code, @@ -118,6 +121,7 @@ async def not_found_handler(request: Request, exc: HTTPException) -> HTMLRespons ) return templates.TemplateResponse( + request, "pages/error.html", context, status_code=404, @@ -144,6 +148,7 @@ async def forbidden_handler(request: Request, exc: HTTPException) -> HTMLRespons ) return templates.TemplateResponse( + request, "pages/error.html", context, status_code=403, @@ -170,13 +175,14 @@ async def server_error_handler(request: Request, exc: Exception) -> HTMLResponse ) return templates.TemplateResponse( + request, "pages/error.html", context, status_code=500, ) -def register_error_handlers(app) -> None: +def register_error_handlers(app: Any) -> None: """Register error handlers with FastAPI app. Args: diff --git a/app/presentation/web/routes.py b/app/presentation/web/routes.py index eb44ec1..b370a37 100644 --- a/app/presentation/web/routes.py +++ b/app/presentation/web/routes.py @@ -28,6 +28,21 @@ router = APIRouter(prefix="/web", tags=["web"]) templates = Jinja2Templates(directory="app/presentation/templates") +def nl2br(value: str) -> str: + """Convert newlines to HTML line breaks. + + Args: + value: String with newlines. + + Returns: + String with
tags instead of newlines. + """ + return value.replace("\n", "
\n") + + +templates.env.filters["nl2br"] = nl2br + + class MockPost: """Mock post object for UI demonstration.