add decimal templatetag (#40599)
This commit is contained in:
parent
fba76ea61a
commit
75957c0d23
|
@ -17,6 +17,7 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
import datetime
|
||||
from decimal import Decimal
|
||||
import json
|
||||
import time
|
||||
|
||||
|
@ -415,3 +416,17 @@ def age_in_months(value, today=None):
|
|||
except ValueError:
|
||||
return ''
|
||||
return years * 12 + months
|
||||
|
||||
@register.filter(is_safe=False)
|
||||
def decimal(value, arg=None):
|
||||
if not isinstance(value, Decimal):
|
||||
if isinstance(value, six.string_types):
|
||||
# replace , by . for French users comfort
|
||||
value = value.replace(',', '.')
|
||||
try:
|
||||
value = Decimal(value).quantize(Decimal('1.0000')).normalize()
|
||||
except (ArithmeticError, TypeError):
|
||||
value = Decimal(0)
|
||||
if arg is None:
|
||||
return value
|
||||
return defaultfilters.floatformat(value, arg=arg)
|
||||
|
|
|
@ -463,3 +463,44 @@ def test_age_in():
|
|||
):
|
||||
tmpl = Template('{%% if %s %%}Good{%% endif %%}' % condition_value)
|
||||
assert tmpl.render(Context(context)) == 'Good'
|
||||
|
||||
|
||||
def test_decimal_templatetag():
|
||||
tmpl = Template('{{ plop|decimal }}')
|
||||
assert tmpl.render(Context({'plop': 'toto'})) == '0'
|
||||
assert tmpl.render(Context({'plop': '3.14'})) == '3.14'
|
||||
assert tmpl.render(Context({'plop': '3,14'})) == '3.14'
|
||||
assert tmpl.render(Context({'plop': 3.14})) == '3.14'
|
||||
assert tmpl.render(Context({'plop': 12345.678})) == '12345.678'
|
||||
assert tmpl.render(Context({'plop': None})) == '0'
|
||||
assert tmpl.render(Context({'plop': 0})) == '0'
|
||||
|
||||
tmpl = Template('{{ plop|decimal:3 }}')
|
||||
assert tmpl.render(Context({'plop': '3.14'})) == '3.140'
|
||||
assert tmpl.render(Context({'plop': None})) == '0.000'
|
||||
tmpl = Template('{{ plop|decimal:"3" }}')
|
||||
assert tmpl.render(Context({'plop': '3.14'})) == '3.140'
|
||||
assert tmpl.render(Context({'plop': None})) == '0.000'
|
||||
|
||||
tmpl = Template('{% if plop|decimal > 2 %}hello{% endif %}')
|
||||
assert tmpl.render(Context({'plop': 3})) == 'hello'
|
||||
assert tmpl.render(Context({'plop': '3'})) == 'hello'
|
||||
assert tmpl.render(Context({'plop': 2.001})) == 'hello'
|
||||
assert tmpl.render(Context({'plop': '2.001'})) == 'hello'
|
||||
assert tmpl.render(Context({'plop': 1})) == ''
|
||||
assert tmpl.render(Context({'plop': 1.99})) == ''
|
||||
assert tmpl.render(Context({'plop': '1.99'})) == ''
|
||||
assert tmpl.render(Context({'plop': 'x'})) == ''
|
||||
assert tmpl.render(Context({'plop': None})) == ''
|
||||
assert tmpl.render(Context({'plop': 0})) == ''
|
||||
|
||||
tmpl = Template('{% if "3"|decimal == 3 %}hello{% endif %}')
|
||||
assert tmpl.render(Context()) == 'hello'
|
||||
tmpl = Template('{% if "3"|decimal == 3.0 %}hello{% endif %}')
|
||||
assert tmpl.render(Context()) == 'hello'
|
||||
tmpl = Template('{% if 3|decimal == 3 %}hello{% endif %}')
|
||||
assert tmpl.render(Context()) == 'hello'
|
||||
tmpl = Template('{% if 3.0|decimal == 3 %}hello{% endif %}')
|
||||
assert tmpl.render(Context()) == 'hello'
|
||||
tmpl = Template('{% if 3|decimal|decimal == 3 %}hello{% endif %}')
|
||||
assert tmpl.render(Context()) == 'hello'
|
||||
|
|
Loading…
Reference in New Issue