diff --git a/lacommunaute/utils/templatetags/date_filters.py b/lacommunaute/utils/templatetags/date_filters.py index 6d6b39ac5..0bf59f660 100644 --- a/lacommunaute/utils/templatetags/date_filters.py +++ b/lacommunaute/utils/templatetags/date_filters.py @@ -23,3 +23,12 @@ def relativetimesince_fr(d): return f"{date(d,'l')}, {time(d)}" return f"il y a {timesince(d)}" + + +@register.filter(is_safe=True) +def convert_seconds_into_hours(value, default=None): + if value is None: + return "0h 00min" + hours = value // 3600 + minutes = (value % 3600) // 60 + return f"{hours}h {minutes:02d}min" diff --git a/lacommunaute/utils/tests/tests_utils.py b/lacommunaute/utils/tests/tests_utils.py index ca87fd8f7..ef5656cfb 100644 --- a/lacommunaute/utils/tests/tests_utils.py +++ b/lacommunaute/utils/tests/tests_utils.py @@ -148,6 +148,15 @@ def test_urlize(self): self.assertEqual(urlize(img), img) +class TestUtilsTemplateTags: + @pytest.mark.parametrize( + "value,expected_result", [(900, "0h 15min"), (3600, "1h 00min"), (7320, "2h 02min"), (None, "0h 00min")] + ) + def test_convert_seconds_into_hours(self, value, expected_result): + template = Template("{% load date_filters %}{{ value|convert_seconds_into_hours }}") + assert template.render(Context({"value": value})) == expected_result + + class UtilsTemplateTagsTestCase(TestCase): def test_pluralizefr(self): """Test `pluralizefr` template tag."""