From 10241ecc01d39f1aad2ae3150d6051700209e391 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 7 Jan 2021 15:56:25 +0100
Subject: [PATCH] misc: add |age_in_working_days filters (#49924)

---
 tests/test_templates.py           | 97 ++++++++++++++++++++++++++++---
 wcs/qommon/templatetags/qommon.py | 23 ++++++++
 2 files changed, 113 insertions(+), 7 deletions(-)

diff --git a/tests/test_templates.py b/tests/test_templates.py
index cf1fe210..31b48c44 100644
--- a/tests/test_templates.py
+++ b/tests/test_templates.py
@@ -815,7 +815,7 @@ def test_is_working_day_settings(settings, pub):
     (datetime.date(2020, 7, 14), False),
     (datetime.date(2020, 7, 15), True),
 ])
-def test_is_working_day(settings, value, expected):
+def test_is_working_day(settings, pub, value, expected):
     settings.WORKING_DAY_CALENDAR = 'workalendar.europe.France'
     t = Template('{{ value|is_working_day }}')
     assert t.render({'value': value}) == str(expected)
@@ -823,7 +823,7 @@ def test_is_working_day(settings, value, expected):
     assert t.render({'value': value}) == str(expected)
 
 
-def test_is_working_day_weekend(settings):
+def test_is_working_day_weekend(settings, pub):
     settings.WORKING_DAY_CALENDAR = 'workalendar.europe.France'
     # check saturday
     t = Template('{{ value|is_working_day }}')
@@ -878,7 +878,7 @@ def test_add_working_days_settings(settings, pub):
     assert t.render({'value': '2020-07-13'}) == '2020-07-15'
 
 
-def test_add_working_days_arg(settings):
+def test_add_working_days_arg(settings, pub):
     settings.WORKING_DAY_CALENDAR = 'workalendar.europe.France'
     t = Template('{{ value|add_working_days:"foobar" }}')
     assert t.render({'value': '2020-07-13'}) == ''
@@ -903,7 +903,7 @@ def test_add_working_days_arg(settings):
     (datetime.datetime(2020, 7, 13, 12, 1, 3), '2020-07-15'),
     (datetime.date(2020, 7, 13), '2020-07-15'),
 ])
-def test_add_working_days(settings, value, expected):
+def test_add_working_days(settings, pub, value, expected):
     settings.WORKING_DAY_CALENDAR = 'workalendar.europe.France'
     t = Template('{{ value|add_working_days:1 }}')
     assert t.render({'value': value}) == str(expected)
@@ -911,7 +911,7 @@ def test_add_working_days(settings, value, expected):
     assert t.render({'value': value}) == str(expected)
 
 
-def test_add_working_days_weekend(settings):
+def test_add_working_days_weekend(settings, pub):
     settings.WORKING_DAY_CALENDAR = 'workalendar.europe.France'
     t = Template('{{ value|add_working_days:1 }}')
     assert t.render({'value': '2020-06-19'}) == '2020-06-22'
@@ -974,7 +974,7 @@ def test_adjust_to_working_day_settings(settings, pub):
     (datetime.date(2020, 7, 14), '2020-07-15'),
     (datetime.date(2020, 7, 15), '2020-07-15'),
 ])
-def test_adjust_to_working_day(settings, value, expected):
+def test_adjust_to_working_day(settings, pub, value, expected):
     settings.WORKING_DAY_CALENDAR = 'workalendar.europe.France'
     t = Template('{{ value|adjust_to_working_day }}')
     assert t.render({'value': value}) == str(expected)
@@ -982,7 +982,7 @@ def test_adjust_to_working_day(settings, value, expected):
     assert t.render({'value': value}) == str(expected)
 
 
-def test_adjust_to_working_day_weekend(settings):
+def test_adjust_to_working_day_weekend(settings, pub):
     settings.WORKING_DAY_CALENDAR = 'workalendar.europe.France'
     t = Template('{{ value|adjust_to_working_day }}')
     assert t.render({'value': '2020-06-20'}) == '2020-06-22'
@@ -990,6 +990,89 @@ def test_adjust_to_working_day_weekend(settings):
     assert t.render({'value': '2020-06-20'}) == '2020-06-20'
 
 
