misc: |add_hours filter (#54411)

This commit is contained in:
Lauréline Guérin 2021-06-11 10:51:53 +02:00
parent 4255ed1674
commit 671d651784
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
3 changed files with 39 additions and 1 deletions

View File

@ -2056,10 +2056,25 @@ def test_lazy_date_with_maths(pub, variable_test_data):
condition = Condition({'type': 'django', 'value': condition_value.replace('today', 'now')})
assert condition.evaluate() is True
for condition_value in (
'"1970-01-01"|add_minutes:0 < today',
'"2100-12-31"|add_minutes:0 > today',
'form_var_datefield|add_minutes:0 <= today',
'form_var_datefield|add_minutes:10 < today',
'form_var_datefield|add_minutes:21024000 > today', # + 100 years
'form_var_datefield|add_minutes:21024000 >= today',
):
condition = Condition({'type': 'django', 'value': condition_value})
assert condition.evaluate() is True
condition = Condition({'type': 'django', 'value': condition_value.replace('today', 'now')})
assert condition.evaluate() is True
for condition_value in (
'today|add_days:0 == today',
'today|add_hours:0 == today',
'today|add_minutes:0 == today',
'now|add_hours:0 == now',
'now|add_minutes:0 == now',
):
condition = Condition({'type': 'django', 'value': condition_value})
assert condition.evaluate() is True

View File

@ -370,7 +370,7 @@ def test_datetime_templatetags():
assert tmpl.render() == ''
def test_date_maths():
def test_date_maths(pub):
tmpl = Template('{{ plop|add_days:4 }}')
assert tmpl.render({'plop': '2017-12-21'}) == '2017-12-25'
assert tmpl.render({'plop': '2017-12-21 18:00'}) == '2017-12-25'
@ -391,6 +391,13 @@ def test_date_maths():
assert tmpl.render({'plop': '2017-12-21'}) == '2017-12-21 12:30'
assert tmpl.render({'plop': '2017-12-21 18:00'}) == '2017-12-22 06:30'
tmpl = Template('{{ plop|add_minutes:30 }}')
assert tmpl.render({'plop': '2017-12-21'}) == '2017-12-21 00:30'
assert tmpl.render({'plop': '2017-12-21 18:00'}) == '2017-12-21 18:30'
tmpl = Template('{{ plop|add_minutes:"12.5"|date:"Y-m-d H:m:s" }}')
assert tmpl.render({'plop': '2017-12-21'}) == '2017-12-21 00:12:30'
assert tmpl.render({'plop': '2017-12-21 18:00'}) == '2017-12-21 18:12:30'
def test_variable_unicode_error_handling():
tmpl = Template('{{ form_var_éléphant }}')

View File

@ -283,6 +283,22 @@ def add_hours(value, arg):
return lazy_date(value + datetime.timedelta(hours=float(arg)))
@register.filter(expects_localtime=True, is_safe=False)
def add_minutes(value, arg):
if hasattr(value, 'timetuple'):
# extract real value in case of lazy object
value = value.timetuple()
value = parse_datetime(value)
if not value:
return ''
from wcs.variables import lazy_date
arg = parse_decimal(arg)
if not arg:
return lazy_date(value)
return lazy_date(value + datetime.timedelta(minutes=float(arg)))
@register.filter(expects_localtime=True, is_safe=False)
def age_in_days(value, now=None):
try: