Projet

Général

Profil

Télécharger (4,47 ko) Statistiques
| Branche: | Tag: | Révision:

root / corbo / api_views.py @ 40b65753

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, 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'] = s.category.slug
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_slug, 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(slug=category_slug)
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
    def get(self, request):
83
        email = request.GET.get('email')
84
        uuid = request.GET.get('uuid')
85
        if not email:
86
            raise PermissionDenied('Email parameter required')
87
        return Response({'data': self.get_subscriptions(email, uuid)})
88

    
89
    def post(self, request):
90
        email = request.GET.get('email')
91
        uuid = request.GET.get('uuid')
92
        if not email:
93
            raise PermissionDenied('Email parameter required')
94
        data = json.loads(request.body)
95
        for subscription in data:
96
            self.update_subscriptions(subscription['id'], subscription['transports'],
97
                                      email, uuid)
98
        return Response({'data': True})
99

    
100
    def delete(self, request):
101
        email = request.GET.get('email')
102
        uuid = request.GET.get('uuid')
103
        if not email:
104
            raise PermissionDenied('Email parameter required')
105

    
106
        for subscription in self.get_subscriptions(email):
107
            self.update_subscriptions(subscription['id'], [], email, uuid)
108
        return Response({'data': True})
109

    
110

    
111
class SubscribeView(SubscriptionsView):
112
    http_method_names = ['post']
113

    
114
    def post(self, request):
115
        email = request.GET.get('email')
116
        uuid = request.GET.get('uuid')
117
        if not email:
118
            raise PermissionDenied('Email parameter required')
119
        data = json.loads(request.body)
120

    
121
        category_id = data['category_id']
122
        transport = ('mailto',)
123

    
124
        self.update_subscriptions(category_id, transport, email, uuid)
125

    
126
        return Response({'data': True})
(3-3/11)