From ccc5fb1ddcd702bcd2d09dc789d1a0ce8743a861 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Wed, 6 Jul 2022 08:36:49 +0200 Subject: [PATCH] templatetags: user list/add filters to create list from simple values (#67054) --- tests/test_formdata.py | 4 ++-- tests/test_templates.py | 10 ++++++++++ wcs/qommon/templatetags/qommon.py | 13 ++++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/tests/test_formdata.py b/tests/test_formdata.py index 8f89f6685..f0b22762b 100644 --- a/tests/test_formdata.py +++ b/tests/test_formdata.py @@ -2662,7 +2662,7 @@ def test_form_digest_error(pub): formdef.name = 'foobar' formdef.url_name = 'foobar' formdef.fields = [fields.DateField(id='0', label='date', varname='date')] - formdef.digest_templates = {'default': 'plop {{ form_var_date|list }} plop'} + formdef.digest_templates = {'default': 'plop {{ form_var_date|reproj:"coin"}} plop'} formdef.store() formdata = formdef.data_class()() @@ -2677,7 +2677,7 @@ def test_form_digest_error(pub): formdef.digest_templates = { 'default': 'plop plop', - 'custom-view:foobar': 'plop {{ form_var_date|list }} plop', + 'custom-view:foobar': 'plop {{ form_var_date|reproj:"coin" }} plop', } formdef.store() diff --git a/tests/test_templates.py b/tests/test_templates.py index b08c94668..98c0750f6 100644 --- a/tests/test_templates.py +++ b/tests/test_templates.py @@ -1295,6 +1295,16 @@ def test_convert_as_list(): assert tmpl.render({'foo': list_range}) == '0' +def test_convert_as_list_with_add(): + tmpl = Template('{{ foo|list|add:bar|join:", " }}') + assert tmpl.render({'foo': [1, 2], 'bar': ['a', 'b']}) == '1, 2, a, b' + assert tmpl.render({'foo': [1, 2], 'bar': 'ab'}) == '1, 2, ab' + assert tmpl.render({'foo': 12, 'bar': ['a', 'b']}) == '12, a, b' + assert tmpl.render({'foo': 12, 'bar': 'ab'}) == '12, ab' + assert tmpl.render({'foo': [1, 2], 'bar': {'a': 'b'}}) == '1, 2, {'a': 'b'}' + assert tmpl.render({'foo': {'a': 'b'}, 'bar': ['a', 'b']}) == '{'a': 'b'}, a, b' + + def test_adjust_to_week_monday(pub): t = Template('{{ value|adjust_to_week_monday }}') assert t.render({'value': '2021-06-13'}) == '2021-06-07' diff --git a/wcs/qommon/templatetags/qommon.py b/wcs/qommon/templatetags/qommon.py index bf3db989e..787ef5f6d 100644 --- a/wcs/qommon/templatetags/qommon.py +++ b/wcs/qommon/templatetags/qommon.py @@ -14,6 +14,7 @@ # You should have received a copy of the GNU General Public License # along with this program; if not, see . +import collections.abc import datetime import hashlib import io @@ -569,6 +570,10 @@ def add(term1, term2): except (ArithmeticError, TypeError, ValueError): pass + # append to term1 if term1 is a list and not term2 + if isinstance(term1, list) and not isinstance(term2, list): + return list(term1) + [term2] + # fallback to django add filter return defaultfilters.add(unlazy(term1), unlazy(term2)) @@ -929,7 +934,13 @@ def last(value): @register.filter(name='list') def list_(value): # turn a generator into a list - return list(unlazy(value)) + real_value = unlazy(value) + if isinstance(real_value, collections.abc.Iterable) and not isinstance( + real_value, (collections.abc.Mapping, str) + ): + return list(real_value) + else: + return [real_value] @register.filter(name='qrcode') -- 2.35.1