From 4423326cec91dbfc9cd90294fc09ca40e917bc63 Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Fri, 8 Jan 2021 20:24:22 -0800 Subject: [PATCH] add the request parameter to render_template This allows us to inspect things about the request we're rendering from. * Use render_template(request, ...) in aurweb.routers.auth Signed-off-by: Kevin Morris --- aurweb/routers/auth.py | 2 +- aurweb/routers/errors.py | 4 ++-- aurweb/routers/html.py | 2 +- aurweb/templates.py | 10 ++++++++-- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/aurweb/routers/auth.py b/aurweb/routers/auth.py index 24f5d4e3..3a1c7192 100644 --- a/aurweb/routers/auth.py +++ b/aurweb/routers/auth.py @@ -17,7 +17,7 @@ def login_template(request: Request, next: str, errors: list = None): context = make_context(request, "Login", next) context["errors"] = errors context["url_base"] = f"{request.url.scheme}://{request.url.netloc}" - return render_template("login.html", context) + return render_template(request, "login.html", context) @router.get("/login", response_class=HTMLResponse) diff --git a/aurweb/routers/errors.py b/aurweb/routers/errors.py index 111d802a..eb935b57 100644 --- a/aurweb/routers/errors.py +++ b/aurweb/routers/errors.py @@ -3,12 +3,12 @@ from aurweb.templates import make_context, render_template async def not_found(request, exc): context = make_context(request, "Page Not Found") - return render_template("errors/404.html", context, 404) + return render_template(request, "errors/404.html", context, 404) async def service_unavailable(request, exc): context = make_context(request, "Service Unavailable") - return render_template("errors/503.html", context, 503) + return render_template(request, "errors/503.html", context, 503) # Maps HTTP errors to functions exceptions = { diff --git a/aurweb/routers/html.py b/aurweb/routers/html.py index 50b62450..32a7e630 100644 --- a/aurweb/routers/html.py +++ b/aurweb/routers/html.py @@ -47,7 +47,7 @@ async def language(request: Request, async def index(request: Request): """ Homepage route. """ context = make_context(request, "Home") - return render_template("index.html", context) + return render_template(request, "index.html", context) # A route that returns a error 503. For testing purposes. diff --git a/aurweb/templates.py b/aurweb/templates.py index c05dce79..c5f378b8 100644 --- a/aurweb/templates.py +++ b/aurweb/templates.py @@ -39,7 +39,10 @@ def make_context(request: Request, title: str, next: str = None): } -def render_template(path: str, context: dict, status_code=int(HTTPStatus.OK)): +def render_template(request: Request, + path: str, + context: dict, + status_code=int(HTTPStatus.OK)): """ Render a Jinja2 multi-lingual template with some context. """ # Create a deep copy of our jinja2 environment. The environment in @@ -54,4 +57,7 @@ def render_template(path: str, context: dict, status_code=int(HTTPStatus.OK)): template = templates.get_template(path) rendered = template.render(context) - return HTMLResponse(rendered, status_code=status_code) + + response = HTMLResponse(rendered, status_code=status_code) + response.set_cookie("AURLANG", context.get("language")) + return response