templatetags: allow 24:00 input hour (#48929)

This commit is contained in:
Nicolas Roche 2020-12-05 22:55:49 +01:00
parent fac5f0587e
commit 3721cc8faa
3 changed files with 29 additions and 3 deletions

View File

@ -100,6 +100,9 @@ def get_slot(day_number, time_table, base_datetime):
end_hour = int(time_table['end_hour'])
end_minute = int(time_table['end_minute'])
if end_hour == 24 and end_minute == 0:
end_hour = 0
start = openinghours_to_datetime(day_number, start_hour, start_minute, base_datetime)
# hours may belongs on next day
@ -144,6 +147,9 @@ def get_period_from_data(time_table):
end_hour = int(time_table['end_hour'])
end_minute = int(time_table['end_minute'])
if end_hour == 24 and end_minute == 0:
end_hour = 0
closing_time = datetime.time(hour=end_hour, minute=end_minute)
opening_time = datetime.time(hour=start_hour, minute=start_minute)
@ -381,6 +387,12 @@ def as_opening_hours_badge(data):
if slot.start >= exclusion.start and slot.end <= exclusion.end:
slots[i] = None
def format_time(hour, minute):
time = "%sh%02d" % (hour, minute)
if time == '0h00':
time = 'minuit'
return time
# parse slots to return the right html
slots = [x for x in slots if x]
if not slots:
@ -397,13 +409,15 @@ def as_opening_hours_badge(data):
day_label = u'demain'
else:
day_label = FR_WEEKDAYS[slots[0].start.weekday()]
label = u'%s %s à %sh%02d' % (verb, day_label, slots[0].start.hour, slots[0].start.minute)
time = format_time(slots[0].start.hour, slots[0].start.minute)
label = u'%s %s à %s' % (verb, day_label, time)
elif base_datetime < slots[0].end:
if (slots[0].end - base_datetime).seconds < 3600:
klass = 'soon-to-be-closed'
else:
klass = 'open'
label = u"Ouvert jusqu'à %sh%02d" % (slots[0].end.hour, slots[0].end.minute)
time = format_time(slots[0].end.hour, slots[0].end.minute)
label = u"Ouvert jusqu'à %s" % time
return mark_safe(u'<div class="badge %s"><span>%s</span></div>' % (klass, label))

View File

@ -176,6 +176,7 @@ def test_mairie_holiday():
('lundi_am', '08h30-11h45', 'closed', 'Ouvre à 8h30'),
('lundi_am', '00h00-00h45', 'soon-to-be-closed', "Ouvert jusqu'à 0h45"),
('dimanche_pm', '20h30-00h45', 'soon-to-be-closed', "Ouvert jusqu'à 0h45"),
('lundi_am', '00h15-24h00', 'open', "Ouvert jusqu'à minuit"),
])
def test_mdr_format(day, hours, badge, text):
geojson = """
@ -198,6 +199,7 @@ def test_mdr_format(day, hours, badge, text):
('Mo 08:30-11:45', 'closed', 'Ouvre à 8h30'),
('Mo 00:00-00:45', 'soon-to-be-closed', "Ouvert jusqu'à 0h45"),
('Su 20:30-00:45', 'soon-to-be-closed', "Ouvert jusqu'à 0h45"),
('Mo 00:15-24:00', 'open', "Ouvert jusqu'à minuit"),
])
def test_mairie_format_openinghours(openinghour, badge, text):
geojson = """
@ -220,6 +222,7 @@ def test_mairie_format_openinghours(openinghour, badge, text):
('Monday', '08:30', '11:45', 'closed', 'Ouvre à 8h30'),
('Monday', '00:00', '00:45', 'soon-to-be-closed', "Ouvert jusqu'à 0h45"),
('Sunday', '20:30', '00:45', 'soon-to-be-closed', "Ouvert jusqu'à 0h45"),
('Monday', '00:15', '24:00', 'open', "Ouvert jusqu'à minuit"),
])
def test_mairie_format_openinghoursspecification(day, opens, closes, badge, text):
geojson = """

View File

@ -80,7 +80,8 @@ def test_mairie_format_openinghours():
"Tu 14:00-17:00",
"We 10:30-18:30",
"Th 22:30-00:30",
"Fr 07:30-07:00"
"Fr 07:30-07:00",
"Sa 21:00-24:00"
]}}
"""
hours = get_mairie_opening_hours(json.loads(geojson))
@ -90,6 +91,7 @@ def test_mairie_format_openinghours():
('mercredi', {'am': '10h30-18h30', 'pm': ''}),
('jeudi', {'am': None, 'pm': '22h30-00h30'}),
('vendredi', {'am': '07h30-07h00', 'pm': ''}),
('samedi', {'am': None, 'pm': '21h00-24h00'}),
]
@ -129,6 +131,12 @@ def test_mairie_format_openinghoursspecification():
"dayOfWeek": "http:\/\/schema.org\/Friday",
"validFrom": "2018-01-01T00:00:00+01:00",
"validThrough": "2018-06-30T23:59:59+02:00"
}, {
"opens": "21:00",
"closes": "24:00",
"dayOfWeek": "http:\/\/schema.org\/Saturday",
"validFrom": "2018-01-01T00:00:00+01:00",
"validThrough": "2018-06-30T23:59:59+02:00"
}]}}
"""
hours = get_mairie_opening_hours(json.loads(geojson))
@ -138,4 +146,5 @@ def test_mairie_format_openinghoursspecification():
('mercredi', {'am': '10h30-18h30', 'pm': ''}),
('jeudi', {'am': None, 'pm': '22h30-00h30'}),
('vendredi', {'am': '07h30-07h00', 'pm': ''}),
('samedi', {'am': None, 'pm': '21h00-24h00'}),
]