From b6aa485b24f0688888e407a72a71490909af7e40 Mon Sep 17 00:00:00 2001 From: Sistason <c3infra@sistason.de> Date: Sat, 17 Apr 2021 21:20:08 +0200 Subject: [PATCH] refactored register_navbar() to not use global variables anymore ... which did break in tests --- uffd/invite/views.py | 1 - uffd/navbar.py | 54 ++++++++++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/uffd/invite/views.py b/uffd/invite/views.py index d6500a70..a0a00e86 100644 --- a/uffd/invite/views.py +++ b/uffd/invite/views.py @@ -15,7 +15,6 @@ from uffd.navbar import register_navbar from uffd.ratelimit import host_ratelimit, format_delay from uffd.signup.views import signup_ratelimit -raise ValueError bp = Blueprint('invite', __name__, template_folder='templates', url_prefix='/invite/') diff --git a/uffd/navbar.py b/uffd/navbar.py index dc46b763..f5e244be 100644 --- a/uffd/navbar.py +++ b/uffd/navbar.py @@ -1,9 +1,6 @@ -# pylint: disable=invalid-name -navbarList = [] -# pylint: enable=invalid-name - def setup_navbar(app): - app.jinja_env.globals['getnavbar'] = lambda: [n for n in navbarList if n['visible']()] + app.navbarList = [] + app.jinja_env.globals['getnavbar'] = lambda: [n for n in app.navbarList if n['visible']()] # iconlib can be 'bootstrap' # ( see: http://getbootstrap.com/components/#glyphicons ) @@ -12,22 +9,35 @@ def setup_navbar(app): # visible is a function that returns "True" if this icon should be visible in the calling context def register_navbar(name, iconlib='fa', icon=None, group=None, endpoint=None, blueprint=None, visible=None): def wrapper(func): - urlendpoint = endpoint - if not endpoint: - # pylint: disable=protected-access - if blueprint: - urlendpoint = "{}.{}".format(blueprint.name, func.__name__) - else: - urlendpoint = func.__name_ + def deferred_call(state): + urlendpoint = endpoint + if not endpoint: + # pylint: disable=protected-access + if blueprint: + urlendpoint = "{}.{}".format(blueprint.name, func.__name__) + else: + urlendpoint = func.__name_ # pylint: enable=protected-access - item = {} - item['iconlib'] = iconlib - item['icon'] = icon - item['group'] = group - item['endpoint'] = urlendpoint - item['name'] = name - item['blueprint'] = blueprint - item['visible'] = visible or (lambda: True) - navbarList.append(item) + item = {} + item['iconlib'] = iconlib + item['icon'] = icon + item['group'] = group + item['endpoint'] = urlendpoint + item['name'] = name + item['blueprint'] = blueprint + item['visible'] = visible or (lambda: True) + + state.app.navbarList.append(item) + + if blueprint: + blueprint.record_once(deferred_call) + else: + class StateMock: + def __init__(self, app): + self.app = app + from flask import current_app + deferred_call(StateMock(current_app)) + return func - return wrapper + + return wrapper \ No newline at end of file -- GitLab