From b43dbdbae3200172f85846137dd576c4de3276bd 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) --- .../qommon/forms/widgets/select--test.html | 1 - tests/test_templates.py | 14 +++++++++++--- wcs/conditions.py | 6 ++++-- wcs/qommon/publisher.py | 4 ++++ wcs/qommon/template.py | 2 ++ wcs/qommon/templates/qommon/forms/widget.html | 1 - 6 files changed, 21 insertions(+), 7 deletions(-) diff --git a/tests/templates/qommon/forms/widgets/select--test.html b/tests/templates/qommon/forms/widgets/select--test.html index 37174963..688f8db5 100644 --- a/tests/templates/qommon/forms/widgets/select--test.html +++ b/tests/templates/qommon/forms/widgets/select--test.html @@ -1,5 +1,4 @@ {% extends "qommon/forms/widget.html" %} -{% load qommon %} {% block widget-control %} diff --git a/tests/test_templates.py b/tests/test_templates.py index fa07b1ab..ab872db5 100644 --- a/tests/test_templates.py +++ b/tests/test_templates.py @@ -58,6 +58,14 @@ def test_template(): tmpl = Template('[if-any foo][foo][endif]') assert tmpl.render({'foo': 'bar'}) == '[if-any foo][foo][endif]' +def test_template_templatetag(): + # check qommon templatetags are always loaded + tmpl = Template('{{ date|parse_datetime|date:"Y" }}') + assert tmpl.render({'date': '2018-06-06'}) == '2018' + + # check other templatetags can be loaded + tmpl = Template('{% load i18n %}{% trans "hello" %}') + assert tmpl.render() == 'hello' def test_template_encoding(): # django @@ -84,7 +92,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 +100,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 %}