1
2
3
4
5
6
7
8
9
10
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.ZenUtils.Utils import convToUnits, zdecode
22 from Products.ZenWidgets import messaging
23
24
25
26 from ZenModelRM import ZenModelRM
27
39
40
41
43
44 meta_type = "DeviceReport"
45
46 path = "/"
47 deviceQuery = ""
48 sortedHeader = ""
49 sortedSence = "asc"
50 groupby = ""
51 columns = []
52 colnames = []
53
54 _properties = ZenModelRM._properties + (
55 {'id':'path', 'type':'string', 'mode':'w'},
56 {'id':'deviceQuery', 'type':'string', 'mode':'w'},
57 {'id':'sortedHeader', 'type':'string', 'mode':'w'},
58 {'id':'sortedSence', 'type':'string', 'mode':'w'},
59 {'id':'groupby', 'type':'string', 'mode':'w'},
60 {'id':'columns', 'type':'lines', 'mode':'w'},
61 {'id':'colnames', 'type':'lines', 'mode':'w'},
62 )
63
64
65
66 factory_type_information = (
67 {
68 'immediate_view' : 'viewDeviceReport',
69 'actions' :
70 (
71 {'name' : 'Report',
72 'action' : 'viewDeviceReport',
73 'permissions' : ("View",),
74 },
75 {'name' : 'Edit',
76 'action' : 'editDeviceReport',
77 'permissions' : ("Manage DMD",),
78 },
79 )
80 },
81 )
82
83 security = ClassSecurityInfo()
84
85
99
100
102 """Return red text style if query is bad.
103 """
104 try:
105 self.getDevices()
106 except:
107 return "color:#FF0000"
108
109
111 """Return red text style if columns and colnames not the same length.
112 """
113 if len(self.columns) != len(self.colnames): return "color:#FF0000"
114
115
124
125
127 h = []
128 for i, field in enumerate(self.columns):
129 try:name = self.colnames[i]
130 except IndexError: name = field
131 h.append((field, name))
132 return h
133
134
135 - def reportBody(self, batch):
136 """body of this report create from a filtered and sorted batch.
137 """
138 body = []
139 for dev in batch:
140
141 if type(dev) in types.StringTypes:
142 body.extend([
143 '<tr class="tablevalues">',
144 ' <td colspan="%d" align="center">' % len(self.columns),
145 ' Query error: %s' % dev,
146 ' </td>',
147 '</tr>',
148 ])
149 else:
150 body.append("<tr class='tablevalues'>")
151 for field in self.columns:
152 body.append("<td>")
153 if field == "getId": field += "Link"
154
155
156 attr = getattr(dev, field, 'Unknown column')
157 variables_and_funcs = {
158 'device':dev, 'dev':dev, 'attr':attr,
159 'convToUnits':convToUnits, 'zdecode':zdecode,
160 }
161 if field.startswith('python:'):
162 expression = field.replace('python:', 'attr=')
163 try:
164 exec(expression, variables_and_funcs)
165 attr = variables_and_funcs['attr']
166 except Exception, ex:
167 attr = str(ex)
168
169 if callable(attr):
170 try: value = attr()
171 except Exception, ex:
172 value = str(ex)
173 else: value = attr
174
175 if type(value) in (types.ListType, types.TupleType):
176
177 try: value = ", ".join(value)
178 except Exception, ex:
179 value = str(ex)
180 if (not field.endswith("Link")
181 and type(value) in types.StringTypes):
182 value = cgi.escape(value)
183 elif type(value) not in types.StringTypes:
184 value = str(value)
185 body.append(value)
186 body.append("</td>")
187 body.append("</tr>")
188
189 return "\n".join(body)
190
191
192 InitializeClass(DeviceReport)
193