templatetags: ease undestanding openinghoursspecification is used as default (#50419)

This commit is contained in:
Nicolas Roche 2021-01-21 18:09:50 +01:00
parent 116511fc31
commit c0d31ce0c7
1 changed files with 12 additions and 9 deletions

View File

@ -254,10 +254,12 @@ def parse_mairie_formats(data, base_datetime, oh_add, ohs_add, ohs_del):
if 'properties' in data: if 'properties' in data:
data = data['properties'] data = data['properties']
some_opening_periods_defined = False
known_format = False known_format = False
previous_week = base_datetime - datetime.timedelta(7) previous_week = base_datetime - datetime.timedelta(7)
next_week = base_datetime + datetime.timedelta(7) next_week = base_datetime + datetime.timedelta(7)
for specification in data.get('openinghoursspecification', []): for specification in data.get('openinghoursspecification', []):
known_format = True
valid_from, valid_through = previous_week, next_week valid_from, valid_through = previous_week, next_week
if specification.get('validFrom'): if specification.get('validFrom'):
valid_from = parse_valid_from(specification) valid_from = parse_valid_from(specification)
@ -265,21 +267,22 @@ def parse_mairie_formats(data, base_datetime, oh_add, ohs_add, ohs_del):
valid_through = parse_valid_through(specification) valid_through = parse_valid_through(specification)
if not valid_from or not valid_through: if not valid_from or not valid_through:
continue continue
# parse specification only for the current period relative to utcnow()
if not valid_from <= base_datetime < valid_through:
continue
if specification.get('opens') and specification.get('closes'): if specification.get('opens') and specification.get('closes'):
known_format = True
# case when opening periods are defined # case when opening periods are defined
if valid_from <= base_datetime < valid_through: some_opening_periods_defined = True
# parse specification only for the current period relative to utcnow() try:
try: day_number, time_table = get_time_table_from_specification(specification)
day_number, time_table = get_time_table_from_specification(specification) except ValueError:
except ValueError: continue
continue ohs_add(day_number, time_table)
ohs_add(day_number, time_table)
else: else:
# case when exclusions are defined # case when exclusions are defined
ohs_del(valid_from, valid_through) ohs_del(valid_from, valid_through)
if not known_format: if not some_opening_periods_defined:
# some mairie may only have opening periods defined into openinghours (e.g. Bron) # some mairie may only have opening periods defined into openinghours (e.g. Bron)
for days_list, time_table in parse_opening_hours_data(data): for days_list, time_table in parse_opening_hours_data(data):
known_format = True known_format = True