0002-add-chrono-app-view-and-utils-16393.patch
combo/apps/chrono/urls.py | ||
---|---|---|
1 |
# combo - content management system |
|
2 |
# Copyright (C) 2015 Entr'ouvert |
|
3 |
# |
|
4 |
# This program is free software: you can redistribute it and/or modify it |
|
5 |
# under the terms of the GNU Affero General Public License as published |
|
6 |
# by the Free Software Foundation, either version 3 of the License, or |
|
7 |
# (at your option) any later version. |
|
8 |
# |
|
9 |
# This program is distributed in the hope that it will be useful, |
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
# GNU Affero General Public License for more details. |
|
13 |
# |
|
14 |
# You should have received a copy of the GNU Affero General Public License |
|
15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | ||
17 |
from django.conf.urls import url |
|
18 | ||
19 |
from .views import EventsView, BookingView |
|
20 | ||
21 |
urlpatterns = [ |
|
22 |
url(r'^chrono/EventsView/(?P<pk>[\w,-]+)/', EventsView.as_view(), name='chrono-events'), |
|
23 |
url(r'^chrono/book/(?P<pk>[\w,-]+)/', BookingView.as_view(), name='chrono-booking'), |
|
24 |
] |
combo/apps/chrono/utils.py | ||
---|---|---|
1 |
# combo - content management system |
|
2 |
# Copyright (C) 2015-2016 Entr'ouvert |
|
3 |
# |
|
4 |
# This program is free software: you can redistribute it and/or modify it |
|
5 |
# under the terms of the GNU Affero General Public License as published |
|
6 |
# by the Free Software Foundation, either version 3 of the License, or |
|
7 |
# (at your option) any later version. |
|
8 |
# |
|
9 |
# This program is distributed in the hope that it will be useful, |
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
# GNU Affero General Public License for more details. |
|
13 |
# |
|
14 |
# You should have received a copy of the GNU Affero General Public License |
|
15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | ||
17 |
from django.conf import settings |
|
18 |
from combo.utils import requests |
|
19 | ||
20 | ||
21 |
def get_chrono_service(): |
|
22 |
if hasattr(settings, 'KNOWN_SERVICES') and settings.KNOWN_SERVICES.get('chrono'): |
|
23 |
return settings.KNOWN_SERVICES['chrono'].values()[0] |
|
24 | ||
25 | ||
26 |
def is_chrono_enabled(): |
|
27 |
return get_chrono_service() |
|
28 | ||
29 | ||
30 |
def convert_widget_format(events): |
|
31 |
data = [] |
|
32 |
for event in events.get('data'): |
|
33 |
data.append({ |
|
34 |
'id': 'unselected', |
|
35 |
'title': event.get('text'), |
|
36 |
'start': event.get('datetime'), |
|
37 |
'editable': False, |
|
38 |
'durationEditable': False, |
|
39 |
'rendering': 'inverse-background' |
|
40 |
}) |
|
41 |
return data |
|
42 | ||
43 | ||
44 |
def get_chrono_events(chrono_url, **kwargs): |
|
45 |
response = requests.get( |
|
46 |
chrono_url, |
|
47 |
headers={'accept': 'application/json'}, |
|
48 |
**kwargs) |
|
49 |
data = convert_widget_format(response.json()) |
|
50 |
return data |
|
51 | ||
52 | ||
53 |
def build_session_vars(cell, data): |
|
54 |
session_vars = {} |
|
55 |
for key, value in cell.session_vars.items(): |
|
56 |
key = 'session_var_%s' % key |
|
57 |
if data.get(value): |
|
58 |
session_vars.update({key: data[value]}) |
|
59 |
else: |
|
60 |
session_vars.update({key: value}) |
|
61 |
return session_vars |
combo/apps/chrono/views.py | ||
---|---|---|
1 |
# combo - content management system |
|
2 |
# Copyright (C) 2015 Entr'ouvert |
|
3 |
# |
|
4 |
# This program is free software: you can redistribute it and/or modify it |
|
5 |
# under the terms of the GNU Affero General Public License as published |
|
6 |
# by the Free Software Foundation, either version 3 of the License, or |
|
7 |
# (at your option) any later version. |
|
8 |
# |
|
9 |
# This program is distributed in the hope that it will be useful, |
|
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
# GNU Affero General Public License for more details. |
|
13 |
# |
|
14 |
# You should have received a copy of the GNU Affero General Public License |
|
15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | ||
17 |
import json |
|
18 |
from django.shortcuts import get_object_or_404 |
|
19 |
from django.http import JsonResponse, HttpResponseRedirect, HttpResponseNotAllowed |
|
20 |
from django.views.decorators.csrf import csrf_exempt |
|
21 |
from django.views.generic import View |
|
22 |
from django.views.generic.detail import SingleObjectMixin |
|
23 |
from django.utils.decorators import method_decorator |
|
24 | ||
25 |
from .models import CalendarCell |
|
26 |
from .utils import get_chrono_events, build_session_vars |
|
27 | ||
28 | ||
29 |
class EventsView(SingleObjectMixin, View): |
|
30 | ||
31 |
http_method_names = ['get'] |
|
32 |
model = CalendarCell |
|
33 | ||
34 |
def get(self, request, *args, **kwargs): |
|
35 |
cell = self.get_object() |
|
36 |
data = get_chrono_events(cell.events_source) |
|
37 |
return JsonResponse(data, safe=False) |
|
38 | ||
39 | ||
40 |
class BookingView(SingleObjectMixin, View): |
|
41 | ||
42 |
http_method_names = ['post'] |
|
43 |
model = CalendarCell |
|
44 | ||
45 |
@method_decorator(csrf_exempt) |
|
46 |
def dispatch(self, request, *args, **kwargs): |
|
47 |
return super(BookingView, self).dispatch(request, *args, **kwargs) |
|
48 | ||
49 |
def post(self, request, *args, **kwargs): |
|
50 |
data = json.loads(request.body) |
|
51 |
cell = self.get_object() |
|
52 |
params = build_session_vars(cell, data) |
|
53 |
params = '&'.join(['%s=%s' % (key, value) for key, value in params.items()]) |
|
54 |
url = '%s?%s' % (cell.form_url, params) |
|
55 |
return JsonResponse({'url': url}, safe=False) |
|
0 |
- |