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

# Source Code for Module Products.ZenWidgets.messaging

      1  ##############################################################################
      2  #
      3  # Copyright (C) Zenoss, Inc. 2007, all rights reserved.
      4  #
      5  # This content is made available according to terms specified in
      6  # License.zenoss under the directory where your Zenoss product is installed.
      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  # Constants representing priorities.
     20  # Parallel definitions exist in zenoss.js.
     21  INFO     = 0
     22  WARNING  = 1
     23  CRITICAL = 2
     24
     25 -class BrowserMessage(object):
     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
     59 -    def delete(self):
     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
     68 -    def mark_as_read(self):
     69          self._read = True
     70          self.delete()
     71
     72
     73 -class MessageBox(object):
     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
     82 -    def get_unread(self, min_priority=INFO):
     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
     96 -    def get_messages(self, min_priority=INFO):
     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
    111 -class BrowserMessageBox(MessageBox):
    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)
    117 -    def __init__(self, context):
    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
    128 -    def get_unread(self, min_priority=INFO):
    129          msgs = super(BrowserMessageBox, self).get_unread(min_priority=min_priority)
    130          # force the session to persist
    131          if msgs:
    132              self.context.REQUEST.SESSION._p_changed = True
    133          return msgs
    134
    135 -class UserMessageBox(MessageBox):
    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
    160 -class MessageSender(object):
    161      """
    162      Adapts persistent objects in order to provide message sending capability.
    163      """
    164      implements(IMessageSender)
    165
    166 -    def __init__(self, context):
    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
    176 -    def sendToBrowser(self, title, body, priority=INFO, image=None, sticky=None):
    177          """
    178          Create a message and store it on the session object.
    179
    180          @param title: The message title
    181          @type title: str
    182          @param body: The body of the message
    183          @type body: str
    184          @param priority: Message priority; one of INFO, WARNING, CRITICAL
    185          @type priority: int
    186          @param image: Optional URL of an image to be displayed in the message
    187          @type image: str
    188          """
    189          context = self.context.REQUEST.SESSION.get('messages')
    190          if context is None:
    191              self.context.REQUEST.SESSION['messages'] = context = []
    192          m = BrowserMessage(title, body, priority, image, sticky)
    193          m.__parent__ = context
    194          context.append(m)
    195          self.context.REQUEST.SESSION._p_changed = True
    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          # done in here to prevent recursive imports from ZenModelRM
    219          from PersistentMessage import PersistentMessage
    220          m = PersistentMessage(id, title, body, priority, image)
    221          us.messages._setObject(m.id, m)
    222
    223 -    def sendToAll(self, title, body, priority=INFO, image=None):
    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
    242 -class ScriptMessageSender(MessageSender):
    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      """
    248 -    def sendToBrowser(self, title, body, priority=INFO, image=None, sticky=None):
    249          pass
    250 -    def sendToUser(self, title, body, priority=INFO, image=None, user=None):
    251          pass
    252