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
113 """
114 Adapter for all persistent objects. Provides a method, L{get_messages},
115 that retrieves L{Message} objects from the current user's session.
116 """
117 implements(IBrowserMessages)
119 """
120 Initialization method.
121
122 @param context: The object being adapted. Must have access to the
123 current request object via acquisition.
124 @type context: Persistent
125 """
126 self.context = context
127 self.messagebox = self.context.REQUEST.SESSION.get('messages', [])
128
129
131 """
132 Adapter for all persistent objects. Provides a method, L{get_messages},
133 that retrieves L{Message} objects from the current user's L{MessageQueue}.
134 """
135 implements(IUserMessages)
136 - def __init__(self, context, user=None):
137 """
138 Initialization method.
139
140 @param context: The object being adapted. Must have access to the dmd
141 via acquisition.
142 @type context: Persistent
143 @param user: Optional username corresponding to the queue from which
144 messages will be retrieved. If left as C{None}, the
145 current user's queue will be used.
146 @type user: str
147 """
148 self.context = context
149 self.user = user
150 users = getToolByName(self.context, 'ZenUsers')
151 us = users.getUserSettings(self.user)
152 self.messagebox = us.messages()
153
154
156 """
157 Adapts persistent objects in order to provide message sending capability.
158 """
159 implements(IMessageSender)
160
162 """
163 Initialization method.
164
165 @param context: The object being adapted. Must have access to the
166 dmd and the current request object via acquisition.
167 @type context: Persistent
168 """
169 self.context = context
170
172 """
173 Create a message and store it on the session object.
174
175 @param title: The message title
176 @type title: str
177 @param body: The body of the message
178 @type body: str
179 @param priority: Message priority; one of INFO, WARNING, CRITICAL
180 @type priority: int
181 @param image: Optional URL of an image to be displayed in the message
182 @type image: str
183 """
184 context = self.context.REQUEST.SESSION.get('messages')
185 if context is None:
186 self.context.REQUEST.SESSION['messages'] = context = []
187 m = BrowserMessage(title, body, priority, image)
188 m.__parent__ = context
189 context.append(m)
190
191 - def sendToUser(self, title, body, priority=INFO, image=None, user=None):
192 """
193 Create a message and store it in the L{IMessageQueue} of the user
194 specified. If no user is specified, use the queue of the current user.
195
196 @param title: The message title
197 @type title: str
198 @param body: The body of the message
199 @type body: str
200 @param priority: Message priority; one of INFO, WARNING, CRITICAL
201 @type priority: int
202 @param image: Optional URL of an image to be displayed in the message
203 @type image: str
204 @param user: Optional username corresponding to the queue to which
205 messages should be sent. If left as C{None}, the current
206 user's queue will be used.
207 @type user: str
208 """
209 users = getToolByName(self.context, 'ZenUsers')
210 us = users.getUserSettings(user)
211 id = ZenRelationshipNameChooser(us.messages).chooseName('msg')
212
213 from PersistentMessage import PersistentMessage
214 m = PersistentMessage(id, title, body, priority, image)
215 us.messages._setObject(m.id, m)
216
218 """
219 For eash user in the system, create an identical message and store it
220 in the user's L{IMessageQueue}.
221
222 @param title: The message title
223 @type title: str
224 @param body: The body of the message
225 @type body: str
226 @param priority: Message priority; one of INFO, WARNING, CRITICAL
227 @type priority: int
228 @param image: Optional URL of an image to be displayed in the message
229 @type image: str
230 """
231 users = getToolByName(self.context, 'ZenUsers')
232 for name in users.getAllUserSettingsNames():
233 self.sendToUser(title, body, priority, user=name, image=image)
234
235
237 """
238 Special message sender for use in scripts. Short-circuits sendToBrowser and
239 sendToUser, since they don't really apply. sendToAll should still work fine
240 though.
241 """
244 - def sendToUser(self, title, body, priority=INFO, image=None, user=None):
246