1
2
3
4
5
6
7
8
9
10
11
12
13
14 import re
15
16 from OFS.PropertyManager import PropertyManager
17 from zExceptions import BadRequest
18 from Globals import DTMLFile
19 from Globals import InitializeClass
20 from Acquisition import aq_base, aq_chain
21 from ZPublisher.Converters import type_converters
22 from Products.ZenModel.ZenossSecurity import *
23 from AccessControl import ClassSecurityInfo
24 from Exceptions import zenmarker
25 iszprop = re.compile("^z[A-Z]").search
26
27 from Products.ZenUtils.Utils import unused
28
30 """
31 ZenPropertyManager adds keyedselection type to PropertyManager.
32 A keyedselection displayes a different name in the popup then
33 the actual value the popup will have.
34
35 It also has management for zenProperties which are properties that can be
36 inherited long the acquision chain. All properties are for a branch are
37 defined on a "root node" specified by the function which must be returned
38 by the function getZenRootNode that should be over ridden in a sub class.
39 Prperties can then be added further "down" the aq_chain by calling
40 setZenProperty on any contained node.
41
42 ZenProperties all have the same prefix which is defined by iszprop
43 this can be overridden in a subclass.
44 """
45 __pychecker__='no-override'
46
47 security = ClassSecurityInfo()
48
49 manage_propertiesForm=DTMLFile('dtml/properties', globals(),
50 property_extensible_schema__=1)
51
53 """override from PerpertyManager to handle checks and ip creation"""
54 self._wrapperCheck(value)
55 if self.getPropertyType(id) == 'keyedselection':
56 value = int(value)
57 if not getattr(self,'_v_propdict',False):
58 self._v_propdict = self.propdict()
59 if self._v_propdict.has_key('setter'):
60 settername = self._v_propdict['setter']
61 setter = getattr(aq_base(self), settername, None)
62 if not setter:
63 raise ValueError("setter %s for property %s doesn't exist"
64 % (settername, id))
65 if not callable(setter):
66 raise TypeError("setter %s for property %s not callable"
67 % (settername, id))
68 setter(value)
69 else:
70 setattr(self,id,value)
71
72
73 - def _setProperty(self, id, value, type='string', label=None,
74 visible=True, setter=None):
75 """for selection and multiple selection properties
76 the value argument indicates the select variable
77 of the property
78 """
79 self._wrapperCheck(value)
80 if not self.valid_property_id(id):
81 raise BadRequest, 'Id %s is invalid or duplicate' % id
82
83 def setprops(**pschema):
84 self._properties=self._properties+(pschema,)
85 if setter: pschema['setter'] = setter
86 if label: pschema['label'] = label
87
88 if type in ('selection', 'multiple selection'):
89 if not hasattr(self, value):
90 raise BadRequest, 'No select variable %s' % value
91 setprops(id=id,type=type, visible=visible,
92 select_variable=value)
93 if type=='selection':
94 self._setPropValue(id, '')
95 else:
96 self._setPropValue(id, [])
97 else:
98 setprops(id=id, type=type, visible=visible)
99 self._setPropValue(id, value)
100
101
103 """ This method sets a property on a zope object. It overrides the
104 method in PropertyManager. If Zope is upgraded you will need to check
105 that this method has not changed! It is overridden so that we can catch
106 the ValueError returned from the field2* converters in the class
107 Converters.py
108 """
109 from Products.ZenWidgets import messaging
110 try:
111 super(ZenPropertyManager, self)._updateProperty(id, value)
112 except ValueError:
113 proptype = self.getPropertyType(id)
114 messaging.IMessageSender(self).sendToBrowser(
115 'Error Saving Property %s' % id,
116 ("New value '%s' is of invalid type. "
117 "It should be type '%s'") % (value, proptype),
118 priority=messaging.CRITICAL
119 )
120
121
122 _onlystars = re.compile("^\*+$").search
123 security.declareProtected(ZEN_ZPROPERTIES_EDIT, 'manage_editProperties')
125 """
126 Edit object properties via the web.
127 The purpose of this method is to change all property values,
128 even those not listed in REQUEST; otherwise checkboxes that
129 get turned off will be ignored. Use manage_changeProperties()
130 instead for most situations.
131 """
132 for prop in self._propertyMap():
133 name=prop['id']
134 if 'w' in prop.get('mode', 'wd'):
135 value=REQUEST.get(name, '')
136 if self.zenPropIsPassword(name) and self._onlystars(value):
137 continue
138 self._updateProperty(name, value)
139 if getattr(self, "index_object", False):
140 self.index_object()
141 if REQUEST:
142 message="Saved changes."
143 return self.manage_propertiesForm(self,REQUEST,
144 manage_tabs_message=message)
145
146
148 """sub class must implement to use zenProperties."""
149 raise NotImplementedError
150
151 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyIds')
153 """
154 Return list of device tree property names.
155 If all use list from property root node.
156 """
157 if all:
158 rootnode = self.getZenRootNode()
159 else:
160 if self.id == self.dmdRootName: return []
161 rootnode = aq_base(self)
162 props = []
163 for prop in rootnode.propertyIds():
164 if not pfilt(prop): continue
165 props.append(prop)
166 props.sort()
167 return props
168
169 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyItems')
171 """Return list of (id, value) tuples of zenProperties.
172 """
173 return map(lambda x: (x, getattr(self, x)), self.zenPropertyIds())
174
175 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyMap')
177 """Return property mapping of device tree properties."""
178 rootnode = self.getZenRootNode()
179 pmap = []
180 for pdict in rootnode.propertyMap():
181 if pfilt(pdict['id']): pmap.append(pdict)
182 pmap.sort(lambda x, y: cmp(x['id'], y['id']))
183 return pmap
184
185 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyString')
196
197 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropIsPassword')
199 """Is this field a password field.
200 """
201 return id.endswith("assword")
202
203 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyPath')
209
210 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyType')
212 """Return the type of this property."""
213 ptype = self.getZenRootNode().getPropertyType(id)
214 if not ptype: ptype = "string"
215 return ptype
216
217 security.declareProtected(ZEN_ZPROPERTIES_EDIT, 'setZenProperty')
219 """
220 Add or set the propvalue of the property propname on this node of
221 the device Class tree.
222 """
223 rootnode = self.getZenRootNode()
224 ptype = rootnode.getPropertyType(propname)
225 if ptype == 'lines':
226 dedupedList = []
227 for x in propvalue:
228 if x not in dedupedList:
229 dedupedList.append(x)
230 propvalue = dedupedList
231 if getattr(aq_base(self), propname, zenmarker) != zenmarker:
232 self._updateProperty(propname, propvalue)
233 else:
234 if ptype in ("selection", 'multiple selection'): ptype="string"
235 if type_converters.has_key(ptype):
236 propvalue=type_converters[ptype](propvalue)
237 if getattr(self, propname, None) != propvalue:
238 self._setProperty(propname, propvalue, type=ptype)
239 if REQUEST: return self.callZenScreen(REQUEST)
240
241 security.declareProtected(ZEN_ZPROPERTIES_EDIT, 'saveZenProperties')
256
257 security.declareProtected(ZEN_ZPROPERTIES_EDIT, 'deleteZenProperty')
259 """
260 Delete device tree properties from the this DeviceClass object.
261 """
262 if propname:
263 self._delProperty(propname)
264 if REQUEST: return self.callZenScreen(REQUEST)
265
266 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyOptions')
268 "Provide a set of default options for a ZProperty"
269 unused(propname)
270 return []
271
272 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'isLocal')
274 """Check to see if a name is local to our current context.
275 """
276 v = getattr(aq_base(self), propname, zenmarker)
277 return v != zenmarker
278
279 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'getOverriddenObjects')
298
299
300 InitializeClass(ZenPropertyManager)
301