From d6507267c15cc85bf1efc6341acf46b96b0ab5d7 Mon Sep 17 00:00:00 2001 From: Nicolas ROCHE Date: Sat, 5 Dec 2020 21:09:59 +0100 Subject: [PATCH] templatetags: simplify start/end date computation on openinghoursspecification (#48919) --- combo_plugin_gnm/templatetags/gnm.py | 44 +++++----------------------- 1 file changed, 7 insertions(+), 37 deletions(-) diff --git a/combo_plugin_gnm/templatetags/gnm.py b/combo_plugin_gnm/templatetags/gnm.py index 93dfe2f..801ac99 100644 --- a/combo_plugin_gnm/templatetags/gnm.py +++ b/combo_plugin_gnm/templatetags/gnm.py @@ -120,36 +120,6 @@ def get_slot(day_number, time_table, base_datetime): return TimeSlot(start, end) -def get_open_close_from_specification(specification, valid_from, base_datetime): - '''Parse geojson 'openinghoursspecification' fields data - ''' - opening_time = datetime.datetime.combine(base_datetime, dateutil_parse(specification['opens']).time()) - closing_time = datetime.datetime.combine(base_datetime, dateutil_parse(specification['closes']).time()) - if not isinstance(specification['dayOfWeek'], str): - raise ValueError() - day_number = EN_FULL_WEEKDAYS_LIST.index(specification['dayOfWeek'].split('/')[-1]) - start = opening_time + datetime.timedelta( - days=(7 + (day_number - opening_time.weekday())) % 7) - end = closing_time + datetime.timedelta( - days=(7 + (day_number - closing_time.weekday())) % 7) - - # hours may belongs on next day - if end < start: - day_number += 1 - end = closing_time + datetime.timedelta( - days=(7 + (day_number - closing_time.weekday())) % 7) - if end < start: - # end time may be find this week whereas start time is picked on next week, - # this occure if we are now past 24:00, on next day. - # Above + (day_number - opening_time.weekday())) % 7 computation, - # which acts as openinghours_to_datetime (return the next date and time after now()) - # can't return a coherent start time. - # in this case we compute start date relative to end date - opening_time = datetime.datetime.combine(end, dateutil_parse(specification['opens']).time()) - start = opening_time - datetime.timedelta(days=1) - return (start, end, day_number) - - def get_time_table_from_specification(specification): """Parse an openinghoursspecification data block """ @@ -157,7 +127,6 @@ def get_time_table_from_specification(specification): 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 = { @@ -166,7 +135,7 @@ def get_time_table_from_specification(specification): 'end_hour': end_hour, 'end_minute': end_minute, } - return (weekday, time_table) + return (day_number, time_table) def get_period_from_data(time_table): @@ -284,13 +253,13 @@ def get_slots_from_mairie_format(data, base_datetime): continue if specification.get('opens') and specification.get('closes'): # case when opening periods are defined - if base_datetime >= valid_from and base_datetime < valid_through: + if valid_from <= base_datetime < valid_through: try: - (opening_time, closing_time, day_number) = get_open_close_from_specification( - specification, valid_from, base_datetime) + day_number, time_table = get_time_table_from_specification(specification) except ValueError: continue - slots.append(TimeSlot(opening_time, closing_time)) + timeslot = get_slot(day_number, time_table, base_datetime) + slots.append(timeslot) else: # case when exclusions are defined exclusion_slots.append(TimeSlot(valid_from, valid_through)) @@ -374,7 +343,8 @@ def get_mairie_opening_hours(mairie_data): if 'opens' in specification and 'closes' in specification: # parse specification only for the current period relative to utcnow() if valid_from < base_datetime < valid_through: - weekday, time_table = get_time_table_from_specification(specification) + day_number, time_table = get_time_table_from_specification(specification) + weekday = EN_ABBREV_WEEKDAYS_LIST[day_number] update_opening_hours(weekday, time_table) if not (any([x['am'] for x in opening_hours_dict.values()]) or