1
|
# corbo - Announces Manager
|
2
|
# Copyright (C) 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
|
import json
|
18
|
from collections import defaultdict
|
19
|
|
20
|
from django.core.exceptions import PermissionDenied
|
21
|
from django.db.models import Q
|
22
|
from django.db import transaction
|
23
|
|
24
|
from rest_framework.views import APIView
|
25
|
from rest_framework.response import Response
|
26
|
|
27
|
from .models import Category, Subscription, channel_choices
|
28
|
|
29
|
|
30
|
class NewslettersView(APIView):
|
31
|
|
32
|
def get(self, request):
|
33
|
newsletters = []
|
34
|
transports = [{'id': identifier, 'text': name} for identifier, name in channel_choices]
|
35
|
for c in Category.objects.all():
|
36
|
newsletter = {'id': c.slug, 'text': c.name,
|
37
|
'transports': transports}
|
38
|
newsletters.append(newsletter)
|
39
|
return Response({'data': newsletters})
|
40
|
|
41
|
|
42
|
class SubscriptionsView(APIView):
|
43
|
|
44
|
def get_subscriptions(self, uuid):
|
45
|
subscriptions = defaultdict(dict)
|
46
|
for s in Subscription.objects.filter(uuid=uuid):
|
47
|
cat_id = s.category.pk
|
48
|
subscriptions[cat_id]['id'] = s.category.slug
|
49
|
subscriptions[cat_id]['text'] = s.category.name
|
50
|
transport_id, transport_name = s.identifier.split(':')
|
51
|
transport = {'id': transport_id,
|
52
|
'text': transport_id}
|
53
|
if 'transports' in subscriptions[cat_id]:
|
54
|
subscriptions[cat_id]['transports'].append(transport)
|
55
|
else:
|
56
|
subscriptions[cat_id]['transports'] = [transport]
|
57
|
|
58
|
return subscriptions.values()
|
59
|
|
60
|
@transaction.atomic
|
61
|
def update_subscriptions(self, category_slug, transports, uuid, email=None, mobile=None):
|
62
|
try:
|
63
|
cat = Category.objects.get(slug=category_slug)
|
64
|
except Category.DoesNotExist:
|
65
|
return
|
66
|
|
67
|
subcriptions = cat.subscription_set
|
68
|
|
69
|
if 'mailto' in transports:
|
70
|
if email:
|
71
|
subcriptions.get_or_create(identifier='mailto:%s' % email, uuid=uuid)
|
72
|
else:
|
73
|
subcriptions.filter(uuid=uuid, identifier__startswith='mailto:').delete()
|
74
|
|
75
|
if 'sms' in transports:
|
76
|
if mobile:
|
77
|
subcriptions.get_or_create(identifier='sms:%s' % mobile, uuid=uuid)
|
78
|
else:
|
79
|
subcriptions.filter(uuid=uuid, identifier__startswith='sms:').delete()
|
80
|
|
81
|
def get(self, request):
|
82
|
uuid = request.GET.get('uuid')
|
83
|
if not uuid:
|
84
|
raise PermissionDenied('Uuid parameter required')
|
85
|
return Response({'data': self.get_subscriptions( uuid)})
|
86
|
|
87
|
def post(self, request):
|
88
|
uuid = request.GET.get('uuid')
|
89
|
email = request.GET.get('email')
|
90
|
mobile = request.GET.get('mobile')
|
91
|
if not uuid:
|
92
|
raise PermissionDenied('Uuid parameter required')
|
93
|
data = json.loads(request.body)
|
94
|
for subscription in data:
|
95
|
self.update_subscriptions(subscription['id'], subscription['transports'],
|
96
|
uuid, email, mobile)
|
97
|
return Response({'data': True})
|
98
|
|
99
|
def delete(self, request):
|
100
|
uuid = request.GET.get('uuid')
|
101
|
if not uuid:
|
102
|
raise PermissionDenied('Uuid parameter required')
|
103
|
for subscription in self.get_subscriptions(uuid):
|
104
|
self.update_subscriptions(subscription['id'], [], uuid)
|
105
|
return Response({'data': True})
|
106
|
|
107
|
|
108
|
class SubscribeView(SubscriptionsView):
|
109
|
http_method_names = ['post']
|
110
|
|
111
|
def post(self, request):
|
112
|
email = request.GET.get('email')
|
113
|
mobile = request.GET.get('mobile')
|
114
|
uuid = request.GET.get('uuid')
|
115
|
if not uuid:
|
116
|
raise PermissionDenied('Uuid parameter required')
|
117
|
data = json.loads(request.body)
|
118
|
|
119
|
self.update_subscriptions(data['category_id'], data['transports'], uuid,
|
120
|
email, mobile)
|
121
|
|
122
|
return Response({'data': True})
|