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

# Source Code for Module Products.ZenModel.Collection

      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  __doc__="""Collection
     12  Holds an assortment of devices and/or components on a multi-style report.
     13  """
     14  import sys
     15
     16  from Globals import InitializeClass
     17  from AccessControl import ClassSecurityInfo, Permissions
     18  from Globals import DTMLFile
     19  from Products.ZenRelations.RelSchema import *
     20  from ZenModelRM import ZenModelRM
     21  from Products.ZenUtils.Utils import resequence
     22  from Products.ZenWidgets import messaging
     23  from Products.ZenUtils.deprecated import deprecated
     24  from Products.ZenMessaging.audit import audit
     25
     26  @deprecated
     27 -def manage_addCollection(context, id, REQUEST = None):
     28      ''' This is here so than zope will let us copy/paste/rename
     29      Collections.
     30      '''
     31      c = Collection(id)
     32      context._setObject(id, c)
     33      if REQUEST is not None:
     34          return REQUEST['RESPONSE'].redirect(context.absolute_url() +'/manage_main')
     35
     36  addCollection = DTMLFile('dtml/addCollection',globals())
     37
     38
     39 -class Collection(ZenModelRM):
     40      """
     41      Holds an assortment of devices and/or components on a multi-style report.
     42      """
     43
     44      meta_type = 'Collection'
     45
     46      _properties = (
     47          )
     48
     49      _relations =  (
     50          ('report',
     51              ToOne(ToManyCont, 'Products.ZenModel.MultiGraphReport', 'collections')),
     52          ('collection_items',
     53              ToManyCont(ToOne, 'Products.ZenModel.CollectionItem', 'collection')),
     54          )
     55
     56      factory_type_information = (
     57      {
     58          'immediate_view' : 'editCollection',
     59          'actions'        :
     60          (
     61              { 'id'            : 'edit'
     62              , 'name'          : 'Collection'
     63              , 'action'        : 'editCollection'
     64              , 'permissions'   : ( Permissions.view, )
     65              },
     66          )
     67      },
     68      )
     69
     70      security = ClassSecurityInfo()
     71
     72
     73 -    def createCollectionItem(self, orgPath='', devId='', compPath='',
     74                              recurse=False, checkExists=False):
     75          ''' Create and insert a new CollectionItem based either on the
     76          orgPath or on devId/compPath.  Returns the new item.
     77          '''
     78          from CollectionItem import CollectionItem
     79          ci = CollectionItem(self.getUnusedId('collection_items', 'Item'))
     80          if orgPath:
     81              ci.deviceOrganizer = orgPath
     82          else:
     83              ci.deviceId = devId
     84              ci.compPath = compPath
     85          ci.recurse = recurse
     86          ci.sequence = len(self.collection_items())
     87          self.collection_items._setObject(ci.id, ci)
     88          ci = self.collection_items._getOb(ci.id)
     89          # This check happens after the _setObject so that ci has full
     90          # aq wrapper in case it needs it.
     91          if checkExists and not ci.getRepresentedItem():
     92              self.collection_items._delObject(ci.id)
     93              ci = None
     94          return ci
     95
     96
     97      security.declareProtected('Manage DMD', 'manage_addCollectionItem')
     98 -    def manage_addCollectionItem(self, itemType,
     99              deviceIds=(), componentPaths=(), deviceClasses=(), systems=(),
    100              groups=(), locations=(), recurse=False, REQUEST=None):
    101          ''' Create a new CollectionItem and add to this collection
    102          '''
    103          count = 0
    104          if itemType == 'devcomp':
    105              if not deviceIds:
    106                  deviceIds = []
    107              if not componentPaths:
    108                  componentPaths = ['']
    109              for i, devId in enumerate(deviceIds):
    110                  for cPath in componentPaths:
    111                      ci = self.createCollectionItem(devId=devId, compPath=cPath,
    112                          recurse=False, checkExists=True)
    113                      if ci:
    114                          count += 1
    115          if itemType == 'deviceClass':
    116              for dClass in deviceClasses:
    117                  self.createCollectionItem(
    118                                      orgPath='/Devices' + dClass, recurse=recurse)
    119              count += 1
    120          if itemType == 'system':
    121              for system in systems:
    122                  self.createCollectionItem(
    123                                      orgPath='/Systems' + system, recurse=recurse)
    124                  count += 1
    125          if itemType == 'group':
    126              for group in groups:
    127                  self.createCollectionItem(
    128                                      orgPath='/Groups' + group, recurse=recurse)
    129                  count += 1
    130          if itemType == 'location':
    131              for loc in locations:
    132                  self.createCollectionItem(
    133                                      orgPath='/Locations' + loc, recurse=recurse)
    134                  count += 1
    135
    136          if REQUEST:
    137              audit('UI.Collection.AddItem', self.id, itemType=itemType, deviceIds=deviceIds,
    138                    componentPaths=componentPaths, deviceClasses=deviceClasses, systems=systems,
    139                    groups=groups, locations=locations)
    140              messaging.IMessageSender(self).sendToBrowser(
    141                  'Items Added',
    142                  ' %s item%s added' % (count, count > 1 and 's' or '')
    143              )
    144              return self.callZenScreen(REQUEST)
    145
    146
    147      security.declareProtected('Manage DMD', 'manage_deleteCollectionItems')
    148 -    def manage_deleteCollectionItems(self, ids=(), REQUEST=None):
    149          """ Delete collection items from this report
    150          """
    151          for id in ids:
    152              deletedItem = self.collection_items._getOb(id)
    153              self.collection_items._delObject(id)
    154              if REQUEST:
    155                  audit('UI.Collection.DeleteItem', self.id, item=deletedItem.id,
    156                        contents=deletedItem.getRepresentedItem().id)
    157
    158          self.manage_resequenceCollectionItems()
    159          if REQUEST:
    160              count = len(ids)
    161              messaging.IMessageSender(self).sendToBrowser(
    162                  'Items Deleted',
    163                  ' %s item%s deleted' % (count, count > 1 and 's' or '')
    164              )
    165              return self.callZenScreen(REQUEST)
    166
    167
    168      security.declareProtected('Manage DMD', 'manage_resequenceCollectionItems')
    169 -    def manage_resequenceCollectionItems(self, seqmap=(), origseq=(),
    170                                                                  REQUEST=None):
    171          """Reorder the sequence of the items.
    172          """
    173          retval = resequence(self, self.collection_items(), seqmap, origseq, REQUEST)
    174          if REQUEST:
    175              audit('UI.Collection.ResequenceItems', self.id, sequence=seqmap,
    176                    oldData_={'sequence':origseq})
    177          return retval
    178
    179
    180      security.declareProtected('Manage DMD', 'getItems')
    181 -    def getItems(self):
    182          ''' Return an ordered list of CollectionItems
    183          '''
    184          def itemKey(a):
    185              try:
    186                  return int(a.sequence)
    187              except ValueError:
    188                  return sys.maxint
    189          return sorted(self.collection_items(), key=itemKey)
    190
    191
    192 -    def getNumItems(self):
    193          ''' Return the number of collection items
    194          '''
    195          return len(self.collection_items())
    196
    197
    198 -    def getDevicesAndComponents(self):
    199          ''' Return a deduped list of devices and components represented
    200          by this collection's collectionitems
    201          '''
    202          things = []
    203          tset = set()
    204          for collectionItem in self.getItems():
    205              devsAndComps = collectionItem.getDevicesAndComponents()
    206              for devOrComp in devsAndComps:
    207                  tid = devOrComp.getPrimaryId()
    208                  if tid not in tset:
    209                      tset.add(tid)
    210                      things.append(devOrComp)
    211          return things
    212
    213  InitializeClass(Collection)
    214