1
2
3
4
5
6
7
8
9
10
11
12
13 import time
14
15 from zope.interface import implements
16 from Products.CMFCore.utils import getToolByName
17 from Products.ZenRelations.utils import ZenRelationshipNameChooser
18 from Products.ZenWidgets.interfaces import *
19
20
21
22
23 INFO = 0
24 WARNING = 1
25 CRITICAL = 2
26
28 """
29 A single message. Messages are stored on UserSettings and in the session
30 object.
31 """
32 implements(IMessage)
33
34 __parent__ = None
35 title = None
36 body = None
37 timestamp = None
38 priority = None
39 _read = False
40
42 """
43 Initialization method.
44
45 @param title: The message title
46 @type title: str
47 @param body: The body of the message
48 @type body: str
49 @param priority: Message priority; one of INFO, WARNING, CRITICAL
50 @type priority: int
51 @param image: Optional URL of an image to be displayed in the message
52 @type image: str
53 """
54 self.title = title
55 self.body = body
56 self.priority = priority
57 self.image = image
58 self.timestamp = time.time()
59
61 """
62 Delete this message from the system.
63 """
64 self._read = True
65 try: self.__parent__.remove(self)
66 except (ValueError): pass
67 del self
68
72
73
75 """
76 Adapter for all persistent objects. Provides a method, L{get_messages},
77 that retrieves L{Message} objects.
78 """
79 implements(IMessageBox)
80
81 messagebox = None
82
84 """
85 Retrieve unread messages.
86
87 @param min_priority: Optional minimum priority of messages to be
88 returned; one of INFO, WARNING, CRITICAL
89 @type min_priority: int
90 @return: A list of objects implementing L{IMessage}.
91 @rtype: list
92 """
93 msgs = self.get_messages(min_priority)
94 msgs = filter(lambda x:not x._read, msgs)
95 return msgs
96
98 """
99 Retrieve messages from the current users's session object.
100
101 @param min_priority: Optional minimum priority of messages to be
102 returned; one of INFO, WARNING, CRITICAL
103 @type min_priority: int
104 @return: A list of L{Message} objects.
105 @rtype: list
106 """
107 msgs = sorted(self.messagebox, key=lambda x:x.timestamp)
108 msgs = filter(lambda x:x.priority>=min_priority, msgs)
109 return msgs
110
111
112
114 """
115 Adapter for all persistent objects. Provides a method, L{get_messages},
116 that retrieves L{Message} objects from the current user's session.
117 """
118 implements(IBrowserMessages)
120 """
121 Initialization method.
122
123 @param context: The object being adapted. Must have access to the
124 current request object via acquisition.
125 @type context: Persistent
126 """
127 self.context = context
128 self.messagebox = self.context.REQUEST.SESSION.get('messages', [])
129
130
131
133 """
134 Adapter for all persistent objects. Provides a method, L{get_messages},
135 that retrieves L{Message} objects from the current user's L{MessageQueue}.
136 """
137 implements(IUserMessages)
138 - def __init__(self, context, user=None):
139 """
140 Initialization method.
141
142 @param context: The object being adapted. Must have access to the dmd
143 via acquisition.
144 @type context: Persistent
145 @param user: Optional username corresponding to the queue from which
146 messages will be retrieved. If left as C{None}, the
147 current user's queue will be used.
148 @type user: str
149 """
150 self.context = context
151 self.user = user
152 users = getToolByName(self.context, 'ZenUsers')
153 us = users.getUserSettings(self.user)
154 self.messagebox = us.messages()
155
156
158 """
159 Adapts persistent objects in order to provide message sending capability.
160 """
161 implements(IMessageSender)
162
164 """
165 Initialization method.
166
167 @param context: The object being adapted. Must have access to the
168 dmd and the current request object via acquisition.
169 @type context: Persistent
170 """
171 self.context = context
172
174 """
175 Create a message and store it on the session object.
176
177 @param title: The message title
178 @type title: str
179 @param body: The body of the message
180 @type body: str
181 @param priority: Message priority; one of INFO, WARNING, CRITICAL
182 @type priority: int
183 @param image: Optional URL of an image to be displayed in the message
184 @type image: str
185 """
186 context = self.context.REQUEST.SESSION.get('messages')
187 if context is None:
188 self.context.REQUEST.SESSION['messages'] = context = []
189 m = BrowserMessage(title, body, priority, image)
190 m.__parent__ = context
191 context.append(m)
192
193 - def sendToUser(self, title, body, priority=INFO, image=None, user=None):
194 """
195 Create a message and store it in the L{IMessageQueue} of the user
196 specified. If no user is specified, use the queue of the current user.
197
198 @param title: The message title
199 @type title: str
200 @param body: The body of the message
201 @type body: str
202 @param priority: Message priority; one of INFO, WARNING, CRITICAL
203 @type priority: int
204 @param image: Optional URL of an image to be displayed in the message
205 @type image: str
206 @param user: Optional username corresponding to the queue to which
207 messages should be sent. If left as C{None}, the current
208 user's queue will be used.
209 @type user: str
210 """
211 users = getToolByName(self.context, 'ZenUsers')
212 us = users.getUserSettings(user)
213 id = ZenRelationshipNameChooser(us.messages).chooseName('msg')
214
215 from PersistentMessage import PersistentMessage
216 m = PersistentMessage(id, title, body, priority, image)
217 us.messages._setObject(m.id, m)
218
220 """
221 For eash user in the system, create an identical message and store it
222 in the user's L{IMessageQueue}.
223
224 @param title: The message title
225 @type title: str
226 @param body: The body of the message
227 @type body: str
228 @param priority: Message priority; one of INFO, WARNING, CRITICAL
229 @type priority: int
230 @param image: Optional URL of an image to be displayed in the message
231 @type image: str
232 """
233 users = getToolByName(self.context, 'ZenUsers')
234 for name in users.getAllUserSettingsNames():
235 self.sendToUser(title, body, priority, user=name, image=image)
236