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