diff --git a/src/moin/app.py b/src/moin/app.py
index 1ccf93580..6646df9a0 100644
--- a/src/moin/app.py
+++ b/src/moin/app.py
@@ -29,13 +29,15 @@
from jinja2 import ChoiceLoader, FileSystemLoader
from whoosh.index import EmptyIndexError
-from moin.utils import monkeypatch # noqa
-from moin.utils.clock import Clock
from moin import auth, user, config
from moin.constants.misc import ANON
from moin.i18n import i18n_init
-from moin.themes import setup_jinja_env, themed_error
+from moin.search import SearchForm
from moin.storage.middleware import protecting, indexing, routing
+from moin.themes import setup_jinja_env, themed_error, ThemeSupport
+from moin.utils import monkeypatch # noqa
+from moin.utils.clock import Clock
+from moin.utils.forms import make_generator
from moin.wikiutil import WikiLinkAnalyzer
from moin import log
@@ -192,7 +194,9 @@ class ItemNameConverter(PathConverter):
if app.cfg.template_dirs:
app.jinja_env.loader = ChoiceLoader([FileSystemLoader(app.cfg.template_dirs), app.jinja_env.loader])
app.register_error_handler(403, themed_error)
+ app.context_processor(inject_common_template_vars)
app.cfg.custom_css_path = os.path.isfile("wiki_local/custom.css")
+ setup_jinja_env(app.jinja_env)
clock.stop("create_app flask-theme")
# Create a global counter to limit Content Security Policy reports and prevent spam
app.csp_count = 0
@@ -313,18 +317,37 @@ def setup_user_anon():
flaskg.user = user.User(name=ANON, auth_method="invalid")
+def inject_common_template_vars() -> dict[str, Any]:
+ if getattr(flaskg, "no_variable_injection", False):
+ return {}
+ else:
+ return {
+ "clock": flaskg.clock,
+ "storage": flaskg.storage,
+ "user": flaskg.user,
+ "item_name": request.view_args.get("item_name", ""),
+ "theme_supp": ThemeSupport(app.cfg),
+ "cfg": app.cfg,
+ "gen": make_generator(),
+ "search_form": SearchForm.from_defaults(),
+ }
+
+
def before_wiki():
"""
Setup environment for wiki requests, start timers.
"""
- if request and (is_static_content(request.path) or request.path == "/+cspreport/log"):
- logging.debug(f"skipping before_wiki for {request.path}")
+ request_path = getattr(request, "path", "") if request else ""
+ if is_static_content(request_path) or request_path == "/+cspreport/log":
+ logging.debug(f"skipping variable injection in before_wiki for {request.path}")
+ setattr(flaskg, "no_variable_injection", True)
return
logging.debug("running before_wiki")
- flaskg.clock = Clock()
- flaskg.clock.start("total")
- flaskg.clock.start("init")
+
+ clock = flaskg.clock = Clock()
+ clock.start("total")
+ clock.start("init")
try:
flaskg.unprotected_storage = app.storage
cli_no_request_ctx = False
@@ -342,40 +365,32 @@ def before_wiki():
if cli_no_request_ctx: # no request.user_agent if this is pytest or cli
flaskg.add_lineno_attr = False
else:
- setup_jinja_env()
flaskg.add_lineno_attr = request.headers.get("User-Agent", None) and flaskg.user.edit_on_doubleclick
finally:
- flaskg.clock.stop("init")
+ clock.stop("init")
def teardown_wiki(response):
"""
Teardown environment of wiki requests, stop timers.
"""
- if request:
- request_path = request.path
- if is_static_content(request_path):
- return response
- else:
- request_path = ""
+ request_path = getattr(request, "path", "")
+ if is_static_content(request_path) or request_path == "/+cspreport/log":
+ return response
logging.debug("running teardown_wiki")
- if hasattr(flaskg, "edit_utils"):
+ if edit_utils := getattr(flaskg, "edit_utils", None):
# if transaction fails with sql file locked, we try to free it here
try:
- flaskg.edit_utils.conn.close()
+ edit_utils.conn.close()
except AttributeError:
pass
try:
# whoosh cache performance
- for cache in (
- flaskg.storage.parse_acl,
- flaskg.storage.eval_acl,
- flaskg.storage.get_acls,
- flaskg.storage.allows,
- ):
+ storage = flaskg.storage
+ for cache in (storage.parse_acl, storage.eval_acl, storage.get_acls, storage.allows):
if cache.cache_info()[3] > 0:
msg = "cache = %s: hits = %s, misses = %s, maxsize = %s, size = %s" % (
(cache.__name__,) + cache.cache_info()
@@ -386,8 +401,10 @@ def teardown_wiki(response):
pass
try:
- flaskg.clock.stop("total", comment=request_path)
- del flaskg.clock
+ clock = flaskg.pop("clock", None)
+ if clock is not None:
+ clock.stop("total", comment=request_path)
+ del clock
except AttributeError:
# can happen if teardown_wiki() is called twice, e.g. by unit tests.
pass
diff --git a/src/moin/apps/admin/templates/admin/group_acl_report.html b/src/moin/apps/admin/templates/admin/group_acl_report.html
index 2507c4830..7d7d43b7e 100644
--- a/src/moin/apps/admin/templates/admin/group_acl_report.html
+++ b/src/moin/apps/admin/templates/admin/group_acl_report.html
@@ -11,7 +11,7 @@
#}
{% extends theme("layout.html") %}
-{% import "utils.html" as utils %}
+{% import "utils.html" as utils with context %}
{% block content %}
{{ _("Group ACL Report") }}
{{ _("Group Name") }}: {{ group_name }}
diff --git a/src/moin/apps/admin/templates/admin/groupbrowser.html b/src/moin/apps/admin/templates/admin/groupbrowser.html
index 46d80311d..f6520d2ba 100644
--- a/src/moin/apps/admin/templates/admin/groupbrowser.html
+++ b/src/moin/apps/admin/templates/admin/groupbrowser.html
@@ -1,5 +1,5 @@
{% extends theme("layout.html") %}
-{% import "utils.html" as utils %}
+{% import "utils.html" as utils with context %}
{% block content %}
{{ _("Groups") }}
diff --git a/src/moin/apps/admin/templates/admin/register_new_user.html b/src/moin/apps/admin/templates/admin/register_new_user.html
index 7eedb8106..d859ac111 100644
--- a/src/moin/apps/admin/templates/admin/register_new_user.html
+++ b/src/moin/apps/admin/templates/admin/register_new_user.html
@@ -4,7 +4,7 @@
#}
{% extends theme("layout.html") %}
-{% import "forms.html" as forms %}
+{% import "forms.html" as forms with context %}
{% block content %}
{{ _("Register New User") }}
diff --git a/src/moin/apps/admin/templates/admin/trash.html b/src/moin/apps/admin/templates/admin/trash.html
index d3c9986c5..3813cf4c1 100644
--- a/src/moin/apps/admin/templates/admin/trash.html
+++ b/src/moin/apps/admin/templates/admin/trash.html
@@ -1,5 +1,5 @@
{% extends theme("layout.html") %}
-{% import "utils.html" as utils %}
+{% import "utils.html" as utils with context %}
{% block content %}
{% if headline %}
{{ headline }}
diff --git a/src/moin/apps/admin/templates/admin/user_acl_report.html b/src/moin/apps/admin/templates/admin/user_acl_report.html
index 1134197a0..49a7057d2 100644
--- a/src/moin/apps/admin/templates/admin/user_acl_report.html
+++ b/src/moin/apps/admin/templates/admin/user_acl_report.html
@@ -11,7 +11,7 @@
#}
{% extends theme("layout.html") %}
-{% import "utils.html" as utils %}
+{% import "utils.html" as utils with context %}
{% block content %}
{{ _("User ACL Report") }}
{{ _("User Names") }}: {{ user_names|join(', ') }}
diff --git a/src/moin/apps/admin/templates/user/highlighterhelp.html b/src/moin/apps/admin/templates/user/highlighterhelp.html
index 7dd01f4a7..5361e91fb 100644
--- a/src/moin/apps/admin/templates/user/highlighterhelp.html
+++ b/src/moin/apps/admin/templates/user/highlighterhelp.html
@@ -1,4 +1,4 @@
-{% import "utils.html" as utils %}
+{% import "utils.html" as utils with context %}
{% extends theme("layout.html") %}
{% block content %}
{{ _("Highlighters") }}
diff --git a/src/moin/apps/admin/templates/user/interwikihelp.html b/src/moin/apps/admin/templates/user/interwikihelp.html
index 63b15198d..cffe40c82 100644
--- a/src/moin/apps/admin/templates/user/interwikihelp.html
+++ b/src/moin/apps/admin/templates/user/interwikihelp.html
@@ -1,4 +1,4 @@
-{% import "utils.html" as utils %}
+{% import "utils.html" as utils with context %}
{% extends theme("layout.html") %}
{% block content %}
{{ _("InterWiki Names") }}
diff --git a/src/moin/apps/admin/templates/user/itemsize.html b/src/moin/apps/admin/templates/user/itemsize.html
index d537cd89b..1605043b7 100644
--- a/src/moin/apps/admin/templates/user/itemsize.html
+++ b/src/moin/apps/admin/templates/user/itemsize.html
@@ -1,4 +1,4 @@
-{% import "utils.html" as utils %}
+{% import "utils.html" as utils with context %}
{% extends theme("layout.html") %}
{% block content %}
{{ _("Item Sizes (last revision)") }}
diff --git a/src/moin/templates/404.html b/src/moin/templates/404.html
index dcea94999..e84c0503b 100644
--- a/src/moin/templates/404.html
+++ b/src/moin/templates/404.html
@@ -5,7 +5,7 @@
abort(404, item_name) # where item_name is the local item name
#}
-{% import "forms.html" as forms %}
+{% import "forms.html" as forms with context %}
{% extends theme("layout.html") %}
{% block content %}
diff --git a/src/moin/templates/atom.html b/src/moin/templates/atom.html
index e0df8d022..b2967a7ce 100644
--- a/src/moin/templates/atom.html
+++ b/src/moin/templates/atom.html
@@ -1,4 +1,4 @@
-{% import "utils.html" as utils %}
+{% import "utils.html" as utils with context %}
{# A few style rules to ensure the content doesn't overflow #}
{% macro atom_style() %}
diff --git a/src/moin/templates/base.html b/src/moin/templates/base.html
index 6aa80cefb..35af05cc3 100644
--- a/src/moin/templates/base.html
+++ b/src/moin/templates/base.html
@@ -8,7 +8,7 @@
{#- This allows child templates passing their header_search macro to the common layout (eg blog).
If there is no header_search macro defined in child templates use the default one. #}
{%- if not header_search %}
- {%- from "utils.html" import header_search %}
+ {%- from "utils.html" import header_search with context %}
{%- endif -%}
diff --git a/src/moin/templates/blog/layout.html b/src/moin/templates/blog/layout.html
index 042eef838..d8b8d3eb7 100644
--- a/src/moin/templates/blog/layout.html
+++ b/src/moin/templates/blog/layout.html
@@ -1,6 +1,6 @@
{% extends theme("show.html") %}
{% import theme("blog/utils.html") as blog_utils with context %}
-{% import "utils.html" as utils %}
+{% import "utils.html" as utils with context %}
{% import "itemviews.html" as itemviews with context %}
{% if blog_item %}
diff --git a/src/moin/templates/blog/modify_entry_meta.html b/src/moin/templates/blog/modify_entry_meta.html
index 58db2036f..947d345e0 100644
--- a/src/moin/templates/blog/modify_entry_meta.html
+++ b/src/moin/templates/blog/modify_entry_meta.html
@@ -1,4 +1,4 @@
-{% import "forms.html" as forms %}
+{% import "forms.html" as forms with context %}
{% macro meta_editor(form, may_admin) %}
Blog entry metadata
diff --git a/src/moin/templates/blog/modify_main_meta.html b/src/moin/templates/blog/modify_main_meta.html
index 5bf0604d7..7e77a66ff 100644
--- a/src/moin/templates/blog/modify_main_meta.html
+++ b/src/moin/templates/blog/modify_main_meta.html
@@ -1,4 +1,4 @@
-{% import "forms.html" as forms %}
+{% import "forms.html" as forms with context %}
{% macro meta_editor(form, may_admin) %}
Blog metadata
diff --git a/src/moin/templates/blog/utils.html b/src/moin/templates/blog/utils.html
index c78968d25..21a9bd584 100644
--- a/src/moin/templates/blog/utils.html
+++ b/src/moin/templates/blog/utils.html
@@ -1,5 +1,5 @@
-{% import "forms.html" as forms %}
-{% import "utils.html" as utils %}
+{% import "forms.html" as forms with context %}
+{% import "utils.html" as utils with context %}
{% macro show_blog_entry(entry_item) %}
{% set summary = entry_item.meta['summary'] %}
diff --git a/src/moin/templates/convert.html b/src/moin/templates/convert.html
index 23218d01a..92993050f 100644
--- a/src/moin/templates/convert.html
+++ b/src/moin/templates/convert.html
@@ -3,7 +3,7 @@
an item to a different text markup language.
#}
-{% import "forms.html" as forms %}
+{% import "forms.html" as forms with context %}
{% extends theme("show.html") %}
{% set title = _("Convert '{item_name}'").format(item_name=item.fqname|shorten_fqname) %}
diff --git a/src/moin/templates/create_new_item.html b/src/moin/templates/create_new_item.html
index 6e72ae8d3..20ec338db 100644
--- a/src/moin/templates/create_new_item.html
+++ b/src/moin/templates/create_new_item.html
@@ -5,7 +5,7 @@
The process rendering this template should have flashed a message describing the error.
#}
-{% import "forms.html" as forms %}
+{% import "forms.html" as forms with context %}
{% extends theme("layout.html") %}
{% set title = _("Create New Item") %}
diff --git a/src/moin/templates/delete.html b/src/moin/templates/delete.html
index 5663cdf13..237de64d0 100644
--- a/src/moin/templates/delete.html
+++ b/src/moin/templates/delete.html
@@ -5,7 +5,7 @@
The revision's meta data and rendered content is displayed for user review.
#}
-{% import "forms.html" as forms %}
+{% import "forms.html" as forms with context %}
{% extends theme("show.html") %}
{% if alias_names %}
diff --git a/src/moin/templates/destroy.html b/src/moin/templates/destroy.html
index eee348b1f..b986b4468 100644
--- a/src/moin/templates/destroy.html
+++ b/src/moin/templates/destroy.html
@@ -7,7 +7,7 @@
is removed from storage so server log is only record of the transaction.
#}
-{% import "forms.html" as forms %}
+{% import "forms.html" as forms with context %}
{% extends theme("show.html") %}
{% if alias_names %}
diff --git a/src/moin/templates/diff_text_atom.html b/src/moin/templates/diff_text_atom.html
index 3d2005527..83eec436c 100644
--- a/src/moin/templates/diff_text_atom.html
+++ b/src/moin/templates/diff_text_atom.html
@@ -1,4 +1,4 @@
-{% import "utils.html" as utils %}
+{% import "utils.html" as utils with context %}
{% for llineno, lcontent, rlineno, rcontent in diffs %}
diff --git a/src/moin/templates/global_history.html b/src/moin/templates/global_history.html
index 8dc0bb5d2..b4bb17225 100644
--- a/src/moin/templates/global_history.html
+++ b/src/moin/templates/global_history.html
@@ -12,7 +12,7 @@
#}
{% extends theme("layout.html") %}
-{% import "utils.html" as utils %}
+{% import "utils.html" as utils with context %}
{# map meta.action to font awesome classes #}
{% set awesome_class = {
diff --git a/src/moin/templates/login.html b/src/moin/templates/login.html
index aa527faed..31694e016 100644
--- a/src/moin/templates/login.html
+++ b/src/moin/templates/login.html
@@ -1,5 +1,5 @@
{% extends theme("layout.html") %}
-{% import "forms.html" as forms %}
+{% import "forms.html" as forms with context %}
{% block content %}