From fc5e9a0eb8b2788a216d882d450ec2c44b8b175b Mon Sep 17 00:00:00 2001 From: Josue Kouka Date: Wed, 17 May 2017 18:19:07 +0200 Subject: [PATCH] add calendar cell model (#16393) --- combo/apps/chrono/README | 18 ++++ combo/apps/chrono/__init__.py | 28 +++++++ combo/apps/chrono/migrations/0001_initial.py | 44 ++++++++++ combo/apps/chrono/migrations/__init__.py | 0 combo/apps/chrono/models.py | 69 +++++++++++++++ combo/apps/chrono/static/chrono/js/calendar.js | 91 ++++++++++++++++++++ combo/apps/chrono/templates/chrono/calendar.html | 13 +++ combo/apps/chrono/urls.py | 24 ++++++ combo/apps/chrono/utils.py | 62 ++++++++++++++ combo/apps/chrono/views.py | 54 ++++++++++++ combo/settings.py | 2 + debian/control | 1 + setup.py | 1 + tests/test_calendar.py | 102 +++++++++++++++++++++++ 14 files changed, 509 insertions(+) create mode 100644 combo/apps/chrono/README create mode 100644 combo/apps/chrono/__init__.py create mode 100644 combo/apps/chrono/migrations/0001_initial.py create mode 100644 combo/apps/chrono/migrations/__init__.py create mode 100644 combo/apps/chrono/models.py create mode 100644 combo/apps/chrono/static/chrono/js/calendar.js create mode 100644 combo/apps/chrono/templates/chrono/calendar.html create mode 100644 combo/apps/chrono/urls.py create mode 100644 combo/apps/chrono/utils.py create mode 100644 combo/apps/chrono/views.py create mode 100644 tests/test_calendar.py diff --git a/combo/apps/chrono/README b/combo/apps/chrono/README new file mode 100644 index 0000000..c4f0bf0 --- /dev/null +++ b/combo/apps/chrono/README @@ -0,0 +1,18 @@ +Combo calendar cell +================= + +To be visible, this cell needs a 'chrono' entry in settings.KNOWN_SERVICES + +Example of session_var definition + +{ + "nusery_id": "southpark", + "date_debut": "$start", + "date_fin": "$end" +} + +$start and $end will be replaced by event start_datetime and end_datetime +and all keys in the session_vars dick will be prefixed by a . +The result will be: + +session_var_nursery_id=southapark&dsession_var_date_debut=2017-05-19T13:30:00&session_var_date_fin=2017-05-19T15:30:00 diff --git a/combo/apps/chrono/__init__.py b/combo/apps/chrono/__init__.py new file mode 100644 index 0000000..feef274 --- /dev/null +++ b/combo/apps/chrono/__init__.py @@ -0,0 +1,28 @@ +# combo - content management system +# Copyright (C) 2017 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import django.apps + + +class AppConfig(django.apps.AppConfig): + name = 'combo.apps.chrono' + + def get_before_urls(self): + from . import urls + return urls.urlpatterns + + +default_app_config = 'combo.apps.chrono.AppConfig' diff --git a/combo/apps/chrono/migrations/0001_initial.py b/combo/apps/chrono/migrations/0001_initial.py new file mode 100644 index 0000000..d8e9fae --- /dev/null +++ b/combo/apps/chrono/migrations/0001_initial.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +import jsonfield.fields +import datetime + + +class Migration(migrations.Migration): + + dependencies = [ + ('auth', '0006_require_contenttypes_0002'), + ('data', '0025_jsoncell_varnames_str'), + ] + + operations = [ + migrations.CreateModel( + name='CalendarCell', + fields=[ + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), + ('placeholder', models.CharField(max_length=20)), + ('order', models.PositiveIntegerField()), + ('slug', models.SlugField(verbose_name='Slug', blank=True)), + ('extra_css_class', models.CharField(max_length=100, verbose_name='Extra classes for CSS styling', blank=True)), + ('public', models.BooleanField(default=True, verbose_name='Public')), + ('restricted_to_unlogged', models.BooleanField(default=False, verbose_name='Restrict to unlogged users')), + ('last_update_timestamp', models.DateTimeField(auto_now=True)), + ('title', models.CharField(max_length=128, verbose_name='Title')), + ('events_source', models.URLField(verbose_name='Events source URL')), + ('form_url', models.URLField(verbose_name='Application form URL')), + ('session_vars', jsonfield.fields.JSONField(default=dict, verbose_name='Session vars')), + ('slot_duration', models.TimeField(default=datetime.time(0, 30), verbose_name='Slot duration')), + ('minimal_event_duration', models.TimeField(default=datetime.time(2, 0), verbose_name='Minimal event duration')), + ('business_hours_start', models.TimeField(default=datetime.time(8, 0), verbose_name='Business hour start')), + ('business_hours_end', models.TimeField(default=datetime.time(18, 0), verbose_name='Business hour end')), + ('event_default_title', models.CharField(max_length=32, verbose_name='Event default title')), + ('groups', models.ManyToManyField(to='auth.Group', verbose_name='Groups', blank=True)), + ('page', models.ForeignKey(to='data.Page')), + ], + options={ + 'verbose_name': 'Calendar Cell', + }, + ), + ] diff --git a/combo/apps/chrono/migrations/__init__.py b/combo/apps/chrono/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/combo/apps/chrono/models.py b/combo/apps/chrono/models.py new file mode 100644 index 0000000..8f6fa0b --- /dev/null +++ b/combo/apps/chrono/models.py @@ -0,0 +1,69 @@ +# combo - content management system +# Copyright (C) 2017 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from django.db import models +from django.utils.translation import ugettext_lazy as _ +from django.utils.dateparse import parse_time + +from combo.data.models import CellBase +from combo.data.library import register_cell_class +from .utils import is_chrono_enabled + +from jsonfield import JSONField + + +@register_cell_class +class CalendarCell(CellBase): + + title = models.CharField(_('Title'), max_length=128) + events_source = models.URLField(_('Events source URL')) + form_url = models.URLField(_('Application form URL')) + session_vars = JSONField(_('Session vars')) + slot_duration = models.TimeField( + _('Slot duration'), default=parse_time('00:30')) + minimal_event_duration = models.TimeField( + _('Minimal event duration'), default=parse_time('02:00')) + business_hours_start = models.TimeField( + _('Business hour start'), default=parse_time('08:00')) + business_hours_end = models.TimeField( + _('Business hour end'), default=parse_time('18:00')) + event_default_title = models.CharField( + _('Event default title'), max_length=32) + + template_name = 'chrono/calendar.html' + + class Meta: + verbose_name = _('Calendar Cell') + + class Media: + js = ( + 'xstatic/lib/moment.min.js', + 'xstatic/fullcalendar.min.js', + 'xstatic/locale-all.js', + 'chrono/js/calendar.js' + ) + css = {'all': ('xstatic/fullcalendar.min.css',)} + + @classmethod + def is_enabled(cls): + return is_chrono_enabled() + + def render(self, context): + include = ['title', 'slot_duration', 'business_hours_start', 'business_hours_end', + 'minimal_event_duration', 'event_default_title'] + context['chrono'] = json.dumps({key: str(value) for key, value in vars(self).items() + if key in include}) + return super(CalendarCell, self).render(context) diff --git a/combo/apps/chrono/static/chrono/js/calendar.js b/combo/apps/chrono/static/chrono/js/calendar.js new file mode 100644 index 0000000..1349da3 --- /dev/null +++ b/combo/apps/chrono/static/chrono/js/calendar.js @@ -0,0 +1,91 @@ +$(function() { + + function renderCal(cal_id){ + $(cal_id).fullCalendar({ + locale: locale, + header: { + left: "prev,next today", + center: "title", + right: "agendaWeek,agendaDay" + }, + defaultView: "agendaWeek", + weekends: false, + allDaySlot: false, + slotDuration: chrono.slot_duration, + forceEventDuration: true, + defaultTimedEventDuration: chrono.default_event_duration, + scrollTime: "08:00", + businessHours: { + start: chrono.business_hours_start, + end: chrono.business_hours_end + }, + events: events_source_url, + eventOverlap: function(stillEvent, movingEvent) { + return true; + }, + editable: true, + selectable: true, + selectHelper: true, + unselectAuto: false, + selectConstraint: { + start: chrono.business_hours_start, + end: chrono.business_hours_end, + id: 'available' + }, + select: function(start, end) { + // can't select in the past + if (start.isBefore(moment())){ + $(cal_id).fullCalendar("unselect"); + return false; + } + var title = chrono.event_default_title; + var eventData; + if (title && title.trim()) { + eventData = { + title: title, + start: start, + end: end + }; + $(cal_id).fullCalendar("renderEvent", eventData); + } + $(cal_id).fullCalendar("unselect"); + }, + eventRender: function(event, element) { + var start = moment(event.start).fromNow(); + element.attr("title", start); + }, + loading: function() { + }, + eventClick: function(calEvent, jsEvent, view){ + if (calEvent.source){ + console.log(calEvent.source); + return false; + } + params = { + start: calEvent.start.format(), + end: calEvent.end.format() + } + form_url = events_booking_url + "?" + $.param(params) + $.ajax({ + type: "POST", + url: events_booking_url, + data: JSON.stringify({ + start: calEvent.start.format(), + end: calEvent.end.format() + }), + dataType: "json", + success: function(response){ + console.log(response.url); + window.location = response.url; + }, + error: function(xhr, status, error){ + console.log(JSON.stringify(status)); + } + }) + }, + }); + } + $("div[data-calendar]").each(function(i, cal){ + renderCal("#" + cal.id); + }); +}); diff --git a/combo/apps/chrono/templates/chrono/calendar.html b/combo/apps/chrono/templates/chrono/calendar.html new file mode 100644 index 0000000..5735fce --- /dev/null +++ b/combo/apps/chrono/templates/chrono/calendar.html @@ -0,0 +1,13 @@ +{% load i18n %} + +

{{title}}

+ + + +
+
diff --git a/combo/apps/chrono/urls.py b/combo/apps/chrono/urls.py new file mode 100644 index 0000000..33ff5a6 --- /dev/null +++ b/combo/apps/chrono/urls.py @@ -0,0 +1,24 @@ +# combo - content management system +# Copyright (C) 2017 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from django.conf.urls import url + +from .views import EventsView, BookingView + +urlpatterns = [ + url(r'^chrono/events/(?P[\w,-]+)/', EventsView.as_view(), name='chrono-events'), + url(r'^chrono/book/(?P[\w,-]+)/', BookingView.as_view(), name='chrono-booking'), +] diff --git a/combo/apps/chrono/utils.py b/combo/apps/chrono/utils.py new file mode 100644 index 0000000..2d49353 --- /dev/null +++ b/combo/apps/chrono/utils.py @@ -0,0 +1,62 @@ +# combo - content management system +# Copyright (C) 2017 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from django.conf import settings +from combo.utils import requests + + +def get_chrono_service(): + if hasattr(settings, 'KNOWN_SERVICES') and settings.KNOWN_SERVICES.get('chrono'): + return settings.KNOWN_SERVICES['chrono'].values()[0] + + +def is_chrono_enabled(): + return get_chrono_service() + + +def convert_widget_format(events): + data = [] + for event in events.get('data'): + data.append({ + 'id': 'available', + 'title': event.get('text'), + 'start': event.get('datetime'), + 'editable': False, + 'durationEditable': False, + 'rendering': 'inverse-background' + }) + return data + + +def get_chrono_events(chrono_url, **kwargs): + response = requests.get( + chrono_url, + headers={'accept': 'application/json'}, + **kwargs) + data = convert_widget_format(response.json()) + return data + + +def build_session_vars(cell, data): + session_vars = {} + for key, value in cell.session_vars.items(): + key = 'session_var_%s' % key + value = value.strip('$') + if data.get(value): + session_vars.update({key: data[value]}) + else: + session_vars.update({key: value}) + return session_vars diff --git a/combo/apps/chrono/views.py b/combo/apps/chrono/views.py new file mode 100644 index 0000000..9de96da --- /dev/null +++ b/combo/apps/chrono/views.py @@ -0,0 +1,54 @@ +# combo - content management system +# Copyright (C) 2017 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import json +import urllib + +from django.http import JsonResponse +from django.views.decorators.csrf import csrf_exempt +from django.views.generic import View +from django.views.generic.detail import SingleObjectMixin + +from .models import CalendarCell +from .utils import get_chrono_events, build_session_vars + + +class EventsView(SingleObjectMixin, View): + + http_method_names = ['get'] + model = CalendarCell + + def get(self, request, *args, **kwargs): + cell = self.get_object() + data = get_chrono_events(cell.events_source) + return JsonResponse(data, safe=False) + + +class BookingView(SingleObjectMixin, View): + + http_method_names = ['post'] + model = CalendarCell + + @csrf_exempt + def dispatch(self, request, *args, **kwargs): + return super(BookingView, self).dispatch(request, *args, **kwargs) + + def post(self, request, *args, **kwargs): + data = json.loads(request.body) + cell = self.get_object() + params = build_session_vars(cell, data) + url = '%s?%s' % (cell.form_url, urllib.urlencode(params)) + return JsonResponse({'url': url}, safe=False) diff --git a/combo/settings.py b/combo/settings.py index 0f0247d..f1bdfb5 100644 --- a/combo/settings.py +++ b/combo/settings.py @@ -75,8 +75,10 @@ INSTALLED_APPS = ( 'combo.apps.notifications', 'combo.apps.search', 'combo.apps.usersearch', + 'combo.apps.chrono', 'haystack', 'xstatic.pkg.chartnew_js', + 'xstatic.pkg.fullcalendar', ) INSTALLED_APPS = plugins.register_plugins_apps(INSTALLED_APPS) diff --git a/debian/control b/debian/control index 1ef3ef8..a1cfa59 100644 --- a/debian/control +++ b/debian/control @@ -18,6 +18,7 @@ Depends: ${misc:Depends}, ${python:Depends}, python-xstatic-chartnew-js, python-eopayment (>= 1.9), python-django-haystack (>= 2.4.0) + python-xstatic-fullcalendar (>= 3.4.0) Recommends: python-django-mellon, python-whoosh Conflicts: python-lingo Description: Portal Management System (Python module) diff --git a/setup.py b/setup.py index 6a7351e..e484716 100644 --- a/setup.py +++ b/setup.py @@ -116,6 +116,7 @@ setup( 'djangorestframework>=3.3, <3.4', 'django-haystack', 'whoosh', + 'XStatic-fullcalendar>=3.4.0' ], zip_safe=False, cmdclass={ diff --git a/tests/test_calendar.py b/tests/test_calendar.py new file mode 100644 index 0000000..d3f9369 --- /dev/null +++ b/tests/test_calendar.py @@ -0,0 +1,102 @@ +import json +import urlparse + +import pytest +import mock + +from django.utils.dateparse import parse_time + +from combo.data.models import Page +from combo.apps.chrono.models import CalendarCell + +pytestmark = pytest.mark.django_db + +CHRONO_EVENTS = { + "data": [ + { + "disabled": True, + "text": "19 mai 2017 08:00", + "api": { + "fillslot_url": "http://example.net/api/agenda/whatever/fillslot/86/" + }, + "id": 86, + "datetime": "2017-05-19 08:00:00" + }, + { + "disabled": True, + "text": "19 mai 2017 08:30", + "api": { + "fillslot_url": "http://example.net/api/agenda/whatever/fillslot/87/" + }, + "id": 87, + "datetime": "2017-05-19 08:30:00" + }, + { + "disabled": True, + "text": "19 mai 2017 09:00", + "api": { + "fillslot_url": "http://example.net/api/agenda/whatever/fillslot/88/" + }, + "id": 88, + "datetime": "2017-05-19 09:00:00" + }, + { + "disabled": True, + "text": "19 mai 2017 09:30", + "api": { + "fillslot_url": "http://example.net/api/agenda/whatever/fillslot/89/" + }, + "id": 89, + "datetime": "2017-05-19 09:30:00" + } + ] +} + + +class MockedRequestResponse(mock.Mock): + + def json(self): + return json.loads(self.content) + + +@pytest.fixture +def cell(db): + page = Page.objects.create() + cell = CalendarCell( + page=page, title='Example Of Calendar', order=1, + events_source='http://example.net/api/events/', + form_url='http://example.net/form/whatever/', + session_vars={ + "start_dt": "$start", "end_dt": "$end", + "whatever_slug": "whatever" + }, + slot_duration=parse_time('00:30'), + business_hours_start=parse_time('08:00'), + business_hours_end=parse_time('18:00'), + event_default_title='Available' + ) + cell.save() + return cell + + +@mock.patch('combo.apps.chrono.utils.requests.get') +def test_get_events(mocked_get, app, cell): + mocked_get.return_value = MockedRequestResponse(content=json.dumps(CHRONO_EVENTS)) + resp = app.get('/chrono/events/%s/' % cell.pk) + assert len(resp.json) == 4 + for datum in resp.json: + assert datum['id'] == 'available' + assert datum['rendering'] == 'inverse-background' + + +def test_redirection_session_vars(app, cell, settings): + params = { + 'start': '2017-05-19T10:30:23', + 'end': '2017-05-19T12:30:14', + } + resp = app.post_json('/chrono/book/%s/' % cell.pk, params=params) + parsed = urlparse.urlparse(resp.json['url']) + qs = urlparse.parse_qs(parsed.query) + assert qs['session_var_whatever_slug'] == ['whatever'] + assert qs['session_var_start_dt'] == [params['start']] + assert qs['session_var_end_dt'] == [params['end']] -- 2.11.0