Package Products :: Package ZenModel :: Module ZenPacker |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenModel.ZenPacker

     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 Globals
    14  from AccessControl import ClassSecurityInfo
    15  from Products.ZenWidgets import messaging
    16
    17 -class ZenPacker(object):
    18      security = ClassSecurityInfo()
    19
    20      security.declareProtected('Manage DMD', 'addToZenPack')
    21 -    def addToZenPack(self, ids=(), organizerPaths=(), pack=None, REQUEST=None):
    22          "Add elements from a displayed list of objects to a ZenPack"
    23          from Products.ZenModel.ZenPackable import ZenPackable
    24          from Products.ZenModel.ZenPack import ZenPack
    25          ids = list(ids) + list(organizerPaths)
    26          message = "You must provide a valid ZenPack"
    27          if pack:
    28              pack = self.dmd.ZenPackManager.packs._getOb(pack)
    29              message = 'Saved to %s' % pack.id
    30              if ids:
    31                  for id in ids:
    32                      try:
    33                          obj = self.findObject(id)
    34                      except AttributeError, ex:
    35                          message = str(ex)
    36                          break
    37                      obj.buildRelations()
    38                      pack.packables.addRelation(obj)
    39              else:
    40                  if isinstance(self, ZenPackable):
    41                      self.buildRelations()
    42                      pack.packables.addRelation(self)
    43                  else:
    44                      message = 'Nothing to save'
    45          if REQUEST:
    46              if isinstance(pack, ZenPack):
    47                  messaging.IMessageSender(self).sendToBrowser(
    48                      'Add To ZenPack', message)
    49              return REQUEST['RESPONSE'].redirect('/zport/dmd/ZenPackManager' +
    50                      '/packs/%s' % pack.id if pack else '')
    51
    52 -    def findObject(self, id):
    53          "Ugly hack for inconsistent object structure accross Organizers"
    54          result = []
    55          try:
    56              result.append(self._getOb(id))
    57          except AttributeError:
    58              pass
    59          for name, relationship in self._relations:
    60              try:
    61                  result.append(getattr(self, name)._getOb(id))
    62              except AttributeError:
    63                  pass
    64          if len(result) == 0:
    65              try:
    66                  result.append(self.dmd.unrestrictedTraverse(id))
    67              except KeyError:
    68                  pass
    69          if len(result) == 1:
    70              return result[0]
    71          raise AttributeError('Cannot find a unique %s on %s' % (id, self.id))
    72
    73
    74 -    def eligiblePacks(self):
    75          """
    76          Return a list of zenpacks that objects can be added to.  (The
    77          development mode zenpacks.)
    78          """
    79          return [zp for zp in self.dmd.ZenPackManager.packs()
    80                  if zp.isDevelopment()]
    81