templatetags: manage lazy variable on decimal filter (#30793)
This commit is contained in:
parent
3e763b999a
commit
144fad9847
|
@ -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'
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue