From adbbbac39238636e22132158f0b0767d329303c7 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) --- combo/apps/calendar/models.py | 2 +- .../templates/calendar/booking_calendar_cell.html | 7 +++- combo/apps/calendar/utils.py | 38 ++++++++++++++++++---- tests/test_calendar.py | 22 +++++++++++-- 4 files changed, 58 insertions(+), 11 deletions(-) diff --git a/combo/apps/calendar/models.py b/combo/apps/calendar/models.py index d1906c1..867cff6 100644 --- a/combo/apps/calendar/models.py +++ b/combo/apps/calendar/models.py @@ -54,6 +54,6 @@ class BookingCalendar(CellBase): def render(self, context): calendar = get_calendar(self.agenda_reference, self.slot_duration, - self.displayed_days) + self.displayed_days, self.minimal_booking_duration) context['calendar'] = calendar return super(BookingCalendar, self).render(context) diff --git a/combo/apps/calendar/templates/calendar/booking_calendar_cell.html b/combo/apps/calendar/templates/calendar/booking_calendar_cell.html index c7e814a..214193e 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 9917d67..144d5e5 100644 --- a/combo/apps/calendar/utils.py +++ b/combo/apps/calendar/utils.py @@ -14,12 +14,15 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -import urllib import datetime +import urllib from django.conf import settings +from django.utils.dateformat import DateFormat from django.utils.dateparse import parse_datetime -from django.utils.timezone import now +from django.utils.formats import get_format +from django.utils.timezone import localtime, make_aware, now +from django.utils.translation import ugettext_lazy as _ from combo.utils import requests @@ -76,11 +79,11 @@ def get_chrono_events(agenda_reference): return result.get('data', []) -def get_calendar(agenda_reference, offset, size): +def get_calendar(agenda_reference, offset, size, min_duration): if not agenda_reference: return [] events = get_chrono_events(agenda_reference) - weekcal = Calendar(offset, size) + weekcal = Calendar(offset, min_duration) for event in events: event_datetime = parse_datetime(event['datetime']) event_date = event_datetime.date() @@ -110,13 +113,17 @@ 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): + date = DateFormat(self.date_time).format(get_format('DATE_FORMAT')) + return _('%s at %s') % (date, self.date_time.strftime('%H:%M')) + @property def label(self): return '%s' % self.date_time.isoformat() @@ -148,19 +155,38 @@ class CalDay(object): def get_maximum_slot(self): return max(self.slots, key=lambda x: x.date_time.time()) + def get_first_available_slot(self, offset, min_duration): + step = min_duration.seconds / offset.seconds + for idx in range(len(self.slots)): + tmp = self.slots[idx:idx + step] + if (tmp[-1].date_time - tmp[0].date_time) != ((step - 1) * offset): + continue + if not all(map(lambda x: x.available, tmp)): + continue + return tmp[0] + return None + class Calendar(object): - def __init__(self, offset, size): + def __init__(self, offset, size, min_duration): self.offset = offset self.days = [] self.size = size + self.min_duration = min_duration def __repr__(self): if self.days: return ' %s >' % (self.days[0], self.days[-1]) return '' + def get_info(self): + for day in self.days: + first_available_slot = day.get_first_available_slot(self.offset, self.min_duration) + if first_available_slot: + return _('(Next available slot: %s)') % first_available_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..7714720 100644 --- a/tests/test_calendar.py +++ b/tests/test_calendar.py @@ -215,13 +215,13 @@ 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) def test_calendar(mocked_get, cell): - cal = get_calendar('default:whatever', cell.slot_duration) + cal = get_calendar('default:whatever', cell.slot_duration, cell.minimal_booking_duration) assert len(cal.days) == 3 for day in cal.get_days(): assert day in [ @@ -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: 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