Commit 31e30335 authored by Jérome Perrin's avatar Jérome Perrin

getProperty passes default parameter to getter, if getter supports it.

All ERP5Type tests can pass with this.



git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@10273 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent bb077547
......@@ -89,6 +89,8 @@ except ImportError:
from ZODB.POSException import ConflictError
from zLOG import LOG, INFO, ERROR, WARNING
_MARKER=[]
class WorkflowMethod(Method):
def __init__(self, method, id=None, reindex=1):
......@@ -789,16 +791,24 @@ class Base( CopyContainer, PortalContent, ActiveObject, Historical, ERP5Property
return default_value
security.declareProtected( Permissions.AccessContentsInformation, 'getProperty' )
def getProperty(self, key, d=None, **kw):
"""
Previous Name: getValue
Generic accessor. Calls the real accessor
def getProperty(self, key, d=_MARKER, **kw):
"""getProperty is the generic accessor to all properties and categories
defined on this object.
If an accessor exists for this property, the accessor will be called,
default value will be passed to the accessor as first positional argument.
"""
accessor_name = 'get' + UpperCase(key)
aq_self = aq_base(self)
if hasattr(aq_self, accessor_name):
method = getattr(self, accessor_name)
if d is not _MARKER:
try:
# here method is a method defined on the class, we don't know if the
# method supports default argument or not, so we'll try and if the
# method doesn't accepts it, we ignore default argument.
return method(d, **kw)
except TypeError:
pass
return method(**kw)
# Try to get a portal_type property (Implementation Dependent)
if not Base.aq_portal_type.has_key(self.portal_type):
......@@ -808,16 +818,20 @@ class Base( CopyContainer, PortalContent, ActiveObject, Historical, ERP5Property
pass
if hasattr(Base.aq_portal_type[self.portal_type], accessor_name):
method = getattr(self, accessor_name)
if d is not _MARKER:
try:
return method(d, **kw)
except TypeError:
pass
return method(**kw)
else:
return ERP5PropertyManager.getProperty(self, key, d=d, **kw)
if d is not _MARKER:
return ERP5PropertyManager.getProperty(self, key, d=d, **kw)
return ERP5PropertyManager.getProperty(self, key, **kw)
security.declareProtected( Permissions.AccessContentsInformation, 'getPropertyList' )
def getPropertyList(self, key, d=None):
"""
Previous Name: getValue
Generic accessor. Calls the real accessor
"""Same as getProperty, but for list properties.
"""
return self.getProperty('%s_list' % key)
......
......@@ -765,15 +765,14 @@ class TestPropertySheet:
self.assertEquals(person.getLastName(), None)
self.assertEquals(person.getLastName('foo'), 'foo')
self.assertEquals(person.getLastName(default='foo'), 'foo')
# FIXME: default not supported with getProperty and Getter methods
# test static method through getProperty
#self.assertEquals(person.getProperty('first_name'), None)
#self.assertEquals(person.getProperty('first_name', 'foo'), 'foo')
#self.assertEquals(person.getProperty('first_name', d='foo'), 'foo')
self.assertEquals(person.getProperty('first_name'), None)
self.assertEquals(person.getProperty('first_name', 'foo'), 'foo')
self.assertEquals(person.getProperty('first_name', d='foo'), 'foo')
# test dynamic method through getProperty
#self.assertEquals(person.getProperty('last_name'), None)
#self.assertEquals(person.getProperty('last_name', 'foo'), 'foo')
#self.assertEquals(person.getProperty('last_name', d='foo'), 'foo')
self.assertEquals(person.getProperty('last_name'), None)
self.assertEquals(person.getProperty('last_name', 'foo'), 'foo')
self.assertEquals(person.getProperty('last_name', d='foo'), 'foo')
# test simple property through getProperty
property_name = 'XXXthis_property_does_not_exist123123'
self.assertEquals(person.getProperty(property_name), None)
......@@ -796,12 +795,10 @@ class TestPropertySheet:
self.assertEquals('default', person.getDummyPsProp('default'))
# using getProperty
self.assertEquals('ps_default', person.getProperty('dummy_ps_prop'))
# FIXME: default not supported with getProperty and Getter methods
# XXX self.assertEquals('default', person.getProperty('dummy_ps_prop', 'default'))
self.assertEquals('default', person.getProperty('dummy_ps_prop', 'default'))
# None can be a default value too
# FIXME: default not supported with getProperty and Getter methods
# XXX self.assertEquals(None, person.getProperty('dummy_ps_prop', None))
self.assertEquals(None, person.getProperty('dummy_ps_prop', None))
self.assertEquals(None, person.getDummyPsProp(None))
# once the value has been set, there's no default
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment