From d3980b2fd54cc2b6c25eb905212a5591e970d504 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Wed, 6 Jul 2022 08:36:49 +0200 Subject: [PATCH] templatetags: allow list filter to create list from elements (#67054) --- tests/test_formdata.py | 4 ++-- tests/test_templates.py | 10 ++++++++++ wcs/qommon/templatetags/qommon.py | 19 +++++++++++++++++-- 3 files changed, 29 insertions(+), 4 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..b596d07fb 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_argument(): + tmpl = Template('{{ foo|list: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..9d475a54e 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 @@ -927,9 +928,23 @@ def last(value): @register.filter(name='list') -def list_(value): +def list_(value, other=Ellipsis): # turn a generator into a list - return list(unlazy(value)) + result = [] + + def add_to_result(value): + if isinstance(value, collections.abc.Iterable) and not isinstance( + value, (collections.abc.Mapping, str, bytes) + ): + for elt in unlazy(value): + result.append(elt) + else: + result.append(unlazy(value)) + + add_to_result(value) + if other is not Ellipsis: + add_to_result(other) + return result @register.filter(name='qrcode') -- 2.35.1