templatetags: use openinghoursspecification as default format (#50404)

This commit is contained in:
Nicolas Roche 2021-01-21 15:35:53 +01:00
parent 045a98a06c
commit 8e99bea02f
4 changed files with 1267 additions and 28 deletions

View File

@ -260,8 +260,7 @@ def get_slots_from_mairie_format(data, base_datetime):
exclusion_slots = []
previous_week = base_datetime - datetime.timedelta(7)
next_week = base_datetime + datetime.timedelta(7)
if len(data.get('openinghours', [])) or len(data.get('openinghoursspecification', [])):
known_format = True
if len(data.get('openinghoursspecification', [])) > 0:
# prepare annual opening exclusions
for specification in data.get('openinghoursspecification', []):
valid_from, valid_through = previous_week, next_week
@ -274,6 +273,7 @@ def get_slots_from_mairie_format(data, base_datetime):
if specification.get('opens') and specification.get('closes'):
# case when opening periods are defined
if valid_from <= base_datetime < valid_through:
known_format = True
try:
day_number, time_table = get_time_table_from_specification(specification)
except ValueError:
@ -284,15 +284,17 @@ def get_slots_from_mairie_format(data, base_datetime):
# case when exclusions are defined
exclusion_slots.append(TimeSlot(valid_from, valid_through))
if not known_format and len(data.get('openinghours', [])) > 0:
for days_list, time_table in parse_opening_hours_data(data):
known_format = True
for weekday in days_list:
day_number = EN_ABBREV_WEEKDAYS_LIST.index(weekday)
timeslot = get_slot(day_number, time_table, base_datetime)
# add to slots the opening hours in chronological order beginning from today
slots.append(timeslot)
# order slots and cycle the list beginning with 'base_datetime'
slots.sort(key=operator.attrgetter('start'))
# order slots and cycle the list beginning with 'base_datetime'
slots.sort(key=operator.attrgetter('start'))
return (slots, exclusion_slots, known_format)
@ -340,31 +342,31 @@ def get_mairie_opening_hours(mairie_data):
)
known_format = False
for days_list, time_table in parse_opening_hours_data(mairie_data):
known_format = True
for weekday in days_list:
update_opening_hours(weekday, time_table)
previous_week = base_datetime - datetime.timedelta(7)
next_week = base_datetime + datetime.timedelta(7)
for specification in mairie_data.get('openinghoursspecification', []):
valid_from, valid_through = previous_week, next_week
if specification.get('validFrom'):
valid_from = parse_valid_from(specification)
if specification.get('validThrough'):
valid_through = parse_valid_through(specification)
if not valid_from or not valid_through:
continue
# case when opening periods are defined
if 'opens' in specification and 'closes' in specification:
known_format = True
# parse specification only for the current period relative to utcnow()
if valid_from < base_datetime < valid_through:
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 known_format:
# some mairie only have openinghoursspecification (e.g. Jonage)
previous_week = base_datetime - datetime.timedelta(7)
next_week = base_datetime + datetime.timedelta(7)
for specification in mairie_data.get('openinghoursspecification', []):
# some mairie may only have opening periods defined into openinghours (e.g. Bron)
for days_list, time_table in parse_opening_hours_data(mairie_data):
known_format = True
valid_from, valid_through = previous_week, next_week
if specification.get('validFrom'):
valid_from = parse_valid_from(specification)
if specification.get('validThrough'):
valid_through = parse_valid_through(specification)
if not valid_from or not valid_through:
continue
# 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 valid_from < base_datetime < valid_through:
day_number, time_table = get_time_table_from_specification(specification)
weekday = EN_ABBREV_WEEKDAYS_LIST[day_number]
update_opening_hours(weekday, time_table)
for weekday in days_list:
update_opening_hours(weekday, time_table)
if not (
any([x['am'] for x in opening_hours_dict.values()])

File diff suppressed because it is too large Load Diff

View File

@ -23,6 +23,7 @@ def test_every_mairie_closed():
assert len([x for x in opening_hours if 'open' in x]) == 0
@pytest.mark.freeze_time("2018-03-04 23:00:00")
def test_all_mairie_data_parsed_correct():
"""everything got parsed correctly"""
opening_hours = [
@ -274,3 +275,36 @@ def test_mairie_format_openinghoursspecification(day, opens, closes, badge, text
)
html = as_opening_hours_badge(json.loads(geojson))
assert html == '<div class="badge %s"><span>%s</span></div>' % (badge, text)
@pytest.mark.freeze_time("2018-03-09 00:30:00")
def test_mairie_having_both_formats():
""" openinghoursspecification take preference over openinghours"""
geojson = """
{
"properties": {
"openinghours": [
"Mo 08:30-11:30"
],
"openinghoursspecification": [{
"opens": "09:30",
"closes": "12:30",
"dayOfWeek": "http:\/\/schema.org\/Monday",
"validFrom": "2018-01-01T00:00:00+01:00",
"validThrough": "2018-06-30T23:59:59+02:00"
}]
}
}
"""
html = as_opening_hours_badge(json.loads(geojson))
assert html == '<div class="badge %s"><span>%s</span></div>' % (
'closed', 'Réouvre lundi à 9h30')
@pytest.mark.freeze_time("2021-01-21 15:37:00")
def test_mairie_saint_genis_lavak():
""" #50337 """
geojson = json.load(open(os.path.join(BASE_DIR, 'tests/data/mairie-saint-genis-lavak.json')))
test_html = as_opening_hours_badge(geojson['features'][0])
klass, label = 'open', u"Ouvert jusqu'à 17h30"
assert test_html == mark_safe(u'<div class="badge %s"><span>%s</span></div>' % (klass, label))

View File

@ -28,7 +28,7 @@ def test_mairie_hours_special_data():
"""test results for various data examples"""
for x in GEOJSON:
if x['properties']['identifiant'] == 'S1376':
# La Mulatière
# La Mulatière : openinghoursspecification set but without opens/closes
assert get_mairie_opening_hours(x) == [
('lundi', {'am': u'08h30-12h00', 'pm': None}),
('mardi', {'am': u'08h30-12h30', 'pm': u'13h30-17h00'}),
@ -39,6 +39,7 @@ def test_mairie_hours_special_data():
]
if x['properties']['identifiant'] == 'S1437':
x['properties']['openinghoursspecification'] = [] # force using openinghours
# special openinghours format with days intervals, comma-separated list and one day definition with a saturday
assert get_mairie_opening_hours(x) == [
('lundi', {'am': u'08h45-12h30', 'pm': u'14h00-16h45'}),
@ -50,6 +51,7 @@ def test_mairie_hours_special_data():
]
if x['properties']['identifiant'] == 'S5564':
x['properties']['openinghoursspecification'] = [] # force using openinghours
# classic openinghours days interval for am and pm
assert get_mairie_opening_hours(x) == [
('lundi', {'am': u'08h30-12h15', 'pm': u'13h15-17h00'}),
@ -124,6 +126,7 @@ def test_mairie_saint_priest():
def test_mairie_format_openinghours():
""" some mairie may still only define the openinghours format """
geojson = """
{
"properties": {
@ -151,7 +154,7 @@ def test_mairie_format_openinghours():
@pytest.mark.freeze_time("2018-03-09 00:30:00")
def test_mairie_format_openinghoursspecification():
""" some mairie only have openinghoursspecification """
""" openinghoursspecification the default format """
geojson = """
{
"properties": {
@ -209,3 +212,41 @@ def test_mairie_format_openinghoursspecification():
('samedi', {'am': None, 'pm': '21h00-24h00'}),
('dimanche', {'am': '00h00-24h00', 'pm': ''}),
]
@pytest.mark.freeze_time("2018-03-09 00:30:00")
def test_mairie_having_both_formats():
""" openinghoursspecification take preference over openinghours"""
geojson = """
{
"properties": {
"openinghours": [
"Mo 08:30-11:30"
],
"openinghoursspecification": [{
"opens": "09:30",
"closes": "12:30",
"dayOfWeek": "http:\/\/schema.org\/Monday",
"validFrom": "2018-01-01T00:00:00+01:00",
"validThrough": "2018-06-30T23:59:59+02:00"
}]
}
}
"""
hours = get_mairie_opening_hours(json.loads(geojson))
assert hours[0] == ('lundi', {'am': '09h30-12h30', 'pm': None})
@pytest.mark.freeze_time("2021-01-21 15:37:00")
def test_mairie_saint_genis_lavak():
""" #50337 """
geojson = json.load(open(os.path.join(BASE_DIR, 'tests/data/mairie-saint-genis-lavak.json')))
test_time_table = get_mairie_opening_hours(geojson['features'][0])
assert test_time_table == [
('lundi', {'am': '08h30-12h00', 'pm': '13h30-17h30'}),
('mardi', {'am': '08h30-12h00', 'pm': '13h30-17h30'}),
('mercredi', {'am': '08h30-12h00', 'pm': None}),
('jeudi', {'am': '08h30-12h00', 'pm': '13h30-17h30'}),
('vendredi', {'am': '08h30-12h00', 'pm': '13h30-17h30'}),
('samedi', {'am': '09h00-12h00', 'pm': None})
]