Revision 8759c981
Added by Serghei Mihai over 8 years ago
corbo/api_views.py | ||
---|---|---|
14 | 14 |
# You should have received a copy of the GNU Affero General Public License |
15 | 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | 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 |
|
|
17 | 24 |
from rest_framework.views import APIView |
18 | 25 |
from rest_framework.response import Response |
19 | 26 |
|
... | ... | |
30 | 37 |
'transports': transports} |
31 | 38 |
newsletters.append(newsletter) |
32 | 39 |
return Response({'data': newsletters}) |
40 |
|
|
41 |
|
|
42 |
class SubscriptionsView(APIView): |
|
43 |
|
|
44 |
def get_subscriptions(self, email, uuid=None): |
|
45 |
subscriptions = defaultdict(dict) |
|
46 |
identifier = 'mailto:'+email |
|
47 |
for s in Subscription.objects.filter(Q(identifier=identifier)|Q(uuid=uuid)): |
|
48 |
cat_id = s.category.pk |
|
49 |
subscriptions[cat_id]['id'] = str(cat_id) |
|
50 |
subscriptions[cat_id]['text'] = s.category.name |
|
51 |
transport_id, transport_name = identifier.split(':') |
|
52 |
transport = {'id': transport_id, |
|
53 |
'text': transport_id} |
|
54 |
if 'transports' in subscriptions[cat_id]: |
|
55 |
subscriptions[cat_id]['transports'].append(transport) |
|
56 |
else: |
|
57 |
subscriptions[cat_id]['transports'] = [transport] |
|
58 |
|
|
59 |
return subscriptions.values() |
|
60 |
|
|
61 |
@transaction.atomic |
|
62 |
def update_subscriptions(self, category_id, transports, email, uuid=None): |
|
63 |
uuid = uuid or u'' |
|
64 |
identifier = u'mailto:' |
|
65 |
if email: |
|
66 |
identifier += email |
|
67 |
try: |
|
68 |
cat = Category.objects.get(pk=category_id) |
|
69 |
except Category.DoesNotExist: |
|
70 |
return |
|
71 |
|
|
72 |
subcriptions = cat.subscription_set |
|
73 |
|
|
74 |
if email: |
|
75 |
if 'mailto' in transports: |
|
76 |
subcriptions.get_or_create(identifier=identifier, uuid=uuid) |
|
77 |
if uuid: |
|
78 |
subcriptions.exclude(uuid=uuid).filter(identifier=identifier).delete() |
|
79 |
else: |
|
80 |
subcriptions.filter(identifier=identifier).delete() |
|
81 |
|
|
82 |
if uuid: |
|
83 |
if 'homepage' in transports: |
|
84 |
subcriptions.get_or_create(uuid=uuid) |
|
85 |
else: |
|
86 |
subcriptions.filter(identifier='', uuid=uuid).delete() |
|
87 |
|
|
88 |
def get(self, request): |
|
89 |
email = request.GET.get('email') |
|
90 |
uuid = request.GET.get('uuid') |
|
91 |
if not email: |
|
92 |
raise PermissionDenied('Email parameter required') |
|
93 |
return Response({'data': self.get_subscriptions(email, uuid)}) |
|
94 |
|
|
95 |
def post(self, request): |
|
96 |
email = request.GET.get('email') |
|
97 |
uuid = request.GET.get('uuid') |
|
98 |
if not email: |
|
99 |
raise PermissionDenied('Email parameter required') |
|
100 |
data = json.loads(request.body) |
|
101 |
for subscription in data: |
|
102 |
self.update_subscriptions(subscription['id'], subscription['transports'], |
|
103 |
email, uuid) |
|
104 |
return Response({'data': True}) |
|
105 |
|
|
106 |
def delete(self, request): |
|
107 |
email = request.GET.get('email') |
|
108 |
uuid = request.GET.get('uuid') |
|
109 |
if not email: |
|
110 |
raise PermissionDenied('Email parameter required') |
|
111 |
|
|
112 |
for subscription in self.get_subscriptions(email): |
|
113 |
self.update_subscriptions(subscription['id'], [], email, uuid) |
|
114 |
return Response({'data': True}) |
Also available in: Unified diff
api: subscriptions management endpoint (#10794)