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

# Source Code for Module Products.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 or (at your
      8  # option) any later version as published by 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 zenoss.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, sticky=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          self.sticky = sticky
     60
     61 -    def delete(self):
     62          """
     63          Delete this message from the system.
     64          """
     65          self._read = True
     66          try: self.__parent__.remove(self)
     67          except (ValueError): pass
     68          del self
     69
     70 -    def mark_as_read(self):
     71          self._read = True
     72          self.delete()
     73
     74
     75 -class MessageBox(object):
     76      """
     77      Adapter for all persistent objects. Provides a method, L{get_messages},
     78      that retrieves L{Message} objects.
     79      """
     80      implements(IMessageBox)
     81
     82      messagebox = None
     83
     84 -    def get_unread(self, min_priority=INFO):
     85          """
     86          Retrieve unread messages.
     87
     88          @param min_priority: Optional minimum priority of messages to be
     89          returned; one of INFO, WARNING, CRITICAL
     90          @type min_priority: int
     91          @return: A list of objects implementing L{IMessage}.
     92          @rtype: list
     93          """
     94          msgs = self.get_messages(min_priority)
     95          msgs = filter(lambda x:not x._read, msgs)
     96          return msgs
     97
     98 -    def get_messages(self, min_priority=INFO):
     99          """
    100          Retrieve messages from the current users's session object.
    101
    102          @param min_priority: Optional minimum priority of messages to be
    103          returned; one of INFO, WARNING, CRITICAL
    104          @type min_priority: int
    105          @return: A list of L{Message} objects.
    106          @rtype: list
    107          """
    108          msgs = sorted(self.messagebox, key=lambda x:x.timestamp)
    109          msgs = filter(lambda x:x.priority>=min_priority, msgs)
    110          return msgs
    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 -class UserMessageBox(MessageBox):
    132      """
    133      Adapter for all persistent objects. Provides a method, L{get_messages},
    134      that retrieves L{Message} objects from the current user's L{MessageQueue}.
    135      """
    136      implements(IUserMessages)
    137 -    def __init__(self, context, user=None):
    138          """
    139          Initialization method.
    140
    141          @param context: The object being adapted. Must have access to the dmd
    142                          via acquisition.
    143          @type context: Persistent
    144          @param user: Optional username corresponding to the queue from which
    145                       messages will be retrieved. If left as C{None}, the
    146                       current user's queue will be used.
    147          @type user: str
    148          """
    149          self.context = context
    150          self.user = user
    151          users = getToolByName(self.context, 'ZenUsers')
    152          us = users.getUserSettings(self.user)
    153          self.messagebox = us.messages()
    154
    155
    156 -class MessageSender(object):
    157      """
    158      Adapts persistent objects in order to provide message sending capability.
    159      """
    160      implements(IMessageSender)
    161
    162 -    def __init__(self, context):
    163          """
    164          Initialization method.
    165
    166          @param context: The object being adapted. Must have access to the
    167                          dmd and the current request object via acquisition.
    168          @type context: Persistent
    169          """
    170          self.context = context
    171
    172 -    def sendToBrowser(self, title, body, priority=INFO, image=None, sticky=None):
    173          """
    174          Create a message and store it on the session object.
    175
    176          @param title: The message title
    177          @type title: str
    178          @param body: The body of the message
    179          @type body: str
    180          @param priority: Message priority; one of INFO, WARNING, CRITICAL
    181          @type priority: int
    182          @param image: Optional URL of an image to be displayed in the message
    183          @type image: str
    184          """
    185          context = self.context.REQUEST.SESSION.get('messages')
    186          if context is None:
    187              self.context.REQUEST.SESSION['messages'] = context = []
    188          m = BrowserMessage(title, body, priority, image, sticky)
    189          m.__parent__ = context
    190          context.append(m)
    191
    192 -    def sendToUser(self, title, body, priority=INFO, image=None, user=None):
    193          """
    194          Create a message and store it in the L{IMessageQueue} of the user
    195          specified. If no user is specified, use the queue of the current user.
    196
    197          @param title: The message title
    198          @type title: str
    199          @param body: The body of the message
    200          @type body: str
    201          @param priority: Message priority; one of INFO, WARNING, CRITICAL
    202          @type priority: int
    203          @param image: Optional URL of an image to be displayed in the message
    204          @type image: str
    205          @param user: Optional username corresponding to the queue to which
    206                       messages should be sent. If left as C{None}, the current
    207                       user's queue will be used.
    208          @type user: str
    209          """
    210          users = getToolByName(self.context, 'ZenUsers')
    211          us = users.getUserSettings(user)
    212          id = ZenRelationshipNameChooser(us.messages).chooseName('msg')
    213          # done in here to prevent recursive imports from ZenModelRM
    214          from PersistentMessage import PersistentMessage
    215          m = PersistentMessage(id, title, body, priority, image)
    216          us.messages._setObject(m.id, m)
    217
    218 -    def sendToAll(self, title, body, priority=INFO, image=None):
    219          """
    220          For eash user in the system, create an identical message and store it
    221          in the user's L{IMessageQueue}.
    222
    223          @param title: The message title
    224          @type title: str
    225          @param body: The body of the message
    226          @type body: str
    227          @param priority: Message priority; one of INFO, WARNING, CRITICAL
    228          @type priority: int
    229          @param image: Optional URL of an image to be displayed in the message
    230          @type image: str
    231          """
    232          users = getToolByName(self.context, 'ZenUsers')
    233          for name in users.getAllUserSettingsNames():
    234              self.sendToUser(title, body, priority, user=name, image=image)
    235
    236
    237 -class ScriptMessageSender(MessageSender):
    238      """
    239      Special message sender for use in scripts. Short-circuits sendToBrowser and
    240      sendToUser, since they don't really apply. sendToAll should still work fine
    241      though.
    242      """
    243 -    def sendToBrowser(self, title, body, priority=INFO, image=None, sticky=None):
    244          pass
    245 -    def sendToUser(self, title, body, priority=INFO, image=None, user=None):
    246          pass
    247