From 536ac02b14132ccb63e7e5ad4ccc672fc6c7881f Mon Sep 17 00:00:00 2001 From: Josue Kouka Date: Tue, 17 Oct 2017 16:13:45 +0200 Subject: [PATCH] booking calendar cell: display first available slot if any (#19460) --- .../templates/calendar/booking_calendar_cell.html | 7 ++++++- combo/apps/calendar/utils.py | 15 ++++++++++++++- tests/test_calendar.py | 20 ++++++++++++++++++-- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/combo/apps/calendar/templates/calendar/booking_calendar_cell.html b/combo/apps/calendar/templates/calendar/booking_calendar_cell.html index 6f3cc22..56ef074 100644 --- a/combo/apps/calendar/templates/calendar/booking_calendar_cell.html +++ b/combo/apps/calendar/templates/calendar/booking_calendar_cell.html @@ -2,7 +2,12 @@
{% if cell.title %} -

{{cell.title}}

+

+ {{cell.title}} + {% if calendar %} + {{ calendar.get_info }} + {% endif %} +

{% endif %}
{% if calendar.days %} diff --git a/combo/apps/calendar/utils.py b/combo/apps/calendar/utils.py index 1e6be52..db6331e 100644 --- a/combo/apps/calendar/utils.py +++ b/combo/apps/calendar/utils.py @@ -18,7 +18,11 @@ import urllib import datetime from django.conf import settings +from django.utils.dateformat import DateFormat from django.utils.dateparse import parse_datetime +from django.utils.formats import get_format +from django.utils.timezone import localtime, make_aware +from django.utils.translation import ugettext_lazy as _ from combo.utils import requests @@ -109,13 +113,16 @@ def get_form_url_with_params(cell, data): class DaySlot(object): def __init__(self, date_time, available, exist=True): - self.date_time = date_time + self.date_time = localtime(make_aware(date_time)) self.available = available self.exist = exist def __repr__(self): return '' % (self.date_time.isoformat(), self.available) + def __str__(self): + return '%s' % DateFormat(self.date_time).format(get_format('DATETIME_FORMAT')) + @property def label(self): return '%s' % self.date_time.isoformat() @@ -159,6 +166,12 @@ class Calendar(object): return ' %s >' % (self.days[0], self.days[-1]) return '' + def get_info(self): + if self.days: + first_slot = self.days[0].slots[0] + return _('(Next available slot on %s.)') % first_slot + return _('No slot available.') + def get_slots(self): start = self.get_minimum_slot() end = self.get_maximum_slot() diff --git a/tests/test_calendar.py b/tests/test_calendar.py index 2bc6110..2a6ebf4 100644 --- a/tests/test_calendar.py +++ b/tests/test_calendar.py @@ -215,8 +215,8 @@ def test_cell_rendering(mocked_get, client, cell): assert parsed.path == '/test/' qs = urlparse.parse_qs(parsed.query) assert qs['session_var_booking_agenda_slug'] == ['test'] - assert qs['session_var_booking_start'] == ['2017-06-13T08:00:00'] - assert qs['session_var_booking_end'] == ['2017-06-13T09:30:00'] + assert qs['session_var_booking_start'] == ['2017-06-13T08:00:00+00:00'] + assert qs['session_var_booking_end'] == ['2017-06-13T09:30:00+00:00'] @mock.patch('combo.apps.calendar.utils.requests.get', side_effect=mocked_requests_get) @@ -236,3 +236,19 @@ def test_calendar(mocked_get, cell): assert cal.get_minimum_slot() == min_slot.time() assert cal.get_maximum_slot() == max_slot.time() assert cal.get_day(max_slot.date()).slots[-1].available is False + + +@mock.patch('combo.apps.calendar.utils.requests.get', side_effect=mocked_requests_get) +def test_cell_rendering_cal_info(mocked_get, client, cell): + page = client.get('/booking/') + assert 'Next available slot on June 13' in page.content + # test when no slot is available + with mock.patch('combo.utils.requests.get') as request_get: + def side_effect(*args, **kwargs): + if 'chrono' in kwargs['remote_service']['url']: + return MockedRequestResponse(content=json.dumps({"data": []})) + return MockedRequestResponse(content=json.dumps(WCS_FORMDEFS)) + + request_get.side_effect = side_effect + page = client.get('/booking/') + assert 'No slot available.' in page.content -- 2.11.0