Projet

Général

Profil

Télécharger (2,26 ko) Statistiques
| Branche: | Tag: | Révision:

root / corbo / hobo_agent / management / commands / hobo_notify.py @ e5b05b4d

1
# corbo - Announces Manager
2
# Copyright (C) 2017 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.db.models import Q
18

    
19
from hobo.agent.common.management.commands import hobo_notify
20

    
21
from corbo.models import Subscription
22

    
23
class Command(hobo_notify.Command):
24

    
25
    def process_notification(self, tenant, notification):
26
        super(Command, self).process_notification(tenant, notification)
27
        object_type = notification['objects']['@type']
28
        if object_type != 'user':
29
            return
30
        users = notification['objects']['data']
31
        action = notification['@type']
32
        if notification.get('full'):
33
            uuids = [user['uuid'] for user in users]
34
            Subscription.objects.exclude(Q(uuid__in=uuids)|Q(uuid__isnull=True)|Q(uuid='')).delete()
35

    
36
        for user in users:
37
            for subscription in Subscription.objects.filter(uuid=user['uuid']):
38
                if action == 'provision':
39
                    if subscription.identifier.startswith('mailto:'):
40
                        if user.get('email'):
41
                            subscription.identifier = 'mailto:%s' % user['email']
42
                        else:
43
                            subscription.delete()
44
                            continue
45
                    elif subscription.identifier.startswith('sms:'):
46
                        if user.get('mobile'):
47
                            subscription.identifier = 'sms:%s' % user['mobile']
48
                        else:
49
                            subscription.delete()
50
                            continue
51
                    subscription.save()
52
                elif action == 'deprovision':
53
                    subscription.delete()
(2-2/2)