Skip to content

Commit

Permalink
Fix #6414
Browse files Browse the repository at this point in the history
- s'assure qu'on donne un duration_min au template
- s'assure que si on a pas un int on met "moins d'une minute"

(cherry picked from commit 27c6d58)
  • Loading branch information
artragis authored and philippemilink committed Nov 6, 2022
1 parent 6dd4804 commit 4815567
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions zds/tutorialv2/views/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ def get_context_data(self, **kwargs):
* char_count
/ settings.ZDS_APP["content"]["characters_per_minute"]
)
else:
logger.warning("For unknown reason content with id %s has no char count", self.object.pk)
context["reading_time"] = 0
except ZeroDivisionError as e:
logger.warning("could not compute reading time: setting characters_per_minute is set to zero (error=%s)", e)

Expand Down
6 changes: 6 additions & 0 deletions zds/utils/templatetags/humanize_duration.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import logging
import numbers
from typing import Tuple, List, Iterator, Iterable

from django import template
from django.utils.translation import ngettext, gettext as _

logger = logging.getLogger(__file__)
register = template.Library()


Expand All @@ -14,6 +17,9 @@ def humanize_duration(duration_min: int) -> str:
See the unit tests for examples of results.
"""
if not isinstance(duration_min, int):
logger.warning("Invalid duration %s", duration_min)
return humanize_duration(0)
duration_min = max(duration_min, 0) # to avoid surprises with modulo operations
truncated_value = _truncate_duration(
duration_min,
Expand Down
1 change: 1 addition & 0 deletions zds/utils/tests/tests_humanize_duration.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def test_main(self):
{"input": 110, "expected": "1 heure et 45 minutes"},
{"input": 125, "expected": "2 heures"},
{"input": 155, "expected": "2 heures et 30 minutes"},
{"input": "", "expected": "moins d'une minute"},
{"input": 24 * 60, "expected": "24 heures"},
{"input": 48 * 60, "expected": "48 heures"},
]
Expand Down

0 comments on commit 4815567

Please sign in to comment.