templatetags: add support for openinghoursspecification-only entries (#22050)

This commit is contained in:
Frédéric Péters 2018-03-10 17:16:54 +01:00
parent 622f15044a
commit 7873a12535
2 changed files with 31 additions and 5 deletions

View File

@ -113,8 +113,9 @@ def get_slots_from_mairie_format(data, base_datetime):
known_format = False
slots = []
exclusion_slots = []
if len(data.get('openinghours', [])) and len(data.get('openinghoursspecification', [])):
if len(data.get('openinghours', [])) or len(data.get('openinghoursspecification', [])):
known_format = True
WEEKDAYS = EN_ABBREV_WEEKDAYS.values()
# prepare annual opening exclusions
for specification in data.get('openinghoursspecification', []):
valid_from, valid_through = (
@ -126,7 +127,16 @@ def get_slots_from_mairie_format(data, base_datetime):
if 'opens' in specification and 'closes' in specification:
# case when opening periods are defined
if base_datetime >= valid_from and base_datetime < valid_through:
slots.append(TimeSlot(dateutil_parse(specification['opens']), dateutil_parse(specification['closes'])))
opening_time = datetime.datetime.combine(base_datetime, dateutil_parse(specification['opens']).time())
closing_time = datetime.datetime.combine(base_datetime, dateutil_parse(specification['closes']).time())
opening_time = opening_time.replace(tzinfo=valid_from.tzinfo)
closing_time = closing_time.replace(tzinfo=valid_from.tzinfo)
day_of_week = WEEKDAYS.index(specification['dayOfWeek'].split('/')[-1])
opening_time = opening_time + datetime.timedelta(
days=(7 + (day_of_week - opening_time.weekday())) % 7)
closing_time = closing_time + datetime.timedelta(
days=(7 + (day_of_week - closing_time.weekday())) % 7)
slots.append(TimeSlot(opening_time, closing_time))
else:
# case when exclusions are defined
exclusion_slots.append(TimeSlot(valid_from, valid_through))

View File

@ -4,6 +4,7 @@ import json
import os
from django.utils.safestring import mark_safe
from django.utils.timezone import is_naive, make_aware
from combo_plugin_gnm.templatetags.gnm import as_opening_hours_badge
@ -25,9 +26,7 @@ def test_all_mairie_data_parsed_correct():
"""everything got parsed correctly"""
opening_hours = [as_opening_hours_badge(x) for x in GEOJSON
if x['properties'].get('openinghours') or x['properties'].get('openinghoursspecification')]
# actually not everything, Jonage and Solaize are not handled correctly as
# they only use openinghoursspecification → 2
assert opening_hours.count('') == 2 # FIXME
assert opening_hours.count('') == 0
def test_mairie_nodata_as_opening_hours_templatetag():
@ -37,6 +36,23 @@ def test_mairie_nodata_as_opening_hours_templatetag():
assert test_html == ''
def test_jonage():
test_datetime = parse("2018-03-05T15:59:00+01:00")
test_html = [as_opening_hours_badge(x, base_datetime=test_datetime) for x in GEOJSON if x['properties']['nom'] == 'Mairie de Jonage'][0]
klass, label = 'open', u"Ouvert jusqu'à 17h00"
assert test_html == mark_safe(u'<div class="badge %s"><span>%s</span></div>'% (klass, label))
test_datetime = parse("2018-03-10T16:30:00+01:00")
test_html = [as_opening_hours_badge(x, base_datetime=test_datetime) for x in GEOJSON if x['properties']['nom'] == 'Mairie de Jonage'][0]
klass, label = 'soon-to-be-closed', u"Ouvert jusqu'à 17h00"
assert test_html == mark_safe(u'<div class="badge %s"><span>%s</span></div>'% (klass, label))
test_datetime = parse("2018-03-10T17:30:00+01:00")
test_html = [as_opening_hours_badge(x, base_datetime=test_datetime) for x in GEOJSON if x['properties']['nom'] == 'Mairie de Jonage'][0]
klass, label = 'closed', u"Réouvre lundi à 8h30"
assert test_html == mark_safe(u'<div class="badge %s"><span>%s</span></div>'% (klass, label))
def test_mairie_open_as_opening_hours_templatetag():
"""mairie as_opening_hours with a fixed datetime"""
klass = 'open'