Skip to content
Snippets Groups Projects
Commit 4423326c authored by Kevin Morris's avatar Kevin Morris
Browse files

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's avatarKevin Morris <kevr@0cost.org>
parent 5d4a5ded
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
......@@ -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 = {
......
......@@ -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.
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment