From f18ee57c95c415f5f7aeeb08fab0857b8d7b6211 Mon Sep 17 00:00:00 2001 From: Josue Kouka Date: Wed, 17 May 2017 18:19:23 +0200 Subject: [PATCH 2/3] add chrono app view and utils (#16393) --- combo/apps/chrono/urls.py | 24 ++++++++++++++++++ combo/apps/chrono/utils.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++ combo/apps/chrono/views.py | 55 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 combo/apps/chrono/urls.py create mode 100644 combo/apps/chrono/utils.py create mode 100644 combo/apps/chrono/views.py diff --git a/combo/apps/chrono/urls.py b/combo/apps/chrono/urls.py new file mode 100644 index 0000000..925b467 --- /dev/null +++ b/combo/apps/chrono/urls.py @@ -0,0 +1,24 @@ +# combo - content management system +# Copyright (C) 2015 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/EventsView/(?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..1ce0aec --- /dev/null +++ b/combo/apps/chrono/utils.py @@ -0,0 +1,61 @@ +# combo - content management system +# Copyright (C) 2015-2016 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': 'unselected', + '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 + 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..d05c0d6 --- /dev/null +++ b/combo/apps/chrono/views.py @@ -0,0 +1,55 @@ +# combo - content management system +# Copyright (C) 2015 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 +from django.shortcuts import get_object_or_404 +from django.http import JsonResponse, HttpResponseRedirect, HttpResponseNotAllowed +from django.views.decorators.csrf import csrf_exempt +from django.views.generic import View +from django.views.generic.detail import SingleObjectMixin +from django.utils.decorators import method_decorator + +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 + + @method_decorator(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) + params = '&'.join(['%s=%s' % (key, value) for key, value in params.items()]) + url = '%s?%s' % (cell.form_url, params) + return JsonResponse({'url': url}, safe=False) -- 2.11.0