Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 43 additions & 26 deletions src/moin/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/moin/apps/admin/templates/admin/group_acl_report.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#}

{% extends theme("layout.html") %}
{% import "utils.html" as utils %}
{% import "utils.html" as utils with context %}
{% block content %}
<h1>{{ _("Group ACL Report") }}</h1>
<h2>{{ _("Group Name") }}: {{ group_name }}</h2>
Expand Down
2 changes: 1 addition & 1 deletion src/moin/apps/admin/templates/admin/groupbrowser.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends theme("layout.html") %}
{% import "utils.html" as utils %}
{% import "utils.html" as utils with context %}
{% block content %}
<h1>{{ _("Groups") }}</h1>
<table id="moin-group-browser" class="zebra moin-sortable" data-sortlist="[[0,0]]">
Expand Down
2 changes: 1 addition & 1 deletion src/moin/apps/admin/templates/admin/register_new_user.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#}

{% extends theme("layout.html") %}
{% import "forms.html" as forms %}
{% import "forms.html" as forms with context %}

{% block content %}
<h1>{{ _("Register New User") }}</h1>
Expand Down
2 changes: 1 addition & 1 deletion src/moin/apps/admin/templates/admin/trash.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends theme("layout.html") %}
{% import "utils.html" as utils %}
{% import "utils.html" as utils with context %}
{% block content %}
{% if headline %}
<h1>{{ headline }}</h1>
Expand Down
2 changes: 1 addition & 1 deletion src/moin/apps/admin/templates/admin/user_acl_report.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#}

{% extends theme("layout.html") %}
{% import "utils.html" as utils %}
{% import "utils.html" as utils with context %}
{% block content %}
<h1>{{ _("User ACL Report") }}</h1>
<h2>{{ _("User Names") }}: {{ user_names|join(', ') }}</h2>
Expand Down
2 changes: 1 addition & 1 deletion src/moin/apps/admin/templates/user/highlighterhelp.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% import "utils.html" as utils %}
{% import "utils.html" as utils with context %}
{% extends theme("layout.html") %}
{% block content %}
<h1>{{ _("Highlighters") }}</h1>
Expand Down
2 changes: 1 addition & 1 deletion src/moin/apps/admin/templates/user/interwikihelp.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% import "utils.html" as utils %}
{% import "utils.html" as utils with context %}
{% extends theme("layout.html") %}
{% block content %}
<h1>{{ _("InterWiki Names") }}</h1>
Expand Down
2 changes: 1 addition & 1 deletion src/moin/apps/admin/templates/user/itemsize.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% import "utils.html" as utils %}
{% import "utils.html" as utils with context %}
{% extends theme("layout.html") %}
{% block content %}
<h1>{{ _("Item Sizes (last revision)") }}</h1>
Expand Down
2 changes: 1 addition & 1 deletion src/moin/templates/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
2 changes: 1 addition & 1 deletion src/moin/templates/atom.html
Original file line number Diff line number Diff line change
@@ -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() %}
Expand Down
2 changes: 1 addition & 1 deletion src/moin/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}

<html>
Expand Down
2 changes: 1 addition & 1 deletion src/moin/templates/blog/layout.html
Original file line number Diff line number Diff line change
@@ -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 %}
Expand Down
2 changes: 1 addition & 1 deletion src/moin/templates/blog/modify_entry_meta.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% import "forms.html" as forms %}
{% import "forms.html" as forms with context %}

{% macro meta_editor(form, may_admin) %}
<h2>Blog entry metadata</h2>
Expand Down
2 changes: 1 addition & 1 deletion src/moin/templates/blog/modify_main_meta.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% import "forms.html" as forms %}
{% import "forms.html" as forms with context %}

{% macro meta_editor(form, may_admin) %}
<h2>Blog metadata</h2>
Expand Down
4 changes: 2 additions & 2 deletions src/moin/templates/blog/utils.html
Original file line number Diff line number Diff line change
@@ -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'] %}
Expand Down
2 changes: 1 addition & 1 deletion src/moin/templates/convert.html
Original file line number Diff line number Diff line change
Expand Up @@ -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) %}
Expand Down
2 changes: 1 addition & 1 deletion src/moin/templates/create_new_item.html
Original file line number Diff line number Diff line change
Expand Up @@ -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") %}
Expand Down
2 changes: 1 addition & 1 deletion src/moin/templates/delete.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
2 changes: 1 addition & 1 deletion src/moin/templates/destroy.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
2 changes: 1 addition & 1 deletion src/moin/templates/diff_text_atom.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% import "utils.html" as utils %}
{% import "utils.html" as utils with context %}

