Skip to content

Commit

Permalink
add convert_seconds_into_hours template tag
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Oct 8, 2024
1 parent 391e31e commit e297b4e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lacommunaute/utils/templatetags/date_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
9 changes: 9 additions & 0 deletions lacommunaute/utils/tests/tests_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down

0 comments on commit e297b4e

Please sign in to comment.