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

Source Code for Module ZenModel.browser.Autocompleters

  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  from Products.Five.browser import BrowserView 
 15  from Products.ZenUtils.json import json 
 16  from Products.ZenUtils.Utils import formreq 
 17  from Products.AdvancedQuery import MatchGlob 
 18   
 19   
20 -class DeviceNames(BrowserView):
21 """ 22 Provides device names for autocompleter population. 23 24 Adapts DeviceClasses. 25 """ 26 @json 27 @formreq
28 - def __call__(self, query=''):
29 """ 30 @param query: A glob by which to filter device names 31 @type query: str 32 @return: A JSON representation of a list of ids 33 @rtype: "['id1', 'id2', 'id3']" 34 """ 35 brains = self.context.dmd.Devices.deviceSearch.evalAdvancedQuery( 36 MatchGlob('id', query.rstrip('*') + '*')) 37 deviceIds = [b.id for b in brains] 38 deviceIds.sort(lambda x, y: cmp(x.lower(), y.lower())) 39 return deviceIds
40 41
42 -class ComponentPaths(BrowserView):
43 """ 44 Get component paths and names associated with a given device or group of 45 devices. 46 47 Adapts DeviceClasses. 48 """ 49 @json 50 @formreq
51 - def __call__(self, deviceIds=()):
52 """ 53 @param deviceIds: One ore more device ids under which components should be 54 sought 55 @type deviceIds: str 56 @return: A JSON representation of a list of tuples describing components 57 under devices specified 58 @rtype: "[('/path/to/comp1', 'comp1'), ...]" 59 """ 60 paths = set() 61 if isinstance(deviceIds, basestring): 62 deviceIds = [deviceIds] 63 for devId in deviceIds: 64 d = self.context.findDevice(devId) 65 if d: 66 dPathLen = len(d.getPrimaryId()) + 1 67 for comp in d.getMonitoredComponents(): 68 paths.add((comp.getPrimaryId()[dPathLen:], comp.name())) 69 paths = list(paths) 70 paths.sort(lambda x,y: cmp(x[0], y[0])) 71 return paths
72 73
74 -class GraphIds(BrowserView):
75 """ 76 Get a list of the graph defs available for the given device 77 and component. 78 79 Adapts DeviceClasses. 80 """ 81 @json 82 @formreq
83 - def __call__(self, deviceIds=(), componentPaths=()):
84 """ 85 @param deviceIds: One ore more device ids under which graphs should be 86 sought 87 @type deviceIds: str, list 88 @param componentPaths: Path(s) to components under which graphs should 89 be sought 90 @type componentPaths: str, list 91 @return: A JSON representation of a list of ids 92 @rtype: "['id1', 'id2', 'id3']" 93 """ 94 graphIds = set() 95 if isinstance(deviceIds, basestring): 96 deviceIds = [deviceIds] 97 if isinstance(componentPaths, basestring): 98 componentPaths = [componentPaths] 99 if not componentPaths: 100 componentPaths = ('',) 101 for devId in deviceIds: 102 thing = self.context.findDevice(devId) 103 if thing: 104 for compPath in componentPaths: 105 compPath = compPath or '' 106 parts = compPath.split('/') 107 for part in parts: 108 if part: 109 if hasattr(thing, part): 110 thing = getattr(thing, part) 111 else: 112 break 113 else: 114 for t in thing.getRRDTemplates(): 115 for g in t.getGraphDefs(): 116 graphIds.add(g.id) 117 graphIds = list(graphIds) 118 graphIds.sort() 119 return graphIds
120 121
122 -class ServiceList(BrowserView):
123 """ 124 Get a list of id and descriptions for a live search 125 126 """ 127 @json 128 @formreq
129 - def __call__(self, dataRoot='serviceclasses'):
130 """ 131 @param dataRoot: The name of the relation under which services should 132 be sought 133 @type dataRoot: str 134 @return: A JSON representation of a list of service ids 135 @rtype: "['id1', 'id2', ...]" 136 """ 137 liveSearchList = [] 138 for srv in self.context.getSubInstancesGen(rel='serviceclasses'): 139 if getattr(srv, 'description', None): 140 liveSearchList.append('%s [%s]' % (srv.id, srv.description)) 141 else: 142 liveSearchList.append(srv.id) 143 return liveSearchList
144 145
146 -class EventClassNames(BrowserView):
147 """ 148 Get a list of all event classes that match the filter. 149 """ 150 @json 151 @formreq
152 - def __call__(self):
153 """ 154 @return: A JSON representation of a list of paths 155 @rtype: "['/path/1', '/path/2', ...]" 156 """ 157 orgs = self.context.dmd.Events.getSubOrganizers() 158 paths = ['/'.join(x.getPrimaryPath()) for x in orgs] 159 paths = [p.replace('/zport/dmd','') for p in paths] 160 return paths
161 162
163 -class OrganizerNames(BrowserView):
164 """ 165 Return the organizer names to which this user has access 166 """ 167 @json 168 @formreq
169 - def __call__(self, dataRoot="Devices"):
170 """ 171 @return: A JSON representation of a list of organizers 172 @rtype: "['/Systems/Sys1', '/Groups/Group1', ...]" 173 """ 174 root = self.context.dmd.getDmdRoot(dataRoot) 175 return root.getOrganizerNames()
176