Skip to content
Snippets Groups Projects
Commit bfd460a4 authored by Paul McCarthy's avatar Paul McCarthy
Browse files

getConstraint/setConstraint work without an instance - if instance is None,...

getConstraint/setConstraint work without an instance - if instance is None, they query/modify the default property constraints.
parent 5488df08
No related branches found
No related tags found
No related merge requests found
...@@ -371,6 +371,13 @@ class PropertyBase(object): ...@@ -371,6 +371,13 @@ class PropertyBase(object):
has this PropertyBase object as a property, one or more PropertyValue has this PropertyBase object as a property, one or more PropertyValue
instances are created and attached as an attribute of the parent. instances are created and attached as an attribute of the parent.
One important point to note is that a PropertyBase object may exist
without being bound to a HasProperties object (in which case it will
not create or manage any PropertyValue objects). This is useful if you
just want validation functionality via the validate(), getConstraint()
and setConstraint() methods, passing in None for the instance
parameter. Nothing else will work properly though.
Subclasses should: Subclasses should:
- Ensure that PropertyBase.__init__ is called. - Ensure that PropertyBase.__init__ is called.
...@@ -457,19 +464,33 @@ class PropertyBase(object): ...@@ -457,19 +464,33 @@ class PropertyBase(object):
def getConstraint(self, instance, constraint): def getConstraint(self, instance, constraint):
""" """
Returns the value of the named constraint for the specified instance. Returns the value of the named constraint for the specified instance,
or the default constraint value if instance is None.
""" """
return self._getInstanceData(instance).constraints[constraint]
if instance is None:
return self._defaultConstraints[constraint]
else:
return self._getInstanceData(instance).constraints.get(
constraint, None)
def setConstraint(self, instance, constraint, value): def setConstraint(self, instance, constraint, value):
""" """
Sets the value of the named constraint for the specified instance. Sets the value of the named constraint for the specified instance,
or the default value if instance is None.
""" """
log.debug('Changing constraint on {}: {} = {}'.format(self._label,
constraint, log.debug('Changing {} constraint on {}: {} = {}'.format(
value)) self._label,
self._getInstanceData(instance).constraints[constraint] = value 'default' if instance is None else 'instance',
constraint,
value))
if instance is None:
self._defaultConstraints[constraint] = value
else:
self._getInstanceData(instance).constraints[constraint] = value
def getPropVal(self, instance): def getPropVal(self, instance):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment