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 <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-01-27 18:30:57 -08:00
parent 9052688ed2
commit a5be6fc9be
2 changed files with 17 additions and 10 deletions

View file

@ -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,