Add & use this_url template global helper

master
Felix Stupp 2 years ago
parent 2e767fddf0
commit 04565affb2
Signed by: zocker
GPG Key ID: 93E1BD26F6B02FB7

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

@ -63,7 +63,7 @@
{% endmacro %}
{% macro hidden_redirect_back(fragment="") %}
<input type="hidden" name="redirect" value="{{ url_for(request.endpoint, **request.view_args) }}{{ ('#' + fragment) if fragment else '' }}"/>
<input type="hidden" name="redirect" value="{{ this_url() }}{{ ('#' + fragment) if fragment else '' }}"/>
{% endmacro %}
{% macro no_input_post_form(uri, text, fragment="") -%}

Loading…
Cancel
Save