1
2
3
4
5
6
7
8
9
10
11
12
13
14 __doc__="""Collection
15
16 Collection is a grouping of devices and components.
17 """
18
19
20 from Globals import InitializeClass
21 from AccessControl import ClassSecurityInfo, Permissions
22 from Globals import DTMLFile
23 from Products.ZenRelations.RelSchema import *
24 from ZenModelRM import ZenModelRM
25 from Products.ZenWidgets import messaging
26
27
36
37 addCollection = DTMLFile('dtml/addCollection',globals())
38
39
41 ''' Holds an assortment of devices and/or components.
42 '''
43
44 meta_type = 'Collection'
45
46 _properties = (
47 )
48
49 _relations = (
50 ('report',
51 ToOne(ToManyCont, 'Products.ZenModel.MultiGraphReport', 'collections')),
52 ('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):
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 messaging.IMessageSender(self).sendToBrowser(
138 'Items Added',
139 ' %s item%s added' % (count, count > 1 and 's' or '')
140 )
141 return self.callZenScreen(REQUEST)
142
143
144 security.declareProtected('Manage DMD', 'manage_deleteCollectionItems')
158
159
160 security.declareProtected('Manage DMD', 'manage_resequenceCollectionItems')
167
168
169 security.declareProtected('Manage DMD', 'getItems')
171 ''' Return an ordered list of CollectionItems
172 '''
173 import sys
174 def cmpItems(a, b):
175 try: a = int(a.sequence)
176 except ValueError: a = sys.maxint
177 try: b = int(b.sequence)
178 except ValueError: b = sys.maxint
179 return cmp(a, b)
180 items = self.items()[:]
181 items.sort(cmpItems)
182 return items
183
184
186 ''' Return the number of collection items
187 '''
188 return len(self.items())
189
190
192 ''' Return a deduped list of devices and components represented
193 by this collection's collectionitems
194 '''
195 things = []
196 tset = set()
197 for collectionItem in self.getItems():
198 devsAndComps = collectionItem.getDevicesAndComponents()
199 for devOrComp in devsAndComps:
200 tid = devOrComp.getPrimaryId()
201 if tid not in tset:
202 tset.add(tid)
203 things.append(devOrComp)
204 return things
205
206 InitializeClass(Collection)
207