Package ZenModel :: Module DeviceReport
[hide private]
[frames] | no frames]

Source Code for Module ZenModel.DeviceReport

  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  import cgi 
 15  import types 
 16   
 17  from Globals import InitializeClass 
 18  from AccessControl import ClassSecurityInfo 
 19   
 20  from Products.ZenUtils.ZenTales import talesEval 
 21  from Products.ZenWidgets import messaging 
 22   
 23  #from Report import Report 
 24  from ZenModelRM import ZenModelRM 
 25   
26 -def manage_addDeviceReport(context, id, title = None, REQUEST = None):
27 """Add a DeviceReport 28 """ 29 dc = DeviceReport(id, title) 30 context._setObject(id, dc) 31 if REQUEST is not None: 32 messaging.IMessageSender(context).sendToBrowser( 33 'Report Created', 34 'Device report %s was created.' % id 35 ) 36 return REQUEST['RESPONSE'].redirect(context.absolute_url() + '/manage_main')
37 38 39
40 -class DeviceReport(ZenModelRM):
41 42 meta_type = "DeviceReport" 43 44 path = "/" 45 deviceQuery = "" 46 sortedHeader = "" 47 sortedSence = "asc" 48 groupby = "" 49 columns = [] 50 colnames = [] 51 52 _properties = ZenModelRM._properties + ( 53 {'id':'path', 'type':'string', 'mode':'w'}, 54 {'id':'deviceQuery', 'type':'string', 'mode':'w'}, 55 {'id':'sortedHeader', 'type':'string', 'mode':'w'}, 56 {'id':'sortedSence', 'type':'string', 'mode':'w'}, 57 {'id':'groupby', 'type':'string', 'mode':'w'}, 58 {'id':'columns', 'type':'lines', 'mode':'w'}, 59 {'id':'colnames', 'type':'lines', 'mode':'w'}, 60 ) 61 62 63 # Screen action bindings (and tab definitions) 64 factory_type_information = ( 65 { 66 'immediate_view' : 'viewDeviceReport', 67 'actions' : 68 ( 69 {'name' : 'Report', 70 'action' : 'viewDeviceReport', 71 'permissions' : ("View",), 72 }, 73 {'name' : 'Edit', 74 'action' : 'editDeviceReport', 75 'permissions' : ("Manage DMD",), 76 }, 77 ) 78 }, 79 ) 80 81 security = ClassSecurityInfo() 82 83
84 - def getDevices(self):
85 """Return the device list for this report. 86 """ 87 devs = self.getDmdRoot("Devices") 88 if self.path != "/": devs = devs.getOrganizer(self.path) 89 devlist = devs.getSubDevices() 90 if self.deviceQuery: 91 try: 92 return [ dev for dev in devlist \ 93 if talesEval("python:"+self.deviceQuery, dev) ] 94 except Exception, e: 95 return e 96 return devlist
97 98
99 - def testQueryStyle(self):
100 """Return red text style if query is bad. 101 """ 102 try: 103 self.getDevices() 104 except: 105 return "color:#FF0000"
106 107
108 - def testColNamesStyle(self):
109 """Return red text style if columns and colnames not the same length. 110 """ 111 if len(self.columns) != len(self.colnames): return "color:#FF0000"
112 113
114 - def reportHeader(self):
115 h = [] 116 tname = self.getPrimaryId() 117 for i, field in enumerate(self.columns): 118 try:name = self.colnames[i] 119 except IndexError: name = field 120 h.append(self.ZenTableManager.getTableHeader(tname , field, name)) 121 return "\n".join(h)
122 123
124 - def reportHeaders(self):
125 h = [] 126 for i, field in enumerate(self.columns): 127 try:name = self.colnames[i] 128 except IndexError: name = field 129 h.append((field, name)) 130 return h
131 132
133 - def reportBody(self, batch):
134 """body of this report create from a filtered and sorted batch. 135 """ 136 body = [] 137 for dev in batch: 138 # If the query is invalid, dev will be an exception string 139 if type(dev) in types.StringTypes: 140 body.extend([ 141 '<tr class="tablevalues">', 142 ' <td colspan="%d" align="center">' % len(self.columns), 143 ' Query error: %s' % dev, 144 ' </td>', 145 '</tr>', 146 ]) 147 else: 148 body.append("<tr class='tablevalues'>") 149 for field in self.columns: 150 body.append("<td>") 151 if field == "getId": field += "Link" 152 attr = getattr(dev, field, 'Unknown column') 153 if callable(attr): value = attr() 154 else: value = attr 155 if type(value) in (types.ListType, types.TupleType): 156 value = ", ".join(value) 157 if (not field.endswith("Link") 158 and type(value) in types.StringTypes): 159 value = cgi.escape(value) 160 elif type(value) not in types.StringTypes: 161 value = str(value) 162 body.append(value) 163 body.append("</td>") 164 body.append("</tr>") 165 166 return "\n".join(body)
167 168 169 InitializeClass(DeviceReport) 170