templatetags: manage exceptions on opening hours (#50419)

This commit is contained in:
Nicolas Roche 2021-01-21 19:33:47 +01:00
parent 59632e6b30
commit 7764be274a
2 changed files with 60 additions and 7 deletions

View File

@ -267,10 +267,12 @@ def parse_mairie_formats(data, base_datetime, oh_add, ohs_add, ohs_del):
valid_through = parse_valid_through(specification)
if not valid_from or not valid_through:
continue
# parse specification only for the current period relative to utcnow()
if not valid_from <= base_datetime < valid_through:
if base_datetime > valid_through:
continue
if specification.get('opens') and specification.get('closes'):
# parse specification only for the current period relative to utcnow()
if base_datetime < valid_from:
continue
# case when opening periods are defined
some_opening_periods_defined = True
try:
@ -314,6 +316,7 @@ def get_mairie_opening_hours(mairie_data):
return ''
base_datetime = now()
exclusions = set()
opening_hours_dict = OrderedDict(
zip(EN_ABBREV_WEEKDAYS_LIST, [{'am': None, 'pm': None} for i in range(7)])
)
@ -338,9 +341,17 @@ def get_mairie_opening_hours(mairie_data):
update_opening_hours(weekday, time_table)
def ohs_del(valid_from, valid_through):
pass
day = max(base_datetime, valid_from)
nb_max_days = 7 - max(valid_from.toordinal() - base_datetime.toordinal(), 0)
nb_days = 0
while nb_days < nb_max_days and day < valid_through:
exclusions.add(EN_ABBREV_WEEKDAYS_LIST[day.weekday()])
day += datetime.timedelta(days=1)
nb_days += 1
known_format = parse_mairie_formats(mairie_data, base_datetime, oh_add, ohs_add, ohs_del)
for weekday in exclusions:
opening_hours_dict[weekday] = {'am': None, 'pm': None}
if not (
any([x['am'] for x in opening_hours_dict.values()])

View File

@ -4,7 +4,8 @@ import pytest
import json
import os
from combo_plugin_gnm.templatetags.gnm import get_mairie_opening_hours
from combo_plugin_gnm.templatetags.gnm import (
get_mairie_opening_hours, EN_FULL_WEEKDAYS_LIST, FR_WEEKDAYS)
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
@ -253,13 +254,13 @@ def test_mairie_saint_genis_lavak():
@pytest.mark.freeze_time("2018-01-01 14:59:00")
def test_mairie_holiday():
def test_mairie_holiday_day():
# Ecully, using datetimes
test_time_table = [
get_mairie_opening_hours(x) for x in GEOJSON if x['properties']['identifiant'] == 'S1361'
][0]
assert test_time_table == [
('lundi', {'am': '08h30-12h00', 'pm': '13h30-17h00'}), # but closed on 1/1
('lundi', {'am': None, 'pm': None}),
('mardi', {'am': '08h30-12h00', 'pm': '13h30-17h00'}),
('mercredi', {'am': '08h30-12h00', 'pm': '13h30-17h00'}),
('jeudi', {'am': '08h30-12h00', 'pm': '13h30-17h00'}),
@ -272,9 +273,50 @@ def test_mairie_holiday():
get_mairie_opening_hours(x) for x in GEOJSON if x['properties']['identifiant'] == 'S1365'
][0]
assert test_time_table == [
('lundi', {'am': '08h30-12h00', 'pm': '13h30-17h30'}), # but closed on 1/1
('lundi', {'am': None, 'pm': None}),
('mardi', {'am': '08h30-12h00', 'pm': '13h30-17h30'}),
('mercredi', {'am': '08h30-12h00', 'pm': '13h30-17h30'}),
('jeudi', {'am': '08h30-12h00', 'pm': '13h30-17h30'}),
('vendredi', {'am': '08h30-12h00', 'pm': '13h30-17h30'})
]
@pytest.mark.freeze_time('2018-01-04 14:59:00')
@pytest.mark.parametrize(
'validFrom, validTrought, is_open', [
('2017-12-01', '2018-02-01', [False, False, False, False, False, False, False]),
('2018-01-04', '2018-01-04', [True, True, True, False, True, True, True]),
('2018-01-05', '2018-01-07', [True, True, True, True, False, False, False]),
('2018-01-05', '2018-01-09', [False, False, True, True, False, False, False]),
('2018-01-05', '2018-01-11', [False, False, False, True, False, False, False]),
])
def test_mairie_holiday_period(validFrom, validTrought, is_open):
# buid expected time table
open_day = {'am': '08h30-17h30', 'pm': ''}
closed_day = {'am': None, 'pm': None}
expected = []
if not any(is_open):
expected = [(weekday, {'am': None, 'pm': ''}) for weekday in FR_WEEKDAYS]
else:
for index, weekday in enumerate(FR_WEEKDAYS):
if is_open[index]:
expected.append((weekday, open_day))
elif weekday not in ['samedi', 'dimanche']:
expected.append((weekday, closed_day))
ohs = []
for weekday in EN_FULL_WEEKDAYS_LIST:
ohs.append({
'opens': '08:30',
'closes': '17:30',
'dayOfWeek': 'http://schema.org/%s' % weekday,
'validFrom': '2018-01-01T00:00:00+01:00',
'validThrough': '2018-06-30T23:59:59+01:00'
})
ohs.append({
'validFrom': '%sT00:00:00+01:00' % validFrom,
'validThrough': '%sT23:59:59+01:00' % validTrought,
})
data = {'properties': {'openinghoursspecification': ohs}}
time_table = get_mairie_opening_hours(data)
assert time_table == expected