diff --git a/combo_plugin_gnm/templatetags/gnm.py b/combo_plugin_gnm/templatetags/gnm.py index a340add..7eb8b9f 100644 --- a/combo_plugin_gnm/templatetags/gnm.py +++ b/combo_plugin_gnm/templatetags/gnm.py @@ -32,7 +32,7 @@ from django.conf import settings from django.core.serializers.json import DjangoJSONEncoder from django.core import signing from django.utils import six -from django.utils.dateparse import parse_datetime +from django.utils.dateparse import parse_date, parse_datetime from django.utils.http import quote from django.utils.html import format_html from django.utils.text import slugify @@ -199,8 +199,11 @@ def get_slots_from_mairie_format(data, base_datetime): known_format = True # prepare annual opening exclusions for specification in data.get('openinghoursspecification', []): - valid_from = parse_datetime(specification.get('validFrom')) if specification.get('validFrom') else previous_week - valid_through = parse_datetime(specification.get('validThrough')) if specification.get('validThrough') else next_week + 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 if specification.get('opens') and specification.get('closes'): @@ -232,6 +235,20 @@ def get_slots_from_mairie_format(data, base_datetime): return (slots, exclusion_slots, known_format) +def parse_valid_from(spec): + valid_from = parse_datetime(spec.get('validFrom')) or parse_date(spec.get('validFrom')) + if not isinstance(valid_from, datetime.datetime): + valid_from = datetime.datetime(valid_from.year, valid_from.month, valid_from.day) + return valid_from + + +def parse_valid_through(spec): + valid_through = parse_datetime(spec.get('validThrough')) or parse_date(spec.get('validThrough')) + if not isinstance(valid_through, datetime.datetime): + valid_through = datetime.datetime(valid_through.year, valid_through.month, valid_through.day, 23, 59) + return valid_through + + @register.assignment_tag def get_mairie_opening_hours(mairie_data): """Process Mairie Geojson to extract data of each day's opening hours @@ -262,8 +279,11 @@ def get_mairie_opening_hours(mairie_data): previous_week = base_datetime - datetime.timedelta(7) next_week = base_datetime + datetime.timedelta(7) for specification in mairie_data.get('openinghoursspecification', []): - valid_from = parse_datetime(specification.get('validFrom')) if specification.get('validFrom') else previous_week - valid_through = parse_datetime(specification.get('validThrough')) if specification.get('validThrough') else next_week + 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 diff --git a/tests/data/mairie-geojson.json b/tests/data/mairie-geojson.json index 3b798da..8e8fa14 100644 --- a/tests/data/mairie-geojson.json +++ b/tests/data/mairie-geojson.json @@ -159,8 +159,8 @@ "openinghours": ["Mo-Fr 13:30-17:30", "Mo-Fr 08:30-12:00"], "openinghoursspecification": [{ "dayOfWeek": "http:\/\/schema.org\/Monday", - "validFrom": "2018-01-01T00:00:00+01:00", - "validThrough": "2018-01-01T23:59:59+01:00" + "validFrom": "2018-01-01", + "validThrough": "2018-01-01" }, { "dayOfWeek": "http:\/\/schema.org\/Monday", "validFrom": "2018-04-02T00:00:00+02:00", diff --git a/tests/test_as_opening_hours.py b/tests/test_as_opening_hours.py index 98f61cc..1b67c9c 100644 --- a/tests/test_as_opening_hours.py +++ b/tests/test_as_opening_hours.py @@ -152,3 +152,16 @@ def test_mdr_just_closed(): test_html = as_opening_hours_badge(MDR_GEOJSON[0]) klass, label = 'closed', u"Réouvre lundi à 8h30" assert test_html == mark_safe(u'
%s
' % (klass, label)) + + +@pytest.mark.freeze_time("2018-01-01 14:59:00") +def test_mairie_holiday(): + # Ecully, using datetimes + test_html = [as_opening_hours_badge(x) for x in GEOJSON if x['properties']['identifiant'] == 'S1361'][0] + klass, label = 'closed', u"Réouvre demain à 8h30" + assert test_html == mark_safe(u'
%s
' % (klass, label)) + + # Feyzin, using dates + test_html = [as_opening_hours_badge(x) for x in GEOJSON if x['properties']['identifiant'] == 'S1365'][0] + klass, label = 'closed', u"Réouvre demain à 8h30" + assert test_html == mark_safe(u'
%s
' % (klass, label))