Skip to content

Commit 04f7a60

Browse files
committed
refactor: turn jinja filters without args into functions
1 parent 07e1864 commit 04f7a60

File tree

6 files changed

+52
-14
lines changed

6 files changed

+52
-14
lines changed

src/test/unit/web_interface/test_app_jinja_filter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
@pytest.fixture
88
def filter_class(web_frontend):
9-
return FilterClass(web_frontend.app, '', web_frontend.db)
9+
return FilterClass(web_frontend.app, web_frontend.db)
1010

1111

1212
class TestAppShowAnalysis:
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from typing import Any
2+
3+
import pytest
4+
from flask import render_template_string
5+
6+
from version import __VERSION__
7+
8+
9+
def test_auth_is_disabled(web_frontend):
10+
_assert_is_rendered(web_frontend, 'auth_is_enabled', False)
11+
12+
13+
@pytest.mark.frontend_config_overwrite({'authentication': {'enabled': True}})
14+
def test_auth_is_enabled(web_frontend):
15+
_assert_is_rendered(web_frontend, 'auth_is_enabled', True)
16+
17+
18+
def test_get_fact_version(web_frontend):
19+
_assert_is_rendered(web_frontend, 'get_fact_version', __VERSION__)
20+
21+
22+
def _assert_is_rendered(web_frontend, function: str, expected_value: Any):
23+
with web_frontend.app.test_request_context():
24+
template = render_template_string(f'<html><body><div>{{{{ {function}() }}}}</div></body></html>')
25+
assert f'<div>{expected_value}</div>' in template

src/web_interface/components/jinja_filter.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,12 @@ class FilterClass:
2727
This is WEB front end main class
2828
"""
2929

30-
def __init__(self, app, program_version, db: FrontendDatabase, **_):
31-
self._program_version = program_version
30+
def __init__(self, app, db: FrontendDatabase, **_):
3231
self._app = app
3332
self.db = db
3433

3534
self._setup_filters()
3635

37-
def _filter_print_program_version(self, *_):
38-
return f'{self._program_version}'
39-
4036
def _filter_replace_uid_with_file_name(self, input_data):
4137
tmp = input_data.__str__()
4238
uid_list = flt.get_all_uids_in_string(tmp)
@@ -117,9 +113,6 @@ def _virtual_path_element_to_span(hid_element: str, uid: str, root_uid: str, cur
117113
'</span>'
118114
)
119115

120-
def check_auth(self, _):
121-
return config.frontend.authentication.enabled
122-
123116
def data_to_chart_limited(self, data, limit: int | None = None, color_list=None):
124117
limit = self._get_chart_element_count() if limit is None else limit
125118
try:
@@ -150,7 +143,6 @@ def _setup_filters(self):
150143
{
151144
'all_items_equal': lambda data: len({str(value) for value in data.values()}) == 1,
152145
'as_ascii_table': flt.as_ascii_table,
153-
'auth_enabled': self.check_auth,
154146
'base64_encode': flt.encode_base64_filter,
155147
'bytes_to_str': flt.bytes_to_str_filter,
156148
'data_to_chart': self.data_to_chart,
@@ -189,7 +181,6 @@ def _setup_filters(self):
189181
'nice_virtual_path_list': self._nice_virtual_path_list,
190182
'number_format': flt.byte_number_filter,
191183
'octal_to_readable': flt.octal_to_readable,
192-
'print_program_version': self._filter_print_program_version,
193184
'regex_meta': flt.comment_out_regex_meta_chars,
194185
'remaining_time': elapsed_time,
195186
'render_analysis_tags': flt.render_analysis_tags,

src/web_interface/frontend_main.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from __future__ import annotations
22

33
import logging
4+
from inspect import getmembers, isfunction
45

56
from intercom.front_end_binding import InterComFrontEndBinding
67
from storage.redis_status_interface import RedisStatusInterface
78
from version import __VERSION__
9+
from web_interface import jinja_functions
810
from web_interface.app import create_app
911
from web_interface.components.ajax_routes import AjaxRoutes
1012
from web_interface.components.analysis_routes import AnalysisRoutes
@@ -29,6 +31,7 @@ def __init__(self, db: FrontendDatabase | None = None, intercom=None, status_int
2931
self.status_interface = RedisStatusInterface() if status_interface is None else status_interface
3032

3133
self._setup_app()
34+
self._register_jinja_functions()
3235
logging.info('Web front end online')
3336

3437
def _setup_app(self):
@@ -47,4 +50,13 @@ def _setup_app(self):
4750

4851
rest_base = RestBase(**base_args)
4952
PluginRoutes(**base_args, api=rest_base.api)
50-
FilterClass(self.app, self.program_version, self.db)
53+
FilterClass(self.app, self.db)
54+
55+
def _register_jinja_functions(self):
56+
# add functions from the module to the globals in jinja so that the functions can be called from templates
57+
for function_name, function in getmembers(jinja_functions, isfunction):
58+
if (
59+
(not function_name.startswith('_')) # we assume all function beginning with "_" are helper functions
60+
and (function.__module__ == jinja_functions.__name__) # only functions from the module, no imports
61+
):
62+
self.app.jinja_env.globals.update({function_name: function})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import config
2+
from version import __VERSION__
3+
4+
5+
def auth_is_enabled() -> bool:
6+
return config.frontend.authentication.enabled
7+
8+
9+
def get_fact_version():
10+
return __VERSION__

src/web_interface/templates/base.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
{{ navbar_dropdown_item("/admin/missing_analyses", "search", "Find Missing Analyses") }}
114114
{{ navbar_dropdown_item("/admin/logs", "exclamation-circle", "Logs") }}
115115
{% endcall %}
116-
{% if ("" | auth_enabled) %}
116+
{% if auth_is_enabled() %}
117117
{% if current_user.is_authenticated %}
118118
{% call navbar_dropdown_menu(current_user.email | truncate(12), "user") %}
119119
{{ navbar_dropdown_item("/user_profile", "user-cog", "Profile") }}
@@ -177,7 +177,7 @@
177177

178178
<div class="row justify-content-center mb-2">
179179
<div class="col-md-4 text-center">
180-
powered by <a href="https://fkie-cad.github.io/FACT_core/">FACT {{ "" | print_program_version }}</a><br/>
180+
powered by <a href="https://fkie-cad.github.io/FACT_core/">FACT {{ get_fact_version() }}</a><br/>
181181
&copy; <a href="http://www.fkie.fraunhofer.de">Fraunhofer FKIE</a> 2015-2026
182182
</div>
183183
</div>

0 commit comments

Comments
 (0)