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
26 iszprop = re.compile("^z[A-Z]").search
27
29 """
30 ZenPropertyManager adds keyedselection type to PropertyManager.
31 A keyedselection displayes a different name in the popup then
32 the actual value the popup will have.
33
34 It also has management for zenProperties which are properties that can be
35 inherited long the acquision chain. All properties are for a branch are
36 defined on a "root node" specified by the function which must be returned
37 by the function getZenRootNode that should be over ridden in a sub class.
38 Prperties can then be added further "down" the aq_chain by calling
39 setZenProperty on any contained node.
40
41 ZenProperties all have the same prefix which is defined by iszprop
42 this can be overridden in a subclass.
43 """
44 security = ClassSecurityInfo()
45
46 manage_propertiesForm=DTMLFile('dtml/properties', globals(),
47 property_extensible_schema__=1)
48
50 """override from PerpertyManager to handle checks and ip creation"""
51 self._wrapperCheck(value)
52 if self.getPropertyType(id) == 'keyedselection':
53 value = int(value)
54 if not getattr(self,'_v_propdict',False):
55 self._v_propdict = self.propdict()
56 if self._v_propdict.has_key('setter'):
57 settername = self._v_propdict['setter']
58 setter = getattr(aq_base(obj), settername, None)
59 if not setter:
60 raise ValueError("setter %s for property %s doesn't exist"
61 % (settername, id))
62 if not callable(setter):
63 raise TypeError("setter %s for property %s not callable"
64 % (settername, id))
65 setter(value)
66 else:
67 setattr(self,id,value)
68
69
72 """for selection and multiple selection properties
73 the value argument indicates the select variable
74 of the property
75 """
76 self._wrapperCheck(value)
77 if not self.valid_property_id(id):
78 raise BadRequest, 'Id %s is invalid or duplicate' % id
79
80 def setprops(**pschema):
81 self._properties=self._properties+(pschema,)
82 if setter: pschema['setter'] = setter
83 if label: pschema['label'] = label
84
85 if type in ('selection', 'multiple selection'):
86 if not hasattr(self, value):
87 raise BadRequest, 'No select variable %s' % value
88 setprops(id=id,type=type, visible=visible,
89 select_variable=value)
90 if type=='selection':
91 self._setPropValue(id, '')
92 else:
93 self._setPropValue(id, [])
94 else:
95 setprops(id=id, type=type, visible=visible)
96 self._setPropValue(id, value)
97
98
99 _onlystars = re.compile("^\*+$").search
100 security.declareProtected(ZEN_ZPROPERTIES_EDIT, 'manage_editProperties')
102 """
103 Edit object properties via the web.
104 The purpose of this method is to change all property values,
105 even those not listed in REQUEST; otherwise checkboxes that
106 get turned off will be ignored. Use manage_changeProperties()
107 instead for most situations.
108 """
109 for prop in self._propertyMap():
110 name=prop['id']
111 if 'w' in prop.get('mode', 'wd'):
112 value=REQUEST.get(name, '')
113 if self.zenPropIsPassword(name) and self._onlystars(value):
114 continue
115 self._updateProperty(name, value)
116 if getattr(self, "index_object", False):
117 self.index_object()
118 if REQUEST:
119 message="Saved changes."
120 return self.manage_propertiesForm(self,REQUEST,
121 manage_tabs_message=message)
122
123
127
128 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyIds')
130 """
131 Return list of device tree property names.
132 If all use list from property root node.
133 """
134 if all:
135 rootnode = self.getZenRootNode()
136 else:
137 if self.id == self.dmdRootName: return []
138 rootnode = aq_base(self)
139 props = []
140 for prop in rootnode.propertyIds():
141 if not pfilt(prop): continue
142 props.append(prop)
143 props.sort()
144 return props
145
146 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyItems')
148 """Return list of (id, value) tuples of zenProperties.
149 """
150 return map(lambda x: (x, getattr(self, x)), self.zenPropertyIds())
151
152 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyMap')
154 """Return property mapping of device tree properties."""
155 rootnode = self.getZenRootNode()
156 pmap = []
157 for pdict in rootnode.propertyMap():
158 if pfilt(pdict['id']): pmap.append(pdict)
159 pmap.sort(lambda x, y: cmp(x['id'], y['id']))
160 return pmap
161
162 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyString')
173
174 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropIsPassword')
176 """Is this field a password field.
177 """
178 return id.endswith("assword")
179
180 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyPath')
186
187 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyType')
189 """Return the type of this property."""
190 ptype = self.getZenRootNode().getPropertyType(id)
191 if not ptype: ptype = "string"
192 return ptype
193
194 security.declareProtected(ZEN_ZPROPERTIES_EDIT, 'setZenProperty')
196 """
197 Add or set the propvalue of the property propname on this node of
198 the device Class tree.
199 """
200 rootnode = self.getZenRootNode()
201 ptype = rootnode.getPropertyType(propname)
202 if ptype == 'lines':
203 dedupedList = []
204 for x in propvalue:
205 if x not in dedupedList:
206 dedupedList.append(x)
207 propvalue = dedupedList
208 if getattr(aq_base(self), propname, zenmarker) != zenmarker:
209 self._updateProperty(propname, propvalue)
210 else:
211 if ptype in ("selection", 'multiple selection'): ptype="string"
212 if type_converters.has_key(ptype):
213 propvalue=type_converters[ptype](propvalue)
214 self._setProperty(propname, propvalue, type=ptype)
215 if REQUEST: return self.callZenScreen(REQUEST)
216
217 security.declareProtected(ZEN_ZPROPERTIES_EDIT, 'saveZenProperties')
232
233 security.declareProtected(ZEN_ZPROPERTIES_EDIT, 'deleteZenProperty')
235 """
236 Delete device tree properties from the this DeviceClass object.
237 """
238 if propname:
239 self._delProperty(propname)
240 if REQUEST: return self.callZenScreen(REQUEST)
241
242 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'zenPropertyOptions')
244 "Provide a set of default options for a ZProperty"
245 return []
246
247 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'isLocal')
249 """Check to see if a name is local to our current context.
250 """
251 v = getattr(aq_base(self), propname, zenmarker)
252 return v != zenmarker
253
254 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'getOverriddenObjects')
273
274
275 InitializeClass(ZenPropertyManager)
276