fix: add nl2br filter and fix TemplateResponse arguments
- Add nl2br Jinja2 filter to convert newlines to <br> tags - Fix TemplateResponse argument order (request first) in error handlers - Fix type annotations for mypy - All 97 tests passing
This commit is contained in:
@@ -69,8 +69,10 @@ async def http_exception_handler(request: Request, exc: HTTPException) -> HTMLRe
|
|||||||
HTMLResponse with error page.
|
HTMLResponse with error page.
|
||||||
"""
|
"""
|
||||||
# Handle redirects (307, 308)
|
# Handle redirects (307, 308)
|
||||||
if exc.status_code in (307, 308) and "Location" in exc.headers:
|
if exc.status_code in (307, 308):
|
||||||
return RedirectResponse(url=exc.headers["Location"], status_code=exc.status_code)
|
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 = {
|
error_pages = {
|
||||||
403: ("Access Denied", "You don't have permission to access this page."),
|
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(
|
return templates.TemplateResponse(
|
||||||
|
request,
|
||||||
"pages/error.html",
|
"pages/error.html",
|
||||||
context,
|
context,
|
||||||
status_code=exc.status_code,
|
status_code=exc.status_code,
|
||||||
@@ -118,6 +121,7 @@ async def not_found_handler(request: Request, exc: HTTPException) -> HTMLRespons
|
|||||||
)
|
)
|
||||||
|
|
||||||
return templates.TemplateResponse(
|
return templates.TemplateResponse(
|
||||||
|
request,
|
||||||
"pages/error.html",
|
"pages/error.html",
|
||||||
context,
|
context,
|
||||||
status_code=404,
|
status_code=404,
|
||||||
@@ -144,6 +148,7 @@ async def forbidden_handler(request: Request, exc: HTTPException) -> HTMLRespons
|
|||||||
)
|
)
|
||||||
|
|
||||||
return templates.TemplateResponse(
|
return templates.TemplateResponse(
|
||||||
|
request,
|
||||||
"pages/error.html",
|
"pages/error.html",
|
||||||
context,
|
context,
|
||||||
status_code=403,
|
status_code=403,
|
||||||
@@ -170,13 +175,14 @@ async def server_error_handler(request: Request, exc: Exception) -> HTMLResponse
|
|||||||
)
|
)
|
||||||
|
|
||||||
return templates.TemplateResponse(
|
return templates.TemplateResponse(
|
||||||
|
request,
|
||||||
"pages/error.html",
|
"pages/error.html",
|
||||||
context,
|
context,
|
||||||
status_code=500,
|
status_code=500,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def register_error_handlers(app) -> None:
|
def register_error_handlers(app: Any) -> None:
|
||||||
"""Register error handlers with FastAPI app.
|
"""Register error handlers with FastAPI app.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|||||||
@@ -28,6 +28,21 @@ router = APIRouter(prefix="/web", tags=["web"])
|
|||||||
templates = Jinja2Templates(directory="app/presentation/templates")
|
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 <br> tags instead of newlines.
|
||||||
|
"""
|
||||||
|
return value.replace("\n", "<br>\n")
|
||||||
|
|
||||||
|
|
||||||
|
templates.env.filters["nl2br"] = nl2br
|
||||||
|
|
||||||
|
|
||||||
class MockPost:
|
class MockPost:
|
||||||
"""Mock post object for UI demonstration.
|
"""Mock post object for UI demonstration.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user