Projet

Général

Profil

0001-add-decimal-templatetag-40599.patch

Thomas Noël, 10 mars 2020 09:51

Télécharger (3,7 ko)

Voir les différences:

Subject: [PATCH] add decimal templatetag (#40599)

 combo/public/templatetags/combo.py | 15 +++++++++++
 tests/test_public_templatetags.py  | 41 ++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+)
combo/public/templatetags/combo.py
17 17
from __future__ import absolute_import
18 18

  
19 19
import datetime
20
from decimal import Decimal
20 21
import json
21 22
import time
22 23

  
......
415 416
    except ValueError:
416 417
        return ''
417 418
    return years * 12 + months
419

  
420
@register.filter(is_safe=False)
421
def decimal(value, arg=None):
422
    if not isinstance(value, Decimal):
423
        if isinstance(value, six.string_types):
424
            # replace , by . for French users comfort
425
            value = value.replace(',', '.')
426
        try:
427
            value = Decimal(value).quantize(Decimal('1.0000')).normalize()
428
        except (ArithmeticError, TypeError):
429
            value = Decimal(0)
430
    if arg is None:
431
        return value
432
    return defaultfilters.floatformat(value, arg=arg)
tests/test_public_templatetags.py
463 463
            ):
464 464
        tmpl = Template('{%% if %s %%}Good{%% endif %%}' % condition_value)
465 465
        assert tmpl.render(Context(context)) == 'Good'
466

  
467

  
468
def test_decimal_templatetag():
469
    tmpl = Template('{{ plop|decimal }}')
470
    assert tmpl.render(Context({'plop': 'toto'})) == '0'
471
    assert tmpl.render(Context({'plop': '3.14'})) == '3.14'
472
    assert tmpl.render(Context({'plop': '3,14'})) == '3.14'
473
    assert tmpl.render(Context({'plop': 3.14})) == '3.14'
474
    assert tmpl.render(Context({'plop': 12345.678})) == '12345.678'
475
    assert tmpl.render(Context({'plop': None})) == '0'
476
    assert tmpl.render(Context({'plop': 0})) == '0'
477

  
478
    tmpl = Template('{{ plop|decimal:3 }}')
479
    assert tmpl.render(Context({'plop': '3.14'})) == '3.140'
480
    assert tmpl.render(Context({'plop': None})) == '0.000'
481
    tmpl = Template('{{ plop|decimal:"3" }}')
482
    assert tmpl.render(Context({'plop': '3.14'})) == '3.140'
483
    assert tmpl.render(Context({'plop': None})) == '0.000'
484

  
485
    tmpl = Template('{% if plop|decimal > 2 %}hello{% endif %}')
486
    assert tmpl.render(Context({'plop': 3})) == 'hello'
487
    assert tmpl.render(Context({'plop': '3'})) == 'hello'
488
    assert tmpl.render(Context({'plop': 2.001})) == 'hello'
489
    assert tmpl.render(Context({'plop': '2.001'})) == 'hello'
490
    assert tmpl.render(Context({'plop': 1})) == ''
491
    assert tmpl.render(Context({'plop': 1.99})) == ''
492
    assert tmpl.render(Context({'plop': '1.99'})) == ''
493
    assert tmpl.render(Context({'plop': 'x'})) == ''
494
    assert tmpl.render(Context({'plop': None})) == ''
495
    assert tmpl.render(Context({'plop': 0})) == ''
496

  
497
    tmpl = Template('{% if "3"|decimal == 3 %}hello{% endif %}')
498
    assert tmpl.render(Context()) == 'hello'
499
    tmpl = Template('{% if "3"|decimal == 3.0 %}hello{% endif %}')
500
    assert tmpl.render(Context()) == 'hello'
501
    tmpl = Template('{% if 3|decimal == 3 %}hello{% endif %}')
502
    assert tmpl.render(Context()) == 'hello'
503
    tmpl = Template('{% if 3.0|decimal == 3 %}hello{% endif %}')
504
    assert tmpl.render(Context()) == 'hello'
505
    tmpl = Template('{% if 3|decimal|decimal == 3 %}hello{% endif %}')
506
    assert tmpl.render(Context()) == 'hello'
466
-