Projet

Général

Profil

0001-templatetags-add-as_numeral-filters-57983.patch

Valentin Deniaud, 19 octobre 2021 17:11

Télécharger (6,77 ko)

Voir les différences:

Subject: [PATCH] templatetags: add as_numeral filters (#57983)

 debian/control                 |  3 +-
 debian/debian_config_common.py |  3 ++
 hobo/templatetags/__init__.py  |  0
 hobo/templatetags/hobo.py      | 46 ++++++++++++++++++++++++++++++
 tests/settings.py              |  2 ++
 tests/test_templatetags.py     | 52 ++++++++++++++++++++++++++++++++++
 tox.ini                        |  1 +
 7 files changed, 106 insertions(+), 1 deletion(-)
 create mode 100644 hobo/templatetags/__init__.py
 create mode 100644 hobo/templatetags/hobo.py
 create mode 100644 tests/test_templatetags.py
debian/control
17 17
    python3-prometheus-client,
18 18
    python3-djangorestframework,
19 19
    python3-dnspython,
20
    python3-systemd
20
    python3-systemd,
21
    python3-num2words
21 22
Breaks: python-hobo (<< 1.53.post2)
22 23
Replaces: python-hobo (<< 1.53.post2)
23 24
Recommends:
debian/debian_config_common.py
375 375
    if 'authentic2' not in INSTALLED_APPS:
376 376
        MELLON_ADAPTER = ('hobo.multitenant.mellon.MellonAdapter',)
377 377

  
378
if PROJECT_NAME in ('wcs', 'combo'):
379
    TEMPLATES[0]['OPTIONS'].setdefault('builtins', []).append('hobo.templatetags.hobo')
380

  
378 381

  
379 382
if 'authentic2' not in INSTALLED_APPS:
380 383
    MELLON_DEFAULT_ASSERTION_CONSUMER_BINDING = 'artifact'
hobo/templatetags/hobo.py
1
# hobo - portal to configure and deploy applications
2
# Copyright (C) 2015-2021 Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
import decimal
18

  
19
from django import template
20
from django.template.defaultfilters import stringfilter
21
from django.utils.translation import get_language
22
from num2words import num2words
23

  
24
register = template.Library()
25

  
26

  
27
def unlazy(x):
28
    return x.get_value() if hasattr(x, 'get_value') else x
29

  
30

  
31
@register.filter
32
@stringfilter
33
def as_numeral(number):
34
    try:
35
        return num2words(unlazy(number), lang=get_language())
36
    except (TypeError, ValueError, decimal.InvalidOperation, OverflowError):
37
        return ''
38

  
39

  
40
@register.filter
41
@stringfilter
42
def as_numeral_currency(number):
43
    try:
44
        return num2words(unlazy(number), lang=get_language(), to='currency')
45
    except (TypeError, ValueError, decimal.InvalidOperation, OverflowError):
46
        return ''
tests/settings.py
24 24
        },
25 25
    }
26 26
}
27

  
28
TEMPLATES[0]['OPTIONS'].setdefault('builtins', []).append('hobo.templatetags.hobo')
tests/test_templatetags.py
1
# hobo - portal to configure and deploy applications
2
# Copyright (C) 2015-2021 Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
from django.template import Context, Template
18

  
19

  
20
def test_as_numeral(settings):
21
    t = Template('{{ number|as_numeral }}')
22
    assert t.render(Context({'number': 42})) == 'forty-two'
23
    assert t.render(Context({'number': '42'})) == 'forty-two'
24
    assert t.render(Context({'number': 42.15})) == 'forty-two point one five'
25
    assert t.render(Context({'number': -42})) == 'minus forty-two'
26
    assert t.render(Context({'number': None})) == ''
27
    assert t.render(Context({'number': 'foo'})) == ''
28
    assert t.render(Context({'number': ['foo', 'bar']})) == ''
29
    assert t.render(Context({'number': {'foo': 'bar'}})) == ''
30
    assert t.render(Context({'number': 10 ** 500})) == ''
31

  
32
    settings.LANGUAGE_CODE = 'fr'
33
    assert t.render(Context({'number': 42})) == 'quarante-deux'
34
    assert t.render(Context({'number': 42.15})) == 'quarante-deux virgule un cinq'
35

  
36

  
37
def test_as_numeral_currency(settings):
38
    t = Template('{{ number|as_numeral_currency }}')
39
    assert t.render(Context({'number': 42})) == 'forty-two euro, zero cents'
40
    assert t.render(Context({'number': '42'})) == 'forty-two euro, zero cents'
41
    assert t.render(Context({'number': 42.15})) == 'forty-two euro, fifteen cents'
42
    assert t.render(Context({'number': -42})) == 'minus  forty-two euro, zero cents'
43
    assert t.render(Context({'number': None})) == ''
44
    assert t.render(Context({'number': 'foo'})) == ''
45
    assert t.render(Context({'number': ['foo', 'bar']})) == ''
46
    assert t.render(Context({'number': {'foo': 'bar'}})) == ''
47
    assert t.render(Context({'number': 10 ** 500})) == ''
48

  
49
    settings.LANGUAGE_CODE = 'fr'
50
    assert t.render(Context({'number': 42})) == 'quarante-deux euros et zéro centimes'
51
    assert t.render(Context({'number': 42.15})) == 'quarante-deux euros et quinze centimes'
52
    assert t.render(Context({'number': '1'})) == 'un euro et zéro centimes'
tox.ini
55 55
	xmlschema<1.1
56 56
	enum34<=1.1.6
57 57
	psycopg2-binary<2.9
58
	num2words
58 59
	black: pre-commit
59 60
commands =
60 61
	./getlasso3.sh
61
-