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

Source Code for Module Products.ZenModel.browser.DeviceList

  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   
 14  from Products.Five.browser import BrowserView 
 15  from Products.ZenModel.DeviceOrganizer import DeviceOrganizer 
 16  from Products.ZenUtils.jsonutils import json 
 17  from Products.ZenUtils.Utils import formreq, unused, ipsort 
 18  from Products.AdvancedQuery import MatchRegexp, Or, Eq, In 
 19  from Products.ZenUtils.FakeRequest import FakeRequest 
 20  from Products.ZenWidgets import messaging 
 21  from Products.ZenWidgets.interfaces import IMessageSender 
22 23 -class AdvancedQueryDeviceList(object):
24 """ 25 Adapter providing list of devices according to various criteria. 26 """
27 - def __init__(self, context):
28 self.context = context
29
30 - def __call__(self, *args, **kwargs):
31 """ 32 Needs to be definition rather than simple reference due to possibility 33 of monkeypatching the hook. 34 """ 35 return self._getAdvancedQueryDeviceList(*args, **kwargs)
36
37 - def _getAdvancedQueryDeviceList(self, offset=0, count=50, filter='', 38 orderby='titleOrId', orderdir='asc'):
39 """ 40 Ask the catalog for devices matching the criteria specified. 41 """ 42 context = self.context 43 if not isinstance(context, DeviceOrganizer): 44 context = self.context.dmd.Devices 45 catalog = getattr(context, context.default_catalog) 46 filter = '(?is).*%s.*' % filter 47 filterquery = Or( 48 MatchRegexp('id', filter), 49 MatchRegexp('titleOrId', filter), 50 MatchRegexp('getDeviceIp', filter), 51 MatchRegexp('getProdState', filter), 52 MatchRegexp('getDeviceClassPath', filter) 53 ) 54 query = Eq('getPhysicalPath', context.absolute_url_path() 55 ) & filterquery 56 objects = catalog.evalAdvancedQuery(query, ((orderby, orderdir),)) 57 objects = list(objects) 58 totalCount = len(objects) 59 offset, count = int(offset), int(count) 60 obs = objects[offset:offset+count] 61 return totalCount, obs
62
63 64 -class DeviceList(BrowserView):
65 """ 66 Populates the device list. 67 """ 68 @formreq
69 - def __call__(self, *args, **kwargs):
70 return self._getJSONDeviceInfo(*args, **kwargs)
71 72 @json
73 - def _getJSONDeviceInfo(self, offset=0, count=50, filter='', 74 orderby='titleOrId', orderdir='asc'):
75 """ 76 Get devices under self according to criteria and return results as 77 JSON. 78 79 @return: A JSON representation of a tuple containing a list of lists of 80 device info, and the total number of matching devices 81 @rtype: "([[a, b, c], [a, b, c]], 17)" 82 """ 83 devList = AdvancedQueryDeviceList(self.context) 84 totalCount, devicelist = devList(offset, count, filter, orderby, 85 orderdir) 86 obs = [x.getObject() for x in devicelist] 87 if orderby=='getDeviceIp': 88 obs.sort(lambda a,b:ipsort(a.getDeviceIp(), b.getDeviceIp())) 89 if orderdir=='desc': obs.reverse() 90 results = [ob.getDataForJSON(minSeverity=2) + ['odd'] for ob in obs] 91 return results, totalCount
92
93 94 -class DeviceBatch(BrowserView):
95 """ 96 Given various criteria, figure out what devices are relevant and execute 97 the action specified. 98 """ 99 @formreq
100 - def __call__(self, *args, **kwargs):
101 return self._setDeviceBatchProps(*args, **kwargs)
102 103 @property
104 - def id(self):
105 """ 106 This can appear in the acquisition chain, and ZenModelBase.getDmd needs 107 an id attribute. 108 """ 109 return self.context.id
110
111 - def _setDeviceBatchProps(self, method='', extraarg=None, 112 selectstatus='none', goodevids=[], 113 badevids=[], offset=0, count=50, filter='', 114 orderby='titleOrId', orderdir='asc', **kwargs):
115 d = {'lockDevicesFromUpdates':'sendEventWhenBlocked', 116 'lockDevicesFromDeletion':'sendEventWhenBlocked', 117 'unlockDevices':'', 118 'setGroups':'groupPaths', 119 'setSystems':'systemPaths', 120 'setLocation':'locationPath', 121 'setPerformanceMonitor':'performanceMonitor', 122 'moveDevices':'moveTarget', 123 'removeDevices':('deleteStatus', 'deleteHistory', 'deletePerf'), 124 'setProdState':'state', 125 'setPriority':'priority' 126 } 127 if not method or not method in d: 128 IMessageSender(self.request).sendToBrowser( 129 'Unable to Perform Action', 130 'An empty or invalid action was attempted.', 131 priority=messaging.CRITICAL 132 ) 133 return self() 134 135 request = FakeRequest() 136 argdict = dict(REQUEST=request) 137 if d[method]: 138 d_method = d[method] 139 if isinstance(d_method, (tuple, list)): 140 for argName in d_method: 141 argdict[argName] = self.request.get(argName, None) 142 else: 143 argdict[d_method] = extraarg 144 action = getattr(self.context, method) 145 argdict['deviceNames'] = self._getDeviceBatch(selectstatus, 146 goodevids, badevids, offset, count, 147 filter, orderby, orderdir) 148 # This will call the method on the context, which will redirect to a 149 # new (or the same) screen and set a message 150 try: 151 result = action(**argdict) 152 except: 153 msgs = {'lockDevicesFromUpdates':'lock devices from updates', 154 'lockDevicesFromDeletion':'lock devices from deletion', 155 'unlockDevices':'unlock devices', 156 'setGroups':'change device groups', 157 'setSystems':'change device systems', 158 'setLocation':'set the location', 159 'setPerformanceMonitor':'set the performance monitor', 160 'moveDevices':'move devices', 161 'removeDevices':'delete devices', 162 'setProdState':'set production state', 163 'setPriority':'set priority' 164 } 165 IMessageSender(self.request).sendToBrowser( 166 'Unable to Perform Action', 167 'There was an error attempting to %s.' % msgs[method], 168 priority=messaging.CRITICAL 169 ) 170 else: 171 return result
172
173 - def _getDeviceBatch(self, selectstatus='none', goodevids=[], 174 badevids=[], offset=0, count=50, filter='', 175 orderby='titleOrId', orderdir='asc'):
176 unused(count, offset, orderby, orderdir) 177 if not isinstance(goodevids, (list, tuple)): 178 goodevids = [goodevids] 179 if not isinstance(badevids, (list, tuple)): 180 badevids = [badevids] 181 if selectstatus=='all': 182 idquery = ~In('id', badevids) 183 else: 184 idquery = In('id', goodevids) 185 filter = '(?is).*%s.*' % filter 186 filterquery = Or( 187 MatchRegexp('id', filter), 188 MatchRegexp('titleOrId', filter), 189 MatchRegexp('getDeviceIp', filter), 190 MatchRegexp('getProdState', filter), 191 MatchRegexp('getDeviceClassPath', filter) 192 ) 193 query = Eq('getPhysicalPath', self.context.absolute_url_path()) & idquery 194 query = query & filterquery 195 catalog = getattr(self.context, self.context.default_catalog) 196 objects = catalog.evalAdvancedQuery(query) 197 return [x['id'] for x in objects]
198