jslint
[Plinn.git] / EmailNotification.py
1 # -*- coding: utf-8 -*-
2 #######################################################################################
3 # Plinn - http://plinn.org #
4 # Copyright © 2005-2009 Benoît PIN <benoit.pin@ensmp.fr> #
5 # #
6 # This program is free software; you can redistribute it and/or #
7 # modify it under the terms of the GNU General Public License #
8 # as published by the Free Software Foundation; either version 2 #
9 # of the License, or (at your option) any later version. #
10 # #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
15 # #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program; if not, write to the Free Software #
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #
19 #######################################################################################
20 """
21 Module to manage email notification settings.
22
23
24
25 """
26 from ExtensionClass import Base
27 import Acquisition
28 from Globals import InitializeClass, PersistentMapping
29 from AccessControl import ClassSecurityInfo
30 from zope.interface import implements
31 from interfaces import IEmailNotificationSettings
32 from permissions import ListNotificationSettings, SubscribeNotification
33 from Products.CMFCore.utils import getToolByName
34 from Products.Plinn.utils import Message as _
35
36 NOTIFICATION_SETTINGS_NAME = '_notification_settings'
37 EVENTS = ({'interface':'zope.app.container.interfaces.IObjectRemovedEvent'
38 ,'title':_(u'Object deleted')},)
39
40 class EmailNoticationSettings(Base, Acquisition.Implicit):
41 """
42 Adapter used to map users on objects and send them email notification about events.
43 Provide methods to resolve recipients list for a notification.
44 """
45
46 implements(IEmailNotificationSettings)
47 security = ClassSecurityInfo()
48
49 def __init__(self, content) :
50 self._content = content
51 if not hasattr(content.aq_base, NOTIFICATION_SETTINGS_NAME) :
52 setattr(content, NOTIFICATION_SETTINGS_NAME, PersistentMapping())
53
54 def _getSettings(self) :
55 return getattr(self._content, NOTIFICATION_SETTINGS_NAME)
56
57 security.declarePrivate('getSubscribersFor')
58 def getSubscribersFor(self, eventIFace):
59 """returns subscribers for event interface"""
60 settings = self._getSettings()
61 mtool = getToolByName(self, 'portal_membership')
62 memberIds = [mid for mid, mSettings in settings.items() if mSettings.get(eventIFace, False)]
63
64 return mtool.getMembers(memberIds)
65
66 security.declareProtected(SubscribeNotification, 'subscribeToEvent')
67 def subscribeToEvent(self, eventIFace, register):
68 settings = self._getSettings()
69 mtool = getToolByName(self, 'portal_membership')
70 m = mtool.getAuthenticatedMember()
71 mid = m.getId()
72 if not settings.has_key(mid) :
73 settings[mid] = PersistentMapping()
74
75 memberSettings = settings[mid]
76 memberSettings[eventIFace] = register
77
78 security.declareProtected(SubscribeNotification, 'myNotifications')
79 def myNotifications(self):
80 settings = self._getSettings()
81 mtool = getToolByName(self, 'portal_membership')
82 m = mtool.getAuthenticatedMember()
83 mid = m.getId()
84 mySettings = settings.get(mid, {})
85
86 notifications = []
87 for event in EVENTS :
88 setting = event.copy()
89 setting['registered'] = mySettings.get(event['interface'], False)
90 notifications.append(setting)
91
92 return notifications
93
94 security.declarePublic('getManagedEvents')
95 def getManagedEvents(self):
96 return [e['interface'] for e in EVENTS]
97
98
99
100 InitializeClass(EmailNoticationSettings)