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

# Source Code for Module Products.ZenModel.CollectionItem

      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
     14  __doc__="""CollectionItem
     15
     16  Defines attributes for how a data source will be graphed
     17  and builds the nessesary rrd commands.
     18  """
     19
     20  from Globals import InitializeClass
     21  from Globals import DTMLFile
     22  from AccessControl import ClassSecurityInfo, Permissions
     23  from Products.ZenRelations.RelSchema import *
     24  from ZenModelRM import ZenModelRM
     25  from Products.ZenUtils.Utils import unused
     26
     27
     28 -def manage_addCollectionItem(context, id, deviceId, compPath, sequence,
     29                                                              REQUEST = None):
     30      ''' This is here so than zope will let us copy/paste/rename
     31      CollectionItems.
     32      '''
     33      unused(deviceId, compPath, sequence)
     34      ci = CollectionItem(id)
     35      context._setObject(id, ci)
     36      ci.deviceId = deviceId
     37      ci.compPath = compPath
     38      ci.sequence = sequence
     39      if REQUEST is not None:
     40          return REQUEST['RESPONSE'].redirect(context.absolute_url() +'/manage_main')
     41
     42  addCollectionItem = DTMLFile('dtml/addCollectionItem',globals())
     43
     44
     45 -class CollectionItem(ZenModelRM):
     46
     47      meta_type = 'CollectionItem'
     48
     49      sequence = 0
     50      deviceId = ''
     51      compPath = ''
     52      deviceOrganizer = ''
     53      recurse = False
     54
     55      _properties = (
     56          {'id':'sequence', 'type':'long', 'mode':'w'},
     57          {'id':'deviceId', 'type':'string', 'mode':'w'},
     58          {'id':'compPath', 'type':'string', 'mode':'w'},
     59          {'id':'deviceOrganizer', 'type':'string', 'mode':'w'},
     60          {'id':'recurse', 'type':'boolean', 'mode':'w'},
     61          )
     62
     63      _relations = (
     64          ('collection', ToOne(ToManyCont,'Products.ZenModel.Collection','items')),
     65          )
     66
     67      factory_type_information = (
     68          {
     69              'immediate_view' : 'editCollectionItem',
     70              'actions'        :
     71              (
     72                  { 'id'            : 'edit'
     73                  , 'name'          : 'Collection Item'
     74                  , 'action'        : 'editCollectionItem'
     75                  , 'permissions'   : ( Permissions.view, )
     76                  },
     77              )
     78          },
     79      )
     80
     81      security = ClassSecurityInfo()
     82
     83 -    def __init__(self, id, deviceId='', compPath='', deviceOrganizer='',
     84                      recurse=False, sequence=0, title=None, buildRelations=True):
     85          ZenModelRM.__init__(self, id, title, buildRelations)
     86          self.deviceId = deviceId
     87          self.compPath = compPath
     88          self.deviceOrganizer = deviceOrganizer
     89          self.recurse = recurse
     90          self.sequence = sequence
     91
     92
     93 -    def getDesc(self, withLink=True):
     94          ''' Return a string that represents this item
     95          '''
     96          thing = self.getRepresentedItem()
     97          if not thing:
     98              desc = 'missing'
     99          elif self.deviceId:
    100              if self.compPath:
    101                  desc = '%s %s' % (self.deviceId, thing.name())
    102              else:
    103                  desc = self.deviceId
    104              if withLink and thing:
    105                  desc = '<a href="%s">%s</a>' % (thing.getPrimaryUrlPath(),desc)
    106          else:
    107              if withLink and thing:
    108                  desc = '<a href="%s">%s</a>' % (thing.getPrimaryUrlPath(),
    109                      self.deviceOrganizer)
    110              else:
    111                  desc = self.deviceOrganizer
    112              if self.recurse:
    113                  desc += ' and suborganizers'
    114          return desc
    115
    116
    117 -    def getRepresentedItem(self):
    118          ''' Get the device organizer, component or device
    119          that this collection item represents
    120          '''
    121          thing = None
    122          if self.deviceId:
    123              thing = self.dmd.Devices.findDevice(self.deviceId)
    124              if self.compPath:
    125                  for part in self.compPath.split('/'):
    126                      if part:
    127                          thing = getattr(thing, part, None)
    128                          if not thing:
    129                              break
    130          elif self.deviceOrganizer:
    131              try:
    132                  thing = self.dmd.getObjByPath(self.deviceOrganizer.lstrip('/'))
    133              except KeyError:
    134                  thing = None
    135          return thing
    136
    137
    138 -    def getDevicesAndComponents(self):
    139          ''' Return a list of the devices and components referenced by this item
    140          '''
    141          thing = self.getRepresentedItem()
    142          if not thing:
    143              stuff = []
    144          elif self.deviceId:
    145              stuff = [thing]
    146          elif self.recurse:
    147              stuff = thing.getSubDevices()
    148          else:
    149              stuff = thing.devices()
    150          return stuff
    151
    152
    153 -    def getNumDevicesAndComponents(self):
    154          ''' Return the number of devices and components matched by this item
    155          '''
    156          things = self.getDevicesAndComponents()
    157          return len(things)
    158
    159
    160
    161  InitializeClass(CollectionItem)
    162