mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
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 <kevr@0cost.org>
This commit is contained in:
parent
5d4a5deddf
commit
4423326cec
4 changed files with 12 additions and 6 deletions
|
@ -17,7 +17,7 @@ def login_template(request: Request, next: str, errors: list = None):
|
||||||
context = make_context(request, "Login", next)
|
context = make_context(request, "Login", next)
|
||||||
context["errors"] = errors
|
context["errors"] = errors
|
||||||
context["url_base"] = f"{request.url.scheme}://{request.url.netloc}"
|
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)
|
@router.get("/login", response_class=HTMLResponse)
|
||||||
|
|
|
@ -3,12 +3,12 @@ from aurweb.templates import make_context, render_template
|
||||||
|
|
||||||
async def not_found(request, exc):
|
async def not_found(request, exc):
|
||||||
context = make_context(request, "Page Not Found")
|
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):
|
async def service_unavailable(request, exc):
|
||||||
context = make_context(request, "Service Unavailable")
|
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
|
# Maps HTTP errors to functions
|
||||||
exceptions = {
|
exceptions = {
|
||||||
|
|
|
@ -47,7 +47,7 @@ async def language(request: Request,
|
||||||
async def index(request: Request):
|
async def index(request: Request):
|
||||||
""" Homepage route. """
|
""" Homepage route. """
|
||||||
context = make_context(request, "Home")
|
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.
|
# 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. """
|
""" Render a Jinja2 multi-lingual template with some context. """
|
||||||
|
|
||||||
# Create a deep copy of our jinja2 environment. The environment in
|
# 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)
|
template = templates.get_template(path)
|
||||||
rendered = template.render(context)
|
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
|
||||||
|
|
Loading…
Add table
Reference in a new issue