From 144fad9847566a959041ef5ac2f221cac40564e3 Mon Sep 17 00:00:00 2001 From: Nicolas ROCHE Date: Thu, 21 Feb 2019 15:59:30 +0100 Subject: [PATCH] templatetags: manage lazy variable on decimal filter (#30793) --- tests/test_formdata.py | 28 ++++++++++++++++++++++++++++ wcs/qommon/templatetags/qommon.py | 4 ++++ 2 files changed, 32 insertions(+) diff --git a/tests/test_formdata.py b/tests/test_formdata.py index 7cdaf8c91..3bb828a7f 100644 --- a/tests/test_formdata.py +++ b/tests/test_formdata.py @@ -1308,3 +1308,31 @@ def test_form_digest_date(pub): formdata.data = {'0': time.strptime('2015-05-12', '%Y-%m-%d')} formdata.store() assert formdef.data_class().get(formdata.id).digest == 'plop plop' + +def test_lazy_formdata_decimal_filter(pub): + formdef = FormDef() + formdef.name = 'foobar' + formdef.url_name = 'foobar' + formdef.fields = [ + fields.StringField(id='0', label='value', varname='value'), + fields.StringField(id='1', label='arg', varname='arg') + ] + formdef.store() + formdata = formdef.data_class()() + formdata.data = {'0': '3.14', '1': '3'} + formdata.store() + pub.substitutions.feed(formdata) + for mode in (None, 'lazy'): + context = pub.substitutions.get_context_variables(mode=mode) + + tmpl = Template('{{ form_var_value|decimal }}') + assert tmpl.render(context) == '3.14' + + tmpl = Template('{{ form_var_value|decimal:1 }}') + assert tmpl.render(context) == '3.1' + + tmpl = Template('{{ form_var_value|decimal:form_var_arg }}') + assert tmpl.render(context) == '3.140' + + tmpl = Template('{{ 4.12|decimal:form_var_arg }}') + assert tmpl.render(context) == '4.120' diff --git a/wcs/qommon/templatetags/qommon.py b/wcs/qommon/templatetags/qommon.py index 0df9d6921..ceafe7fb9 100644 --- a/wcs/qommon/templatetags/qommon.py +++ b/wcs/qommon/templatetags/qommon.py @@ -122,10 +122,14 @@ def parse_decimal(value): @register.filter(is_safe=False) def decimal(value, arg=None): + if hasattr(value, 'get_value'): + value = value.get_value() # unlazy if not isinstance(value, Decimal): value = parse_decimal(value) if value is None: return '' + if hasattr(arg, 'get_value'): + arg = arg.get_value() # unlazy if arg is None: return value return defaultfilters.floatformat(value, arg=arg)