Projet

Général

Profil

0001-misc-use-ngettext-in-seconds2humanduration-to-plural.patch

Lauréline Guérin, 16 juin 2020 15:34

Télécharger (3,26 ko)

Voir les différences:

Subject: [PATCH] misc: use ngettext in seconds2humanduration to pluralize
 strings (#30788)

 tests/test_misc.py      | 18 ++++++++++++++----
 wcs/qommon/__init__.py  |  2 +-
 wcs/qommon/humantime.py | 14 +++++---------
 3 files changed, 20 insertions(+), 14 deletions(-)
tests/test_misc.py
11 11
import datetime
12 12
import base64
13 13

  
14
from django.utils import six
14
from django.utils import translation
15 15

  
16 16
from quixote import cleanup
17 17

  
......
64 64
            FileSizeWidget.parse_file_size(test_value)
65 65

  
66 66

  
67
def test_humantime():
68
    for x in range(3, 100000, 13):
69
        assert humanduration2seconds(seconds2humanduration(x)) == x
67
@pytest.mark.parametrize('seconds, expected', [
68
    (1, '1 second'),
69
    (3, '3 seconds'),
70
    (100000, '1 day, 3 hours, 46 minutes and 40 seconds'),
71
    (13, '13 seconds'),
72
    (60, '1 minute'),
73
    (3600, '1 hour'),
74
])
75
def test_humantime(seconds, expected):
76
    pub = create_temporary_pub()
77
    pub.ngettext = translation.ngettext
78
    assert seconds2humanduration(seconds) == expected
79
    assert humanduration2seconds(seconds2humanduration(seconds)) == seconds
70 80

  
71 81

  
72 82
def test_parse_mimetypes():
wcs/qommon/__init__.py
49 49
def ngettext(*args):
50 50
    pub = get_publisher()
51 51
    if pub is None:
52
        return message
52
        return args[0]
53 53
    return force_str(force_text(pub.ngettext(*args)))
54 54

  
55 55

  
wcs/qommon/humantime.py
16 16

  
17 17
import re
18 18

  
19
from . import _, N_
19
from . import _, ngettext, N_
20 20

  
21 21
_minute = 60
22 22
_hour = 60 * 60
......
66 66
def seconds2humanduration(seconds):
67 67
    '''Convert a time range in seconds to a human string representation
68 68
    '''
69
    # years = int(seconds / _year)
70
    # secons = seconds - _year * years
71
    # months = int(seconds / _month)
72
    # seconds = seconds - _month * months
73 69
    if not type(seconds) is int:
74 70
        return ""
75 71

  
......
81 77
    seconds = seconds - _minute * minutes
82 78
    human = []
83 79
    if days:
84
        human.append(_("%s days") % days)
80
        human.append(ngettext('%(total)s day', '%(total)s days', days) % {'total': days})
85 81
    if hours:
86
        human.append(_("%s hours") % hours)
82
        human.append(ngettext('%(total)s hour', '%(total)s hours', hours) % {'total': hours})
87 83
    if minutes:
88
        human.append(_("%s minutes") % minutes)
84
        human.append(ngettext('%(total)s minute', '%(total)s minutes', minutes) % {'total': minutes})
89 85
    if seconds:
90
        human.append(_("%s seconds") % seconds)
86
        human.append(ngettext('%(total)s second', '%(total)s seconds', seconds) % {'total': seconds})
91 87
    return list2human(human)
92
-