From 04565affb29eac83a77153d4d60abf8090a574dd Mon Sep 17 00:00:00 2001 From: Felix Stupp Date: Sat, 27 Aug 2022 23:46:38 +0200 Subject: [PATCH] Add & use this_url template global helper --- server/app.py | 13 +++++++++++++ server/templates/macros.htm | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/server/app.py b/server/app.py index fde07a6..ec9a926 100644 --- a/server/app.py +++ b/server/app.py @@ -18,6 +18,7 @@ from typing import ( Dict, Iterable, List, + Mapping, Optional, Set, Union, @@ -30,6 +31,7 @@ from flask import ( redirect, request, send_file, + url_for, ) from flask.templating import render_template from markupsafe import Markup @@ -204,6 +206,17 @@ Pony(flask_app) def encode_options(opts: dict[str, Any]): return urlencode({k: str(v) for k, v in opts.items()}, quote_via=quote_plus) + +@flask_app.template_global() +def this_url(changed_args: Mapping[str, str] = {}): + view_args = dict(request.view_args) + get_args = request.args.to_dict() + get_args.update(changed_args) + return url_for(request.endpoint, **view_args) + ( + f"?{encode_options(get_args)}" if get_args else "" + ) + + @flask_app.template_filter() def as_link(uri: str): uri = Markup.escape(uri) diff --git a/server/templates/macros.htm b/server/templates/macros.htm index 492c72e..93a1525 100644 --- a/server/templates/macros.htm +++ b/server/templates/macros.htm @@ -63,7 +63,7 @@ {% endmacro %} {% macro hidden_redirect_back(fragment="") %} - + {% endmacro %} {% macro no_input_post_form(uri, text, fragment="") -%}