Package ZenWidgets :: Module messaging
[hide private]
[frames] | no frames]

Source Code for Module ZenWidgets.messaging

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2007, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 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  # Constants representing priorities.  
 22  # Parallel definitions exist in uifeedback.js. 
 23  INFO     = 0 
 24  WARNING  = 1 
 25  CRITICAL = 2 
 26   
27 -class BrowserMessage(object):
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
41 - def __init__(self, title, body, priority=INFO, image=None):
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
60 - def delete(self):
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
69 - def mark_as_read(self):
70 self._read = True 71 self.delete()
72 73
74 -class MessageBox(object):
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
83 - def get_unread(self, min_priority=INFO):
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
97 - def get_messages(self, min_priority=INFO):
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
113 -class BrowserMessageBox(MessageBox):
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)
119 - def __init__(self, context):
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
132 -class UserMessageBox(MessageBox):
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
157 -class MessageSender(object):
158 """ 159 Adapts persistent objects in order to provide message sending capability. 160 """ 161 implements(IMessageSender) 162
163 - def __init__(self, context):
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
173 - def sendToBrowser(self, title, body, priority=INFO, image=None):
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 # done in here to prevent recursive imports from ZenModelRM 215 from PersistentMessage import PersistentMessage 216 m = PersistentMessage(id, title, body, priority, image) 217 us.messages._setObject(m.id, m)
218
219 - def sendToAll(self, title, body, priority=INFO, image=None):
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