templatetags: get opening hours directly from specifications (#48919)

This commit is contained in:
Nicolas Roche 2020-12-01 19:59:59 +01:00
parent 3b2d0c9b01
commit 560af3110e
1 changed files with 35 additions and 20 deletions

View File

@ -90,6 +90,25 @@ def get_open_close_from_specification(specification, valid_from, base_datetime):
return (opening_time, closing_time, day_number)
def get_time_table_from_specification(specification):
"""Parse an openinghoursspecification data block
"""
if not isinstance(specification['dayOfWeek'], str):
raise ValueError
day_of_week = specification.get('dayOfWeek')
day_number = EN_FULL_WEEKDAYS_LIST.index(day_of_week.split('/')[-1])
weekday = EN_ABBREV_WEEKDAYS_LIST[day_number]
start_hour, start_minute = specification['opens'].split(':')
end_hour, end_minute = specification['closes'].split(':')
time_table = {
'start_hour': start_hour,
'start_minute': start_minute,
'end_hour': end_hour,
'end_minute': end_minute,
}
return (weekday, time_table)
def openinghours_to_datetime(codename, hour, minute):
"""
return the next date and time after now()
@ -267,15 +286,23 @@ def get_mairie_opening_hours(mairie_data):
opening_hours_dict = OrderedDict(zip(EN_ABBREV_WEEKDAYS_LIST, [{
'am': None, 'pm': None
} for i in range(7)]))
def update_opening_hours(weekday, time_table):
period, all_day_hours = get_period_from_data(
weekday, open_close_time_string=(
time_table['start_hour'], time_table['start_minute'],
time_table['end_hour'], time_table['end_minute']))
if all_day_hours and period == 'am':
opening_hours_dict[weekday]['pm'] = '' # empty string to avoid displaying fermé
opening_hours_dict[weekday][period] = "%sh%s-%sh%s" % (
time_table['start_hour'], time_table['start_minute'],
time_table['end_hour'], time_table['end_minute'])
known_format = False
for days_list, time_table in parse_opening_hours_data(mairie_data):
known_format = True
for weekday in days_list:
(period, all_day_hours) = get_period_from_data(weekday,
open_close_time_string=(time_table['start_hour'], time_table['start_minute'], time_table['end_hour'], time_table['end_minute']))
if all_day_hours and period == 'am':
opening_hours_dict[weekday]['pm'] = '' # empty string to avoid displaying fermé
opening_hours_dict[weekday][period] = "%sh%s-%sh%s" % (time_table['start_hour'], time_table['start_minute'], time_table['end_hour'], time_table['end_minute'])
update_opening_hours(weekday, time_table)
if not known_format:
# some mairie only have openinghoursspecification (e.g. Jonage)
@ -293,21 +320,9 @@ def get_mairie_opening_hours(mairie_data):
# case when opening periods are defined
if 'opens' in specification and 'closes' in specification:
# parse specification only for the current period relative to utcnow()
if base_datetime >= valid_from and base_datetime < valid_through:
try:
(opening_time, closing_time, day_number) = get_open_close_from_specification(
specification, valid_from, base_datetime)
except ValueError:
continue
abbr_day_of_week = EN_ABBREV_WEEKDAYS_LIST[day_number]
(period, all_day_hours) = get_period_from_data(abbr_day_of_week,
opening_time=opening_time, closing_time=closing_time)
if all_day_hours and period == 'am':
opening_hours_dict[abbr_day_of_week]['pm'] = '' # empty string to avoid displaying fermé
opening_hours_dict[abbr_day_of_week][period] = "%sh%s-%sh%s" % (
opening_time.strftime('%H'), opening_time.strftime('%M'),
closing_time.strftime('%H'), closing_time.strftime('%M'))
if valid_from < base_datetime < valid_through:
weekday, time_table = get_time_table_from_specification(specification)
update_opening_hours(weekday, time_table)
if not (any([x['am'] for x in opening_hours_dict.values()]) or
any([x['pm'] for x in opening_hours_dict.values()])):