From 47d98ac5339534f18bce3ed60f51ddb0ac3bae96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Tue, 5 Jun 2018 18:03:02 +0200 Subject: [PATCH] general: load some templatetags libraries by default (#24296) --- tests/test_templates.py | 6 +++--- wcs/conditions.py | 6 ++++-- wcs/qommon/publisher.py | 4 ++++ wcs/qommon/template.py | 2 ++ wcs/qommon/templates/qommon/forms/widget.html | 1 - 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/tests/test_templates.py b/tests/test_templates.py index fa07b1ab..44c0c2e7 100644 --- a/tests/test_templates.py +++ b/tests/test_templates.py @@ -84,7 +84,7 @@ def test_template_encoding(): def test_datetime_templatetags(): - tmpl = Template('{% load qommon %}{{ plop|parse_datetime|date:"d i" }}') + tmpl = Template('{{ plop|parse_datetime|date:"d i" }}') assert tmpl.render({'plop': '2017-12-21 10:32'}) == '21 32' assert tmpl.render({'plop': '2017-12-21 10:32:42'}) == '21 32' assert tmpl.render({'plop': '21/12/2017 10:32'}) == '21 32' @@ -92,11 +92,11 @@ def test_datetime_templatetags(): assert tmpl.render({'plop': '21/12/2017 10h32'}) == '21 32' assert tmpl.render({'plop': 'x'}) == '' - tmpl = Template('{% load qommon %}{{ plop|parse_date|date:"d" }}') + tmpl = Template('{{ plop|parse_date|date:"d" }}') assert tmpl.render({'plop': '2017-12-21'}) == '21' assert tmpl.render({'plop': '21/12/2017'}) == '21' assert tmpl.render({'plop': 'x'}) == '' - tmpl = Template('{% load qommon %}{{ plop|parse_time|date:"H i" }}') + tmpl = Template('{{ plop|parse_time|date:"H i" }}') assert tmpl.render({'plop': '10:32'}) == '10 32' assert tmpl.render({'plop': 'x'}) == '' diff --git a/wcs/conditions.py b/wcs/conditions.py index 9e80e891..0a9d5761 100644 --- a/wcs/conditions.py +++ b/wcs/conditions.py @@ -61,7 +61,8 @@ class Condition(object): return eval(self.value, global_variables, local_variables) def evaluate_django(self, local_variables): - template = Template('{%% load qommon %%}{%% if %s %%}OK{%% endif %%}' % self.value) + template = Template('{%% load %s %%}{%% if %s %%}OK{%% endif %%}' % ( + get_publisher().get_default_templatetags_libraries(), self.value)) context = Context(local_variables) return template.render(context) == 'OK' @@ -82,6 +83,7 @@ class Condition(object): def validate_django(self): try: - Template('{%% load qommon %%}{%% if %s %%}OK{%% endif %%}' % self.value) + Template('{%% load %s %%}{%% if %s %%}OK{%% endif %%}' % ( + get_publisher().get_default_templatetags_libraries(), self.value)) except TemplateSyntaxError as e: raise ValidationError(_('syntax error: %s') % e) diff --git a/wcs/qommon/publisher.py b/wcs/qommon/publisher.py index d45f273f..075f9d9b 100644 --- a/wcs/qommon/publisher.py +++ b/wcs/qommon/publisher.py @@ -945,6 +945,10 @@ class QommonPublisher(Publisher, object): default_position = '50.84;4.36' return default_position + def get_default_templatetags_libraries(self): + libraries = self.get_site_option('default_templatetags_libraries') or '' + return 'qommon %s' % libraries + def get_map_attributes(self): attrs = {} attrs['data-def-lat'], attrs['data-def-lng'] = self.get_default_position().split(';') diff --git a/wcs/qommon/template.py b/wcs/qommon/template.py index 6bf7bdf5..d150ad80 100644 --- a/wcs/qommon/template.py +++ b/wcs/qommon/template.py @@ -465,6 +465,8 @@ class Template(object): self.render = self.django_render if autoescape is False: value = '{%% autoescape off %%}%s{%% endautoescape %%}' % value + value = '{%% load %s %%}%s' % ( + get_publisher().get_default_templatetags_libraries(), value) try: self.template = engines['django'].from_string(value) except DjangoTemplateSyntaxError as e: diff --git a/wcs/qommon/templates/qommon/forms/widget.html b/wcs/qommon/templates/qommon/forms/widget.html index e205ad49..fd4c9bb9 100644 --- a/wcs/qommon/templates/qommon/forms/widget.html +++ b/wcs/qommon/templates/qommon/forms/widget.html @@ -1,4 +1,3 @@ -{% load qommon %}