add templatetag to parse and format onlymoov durations (#15079)

This commit is contained in:
Frédéric Péters 2017-07-10 15:00:54 +02:00
parent 684dd71f68
commit 9ea4def4ec
1 changed files with 22 additions and 0 deletions

View File

@ -77,3 +77,25 @@ def as_opening_hours_badge(data):
label = 'Ouvert jusque %sh%02d' % (slots[0].end.hour, slots[0].end.minute)
return mark_safe('<div class="badge %s"><span>%s</span></div>' % (klass, label))
@register.filter
def onlymoov_duration(string):
# take the hours and minutes components of duration strings provided by
# onlymoov, "PT1H16M3S", "PT1M35S"
try:
groups = re.match(r'PT(\d+H)?(\d+M)', string).groups()
except AttributeError: # didn't match
return '?'
hours = ''
if groups[0]:
nb_hours = int(groups[0][:-1])
if nb_hours > 1:
hours = '%s heures' % nb_hours
else:
hours = '%s heure' % nb_hours
nb_minutes = int(groups[1][:-1])
if nb_minutes == 0:
minutes = ''
else:
minutes = '%d min' % nb_minutes
return '%s %s' % (hours, minutes)