+def test_age_in_working_days_settings(settings, pub):
+    settings.WORKING_DAY_CALENDAR = None
+    t = Template('{{ value|age_in_working_days:"2020-07-15" }}')
+    assert t.render({'value': '2020-07-12'}) == ''
+    t = Template('{{ value|age_in_working_days_with_saturday:"2020-07-15" }}')
+    assert t.render({'value': '2020-07-12'}) == ''
+
+    settings.WORKING_DAY_CALENDAR = ''
+    t = Template('{{ value|age_in_working_days:"2020-07-15" }}')
+    assert t.render({'value': '2020-07-12'}) == ''
+    t = Template('{{ value|age_in_working_days_with_saturday:"2020-07-15" }}')
+    assert t.render({'value': '2020-07-12'}) == ''
+
+    settings.WORKING_DAY_CALENDAR = 'foobar'
+    t = Template('{{ value|age_in_working_days:"2020-07-15" }}')
+    assert t.render({'value': '2020-07-12'}) == ''
+    t = Template('{{ value|age_in_working_days_with_saturday:"2020-07-15" }}')
+    assert t.render({'value': '2020-07-12'}) == ''
+
+    settings.WORKING_DAY_CALENDAR = 'workalendar.europe.France'
+    t = Template('{{ value|age_in_working_days:"2020-07-15" }}')
+    assert t.render({'value': '2020-07-12'}) == '2'
+    t = Template('{{ value|age_in_working_days_with_saturday:"2020-07-15" }}')
+    assert t.render({'value': '2020-07-12'}) == '2'
+
+    pub.site_options.set('options', 'working_day_calendar', 'foobar')
+    pub.site_options.write(open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w'))
+    t = Template('{{ value|age_in_working_days:"2020-07-15" }}')
+    assert t.render({'value': '2020-07-12'}) == ''
+    t = Template('{{ value|age_in_working_days_with_saturday:"2020-07-15" }}')
+    assert t.render({'value': '2020-07-12'}) == ''
+
+    settings.WORKING_DAY_CALENDAR = 'foobar'
+    pub.site_options.set('options', 'working_day_calendar', 'workalendar.europe.France')
+    pub.site_options.write(open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w'))
+    t = Template('{{ value|age_in_working_days:"2020-07-15" }}')
+    assert t.render({'value': '2020-07-12'}) == '2'
+    t = Template('{{ value|age_in_working_days_with_saturday:"2020-07-15" }}')
+    assert t.render({'value': '2020-07-12'}) == '2'
+
+
+def test_age_in_working_days_arg(settings, pub):
+    settings.WORKING_DAY_CALENDAR = 'workalendar.europe.France'
+    t = Template('{{ value|age_in_working_days:"foobar" }}')
+    assert t.render({'value': '2020-07-12'}) == ''
+    t = Template('{{ value|age_in_working_days_with_saturday:"foobar" }}')
+    assert t.render({'value': '2020-07-12'}) == ''
+    t = Template('{{ value|age_in_working_days:"2020-07-15" }}')
+    assert t.render({'value': '2020-07-12'}) == '2'
+    t = Template('{{ value|age_in_working_days_with_saturday:"2020-07-15" }}')
+    assert t.render({'value': '2020-07-12'}) == '2'
+
+
+@pytest.mark.parametrize('value, expected', [
+    (None, ''),
+    ('', ''),
+    ('foobar', ''),
+    (42, ''),
+    ('2020-07-14T12:01:03', '2'),
+    ('2020-07-14 02:03', '2'),
+    ('14/07/2020 02h03', '2'),
+    ('2020-07-14', '2'),
+    ('14/07/2020', '2'),
+    (datetime.datetime(2020, 7, 14, 12, 1, 3), '2'),
+    (datetime.date(2020, 7, 14), '2'),
+    (datetime.date(2020, 7, 15), '1'),
+])
+def test_age_in_working_days(settings, pub, value, expected):
+    settings.WORKING_DAY_CALENDAR = 'workalendar.europe.France'
+    t = Template('{{ value|age_in_working_days:"2020-07-16" }}')
+    assert t.render({'value': value}) == str(expected)
+    t = Template('{{ value|age_in_working_days_with_saturday:"2020-07-16" }}')
+    assert t.render({'value': value}) == str(expected)
+
+
+def test_age_in_working_days_weekend(settings, pub):
+    settings.WORKING_DAY_CALENDAR = 'workalendar.europe.France'
+    t = Template('{{ value|age_in_working_days:"2020-06-22" }}')
+    assert t.render({'value': '2020-06-19'}) == '1'
+    t = Template('{{ value|age_in_working_days_with_saturday:"2020-06-22" }}')
+    assert t.render({'value': '2020-06-19'}) == '2'
+
+
 def test_sum():
     tmpl = Template('{{ "2 29.5 9,5 .5"|split|sum }}')
     assert tmpl.render({}) == '41.5'
diff --git a/wcs/qommon/templatetags/qommon.py b/wcs/qommon/templatetags/qommon.py
index 97c53bf0..a98f4219 100644
--- a/wcs/qommon/templatetags/qommon.py
+++ b/wcs/qommon/templatetags/qommon.py
@@ -382,6 +382,29 @@ def adjust_to_working_day_with_saturday(value):
     return adjust_to_working_day(value, saturday_is_a_working_day=True)
 
 
+@register.filter(expects_localtime=True)
+def age_in_working_days(value, arg, saturday_is_a_working_day=False):
+    print('toto')
+    value = parse_date(value)
+    if not value:
+        return ''
+
+    arg = parse_date(arg)
+    if not arg:
+        return ''
+
+    cal = calendar.get_calendar(saturday_is_a_working_day=saturday_is_a_working_day)
+    if not cal:
+        return ''
+
+    return cal.get_working_days_delta(value, arg)
+
+
+@register.filter(expects_localtime=True)
+def age_in_working_days_with_saturday(value, arg):
+    return age_in_working_days(value, arg, saturday_is_a_working_day=True)
+
+
 @register.simple_tag
 def standard_text(text_id):
     return mark_safe(TextsDirectory.get_html_text(str(text_id)))
-- 
2.29.2