<table class="moin-diff" style="width: 100%;">
{% for llineno, lcontent, rlineno, rcontent in diffs %}
Expand Down
2 changes: 1 addition & 1 deletion src/moin/templates/global_history.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion src/moin/templates/login.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends theme("layout.html") %}
{% import "forms.html" as forms %}
{% import "forms.html" as forms with context %}

{% block content %}
<div class="moin-form">
Expand Down
4 changes: 2 additions & 2 deletions src/moin/templates/lookup.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends theme("layout.html") %}
{% import "utils.html" as utils %}
{% import "forms.html" as forms %}
{% import "utils.html" as utils with context %}
{% import "forms.html" as forms with context %}
{% block content %}
{% if results is not defined %}
<h1>{{ _("Lookup") }}</h1>
Expand Down
2 changes: 1 addition & 1 deletion src/moin/templates/lostpass.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends theme("layout.html") %}
{% import "forms.html" as forms %}
{% import "forms.html" as forms with context %}

{% block content %}
<h1>{{ _("Lost Password") }}</h1>
Expand Down
4 changes: 2 additions & 2 deletions src/moin/templates/modify.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* select edit "from scratch" or "from template"
#}

{% import "forms.html" as forms %}
{% import "utils.html" as utils %}
{% import "forms.html" as forms with context %}
{% import "utils.html" as utils with context %}

{% extends theme("show.html") %}

Expand Down
2 changes: 1 addition & 1 deletion src/moin/templates/modify_binary.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
As binary items cannot be edited by moin, the only choice given is to upload a file.
#}

{% import "forms.html" as forms %}
{% import "forms.html" as forms with context %}

{% macro data_editor(form, item_name) %}
<dl>
Expand Down
2 changes: 1 addition & 1 deletion src/moin/templates/modify_meta.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
The macro defines a portion of a form enabling a user to modify selected meta data.
#}

{% import "forms.html" as forms %}
{% import "forms.html" as forms with context %}

{% macro meta_editor(form, may_admin) %}
<dl>
Expand Down
2 changes: 1 addition & 1 deletion src/moin/templates/modify_text.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
value and textarea scroll bars will be used as needed.
#}

{% from "modify_binary.html" import data_editor as base_editor %}
{% from "modify_binary.html" import data_editor as base_editor with context %}

{% macro data_editor(form, item_name) %}
{% set textarea_rows = '1' if edit_rows == '0' else edit_rows %}
Expand Down
2 changes: 1 addition & 1 deletion src/moin/templates/mychanges.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#}

{% extends theme("layout.html") %}
{% import "utils.html" as utils %}
{% import "utils.html" as utils with context %}

{% block content %}
<h1>{{ _('My Changes') }}</h1>
Expand Down
2 changes: 1 addition & 1 deletion src/moin/templates/recoverpass.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends theme("layout.html") %}
{% import "forms.html" as forms %}
{% import "forms.html" as forms with context %}

{% block content %}
<h1>{{ _("Recover Password") }}</h1>
Expand Down
2 changes: 1 addition & 1 deletion src/moin/templates/register.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends theme("layout.html") %}
{% import "forms.html" as forms %}
{% import "forms.html" as forms with context %}

{% block content %}
<h1>{{ _("Create Account") }}</h1>
Expand Down
2 changes: 1 addition & 1 deletion src/moin/templates/rename.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
The item'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") %}

{% set title = _("Rename '{item_name}'").format(item_name=item.fqname|shorten_fqname) %}
Expand Down
4 changes: 2 additions & 2 deletions src/moin/templates/revert.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
The revision's meta data and rendered content is displayed for user review.
#}

{% import "forms.html" as forms %}
{% import "utils.html" as utils %}
{% import "forms.html" as forms with context %}
{% import "utils.html" as utils with context %}
{% extends theme("layout.html") %}

{% block content %}
Expand Down
Loading