Projet

Général

Profil

0001-misc-add_hours-filter-54411.patch

Lauréline Guérin, 11 juin 2021 10:53

Télécharger (3,83 ko)

Voir les différences:

Subject: [PATCH] misc: |add_hours filter (#54411)

 tests/test_formdata.py            | 15 +++++++++++++++
 tests/test_templates.py           |  9 ++++++++-
 wcs/qommon/templatetags/qommon.py | 16 ++++++++++++++++
 3 files changed, 39 insertions(+), 1 deletion(-)
tests/test_formdata.py
2056 2056
        condition = Condition({'type': 'django', 'value': condition_value.replace('today', 'now')})
2057 2057
        assert condition.evaluate() is True
2058 2058

  
2059
    for condition_value in (
2060
        '"1970-01-01"|add_minutes:0 < today',
2061
        '"2100-12-31"|add_minutes:0 > today',
2062
        'form_var_datefield|add_minutes:0 <= today',
2063
        'form_var_datefield|add_minutes:10 < today',
2064
        'form_var_datefield|add_minutes:21024000 > today',  # + 100 years
2065
        'form_var_datefield|add_minutes:21024000 >= today',
2066
    ):
2067
        condition = Condition({'type': 'django', 'value': condition_value})
2068
        assert condition.evaluate() is True
2069
        condition = Condition({'type': 'django', 'value': condition_value.replace('today', 'now')})
2070
        assert condition.evaluate() is True
2071

  
2059 2072
    for condition_value in (
2060 2073
        'today|add_days:0 == today',
2061 2074
        'today|add_hours:0 == today',
2075
        'today|add_minutes:0 == today',
2062 2076
        'now|add_hours:0 == now',
2077
        'now|add_minutes:0 == now',
2063 2078
    ):
2064 2079
        condition = Condition({'type': 'django', 'value': condition_value})
2065 2080
        assert condition.evaluate() is True
tests/test_templates.py
370 370
    assert tmpl.render() == ''
371 371

  
372 372

  
373
def test_date_maths():
373
def test_date_maths(pub):
374 374
    tmpl = Template('{{ plop|add_days:4 }}')
375 375
    assert tmpl.render({'plop': '2017-12-21'}) == '2017-12-25'
376 376
    assert tmpl.render({'plop': '2017-12-21 18:00'}) == '2017-12-25'
......
391 391
    assert tmpl.render({'plop': '2017-12-21'}) == '2017-12-21 12:30'
392 392
    assert tmpl.render({'plop': '2017-12-21 18:00'}) == '2017-12-22 06:30'
393 393

  
394
    tmpl = Template('{{ plop|add_minutes:30 }}')
395
    assert tmpl.render({'plop': '2017-12-21'}) == '2017-12-21 00:30'
396
    assert tmpl.render({'plop': '2017-12-21 18:00'}) == '2017-12-21 18:30'
397
    tmpl = Template('{{ plop|add_minutes:"12.5"|date:"Y-m-d H:m:s" }}')
398
    assert tmpl.render({'plop': '2017-12-21'}) == '2017-12-21 00:12:30'
399
    assert tmpl.render({'plop': '2017-12-21 18:00'}) == '2017-12-21 18:12:30'
400

  
394 401

  
395 402
def test_variable_unicode_error_handling():
396 403
    tmpl = Template('{{ form_var_éléphant }}')
wcs/qommon/templatetags/qommon.py
283 283
    return lazy_date(value + datetime.timedelta(hours=float(arg)))
284 284

  
285 285

  
286
@register.filter(expects_localtime=True, is_safe=False)
287
def add_minutes(value, arg):
288
    if hasattr(value, 'timetuple'):
289
        # extract real value in case of lazy object
290
        value = value.timetuple()
291
    value = parse_datetime(value)
292
    if not value:
293
        return ''
294
    from wcs.variables import lazy_date
295

  
296
    arg = parse_decimal(arg)
297
    if not arg:
298
        return lazy_date(value)
299
    return lazy_date(value + datetime.timedelta(minutes=float(arg)))
300

  
301

  
286 302
@register.filter(expects_localtime=True, is_safe=False)
287 303
def age_in_days(value, now=None):
288 304
    try:
289
-