templatetags: simplify start/end date computation on mdr_format (#48919)

This commit is contained in:
Nicolas Roche 2020-12-05 21:22:07 +01:00
parent 226207a916
commit fac5f0587e
1 changed files with 12 additions and 15 deletions

View File

@ -160,7 +160,7 @@ def get_period_from_data(time_table):
return (period, all_day_hours)
def get_slots_from_mdr_format(data, today):
def get_slots_from_mdr_format(data, base_datetime):
"""Process data from Maison du rhone geojson data from data.grandlyon.fr
(/ws/grandlyon/ter_territoire.maison_du_rhone/all.json)
add to slots all the next opening hours in chronological order & beginning from today()
@ -172,9 +172,8 @@ def get_slots_from_mdr_format(data, today):
mdr_weekdays_format = ['%s_am' % day for day in FR_WEEKDAYS] + ['%s_pm' % day for day in FR_WEEKDAYS]
if any([re.search('|'.join(mdr_weekdays_format), data_key) is not None for data_key in data.keys()]):
known_format = True
today -= datetime.timedelta(days=1) # add yesterday night hours
today = base_datetime
for i in range(7):
tomorrow = today + datetime.timedelta(days=1)
for period in ('am', 'pm'):
hours = data.get('%s_%s' % (FR_WEEKDAYS[today.weekday()], period))
if not hours:
@ -183,20 +182,18 @@ def get_slots_from_mdr_format(data, today):
parts = re.match(r'(\d?\d)h(\d\d)-(\d?\d)h(\d\d)', hours).groups()
except AttributeError:
continue
time_table = {
'start_hour': int(parts[0]),
'start_minute': int(parts[1]),
'end_hour': int(parts[2]),
'end_minute': int(parts[3]),
}
# add to slots the opening hours in chronological order beginning from today
start = datetime.datetime(today.year, today.month, today.day,
int(parts[0]), int(parts[1]), tzinfo=today.tzinfo)
end = datetime.datetime(today.year, today.month, today.day,
int(parts[2]), int(parts[3]), tzinfo=today.tzinfo)
timeslot = get_slot(today.weekday(), time_table, base_datetime)
slots.append(timeslot)
# hours may belongs on next day
if end < start:
end = datetime.datetime(
tomorrow.year, tomorrow.month, tomorrow.day,
int(parts[2]), int(parts[3]), tzinfo=tomorrow.tzinfo)
slots.append(TimeSlot(start, end))
today = tomorrow
today += datetime.timedelta(days=1)
return (slots, known_format)