Commit a5be6fc9 authored by Kevin Morris's avatar Kevin Morris
Browse files

aurweb.templates: add make_variable_context



A new make_context wrapper which additionally includes either
query parameters (get) or form data (post) in the context.

Use this to simplify setting context variables for form data
in particular.

Signed-off-by: Kevin Morris's avatarKevin Morris <kevr@0cost.org>
parent 9052688e
......@@ -9,7 +9,7 @@ from aurweb.auth import auth_required
from aurweb.l10n import get_translator_for_request
from aurweb.models.user import User
from aurweb.scripts.notify import ResetKeyNotification
from aurweb.templates import make_context, render_template
from aurweb.templates import make_variable_context, render_template
router = APIRouter()
......@@ -17,11 +17,7 @@ router = APIRouter()
@router.get("/passreset", response_class=HTMLResponse)
@auth_required(False)
async def passreset(request: Request):
context = make_context(request, "Password Reset")
for k, v in request.query_params.items():
context[k] = v
context = await make_variable_context(request, "Password Reset")
return render_template(request, "passreset.html", context)
......@@ -34,10 +30,7 @@ async def passreset_post(request: Request,
confirm: str = Form(default=None)):
from aurweb.db import session
context = make_context(request, "Password Reset")
for k, v in dict(await request.form()).items():
context[k] = v
context = await make_variable_context(request, "Password Reset")
# The user parameter being required, we can match against
user = db.query(User, or_(User.Username == user,
......
......@@ -41,6 +41,20 @@ def make_context(request: Request, title: str, next: str = None):
}
async def make_variable_context(request: Request, title: str, next: str = None):
""" Make a context with variables provided by the user
(query params via GET or form data via POST). """
context = make_context(request, title, next)
to_copy = dict(request.query_params) \
if request.method.lower() == "get" \
else dict(await request.form())
for k, v in to_copy.items():
context[k] = v
return context
def render_template(request: Request,
path: str,
context: dict,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment