Projet

Général

Profil

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

root / corbo / api_views.py @ 8759c981

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': str(c.pk), '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'] = 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})
(3-3/11)