0001-misc-add-a-decimal-template-tag-27699.patch
tests/test_templates.py | ||
---|---|---|
4 | 4 | |
5 | 5 |
from quixote import cleanup |
6 | 6 |
from qommon.template import Template, TemplateError |
7 |
from wcs.conditions import Condition |
|
7 | 8 | |
8 | 9 |
from utilities import create_temporary_pub, clean_temporary_pub |
9 | 10 | |
... | ... | |
141 | 142 |
def test_variable_unicode_error_handling(): |
142 | 143 |
tmpl = Template('{{ form_var_éléphant }}') |
143 | 144 |
assert tmpl.render() == '' |
145 | ||
146 |
def test_decimal_templatetag(): |
|
147 |
tmpl = Template('{{ plop|decimal }}') |
|
148 |
assert tmpl.render({'plop': 'toto'}) == '' |
|
149 |
assert tmpl.render({'plop': '3.14'}) == '3.14' |
|
150 |
assert tmpl.render({'plop': '3,14'}) == '3.14' |
|
151 |
assert tmpl.render({'plop': 3.14}) == '3.14' |
|
152 |
assert tmpl.render({'plop': 12345.678}) == '12345.678' |
|
153 |
assert tmpl.render({'plop': None}) == '' |
|
154 | ||
155 |
tmpl = Template('{% if plop|decimal > 2 %}hello{% endif %}') |
|
156 |
assert tmpl.render({'plop': 3}) == 'hello' |
|
157 |
assert tmpl.render({'plop': 1}) == '' |
|
158 |
assert tmpl.render({'plop': None}) == 'hello' # will be str > int, always True. |
wcs/qommon/templatetags/qommon.py | ||
---|---|---|
15 | 15 |
# along with this program; if not, see <http://www.gnu.org/licenses/>. |
16 | 16 | |
17 | 17 |
import datetime |
18 |
from decimal import Decimal |
|
18 | 19 | |
19 | 20 |
from django import template |
20 | 21 |
from django.template import defaultfilters |
... | ... | |
75 | 76 |
value = parse_date(value) |
76 | 77 |
return defaultfilters.date(value, arg=arg) |
77 | 78 | |
79 |
@register.filter(is_safe=True) |
|
80 |
def decimal(value): |
|
81 |
if not value: |
|
82 |
return '' |
|
83 |
try: |
|
84 |
if isinstance(value, basestring): |
|
85 |
# replace , by . for French users comfort |
|
86 |
value = value.replace(',', '.') |
|
87 |
return Decimal(value).quantize(Decimal('1.0000')).normalize() |
|
88 |
except ArithmeticError: |
|
89 |
return '' |
|
90 | ||
78 | 91 |
@register.simple_tag |
79 | 92 |
def standard_text(text_id): |
80 | 93 |
return mark_safe(TextsDirectory.get_html_text(str(text_id))) |
81 |
- |