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

Source Code for Module Products.ZenModel.browser.Autocompleters

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