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: ...@@ -89,6 +89,8 @@ except ImportError:
from ZODB.POSException import ConflictError from ZODB.POSException import ConflictError
from zLOG import LOG, INFO, ERROR, WARNING from zLOG import LOG, INFO, ERROR, WARNING
_MARKER=[]
class WorkflowMethod(Method): class WorkflowMethod(Method):
def __init__(self, method, id=None, reindex=1): def __init__(self, method, id=None, reindex=1):
...@@ -789,16 +791,24 @@ class Base( CopyContainer, PortalContent, ActiveObject, Historical, ERP5Property ...@@ -789,16 +791,24 @@ class Base( CopyContainer, PortalContent, ActiveObject, Historical, ERP5Property
return default_value return default_value
security.declareProtected( Permissions.AccessContentsInformation, 'getProperty' ) security.declareProtected( Permissions.AccessContentsInformation, 'getProperty' )
def getProperty(self, key, d=None, **kw): def getProperty(self, key, d=_MARKER, **kw):
""" """getProperty is the generic accessor to all properties and categories
Previous Name: getValue defined on this object.
If an accessor exists for this property, the accessor will be called,
Generic accessor. Calls the real accessor default value will be passed to the accessor as first positional argument.
""" """
accessor_name = 'get' + UpperCase(key) accessor_name = 'get' + UpperCase(key)
aq_self = aq_base(self) aq_self = aq_base(self)
if hasattr(aq_self, accessor_name): if hasattr(aq_self, accessor_name):
method = getattr(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) return method(**kw)
# Try to get a portal_type property (Implementation Dependent) # Try to get a portal_type property (Implementation Dependent)
if not Base.aq_portal_type.has_key(self.portal_type): if not Base.aq_portal_type.has_key(self.portal_type):
...@@ -808,16 +818,20 @@ class Base( CopyContainer, PortalContent, ActiveObject, Historical, ERP5Property ...@@ -808,16 +818,20 @@ class Base( CopyContainer, PortalContent, ActiveObject, Historical, ERP5Property
pass pass
if hasattr(Base.aq_portal_type[self.portal_type], accessor_name): if hasattr(Base.aq_portal_type[self.portal_type], accessor_name):
method = getattr(self, accessor_name) method = getattr(self, accessor_name)
if d is not _MARKER:
try:
return method(d, **kw)
except TypeError:
pass
return method(**kw) return method(**kw)
else: else:
if d is not _MARKER:
return ERP5PropertyManager.getProperty(self, key, d=d, **kw) return ERP5PropertyManager.getProperty(self, key, d=d, **kw)
return ERP5PropertyManager.getProperty(self, key, **kw)
security.declareProtected( Permissions.AccessContentsInformation, 'getPropertyList' ) security.declareProtected( Permissions.AccessContentsInformation, 'getPropertyList' )
def getPropertyList(self, key, d=None): def getPropertyList(self, key, d=None):
""" """Same as getProperty, but for list properties.
Previous Name: getValue
Generic accessor. Calls the real accessor
""" """
return self.getProperty('%s_list' % key) return self.getProperty('%s_list' % key)
......
...@@ -765,15 +765,14 @@ class TestPropertySheet: ...@@ -765,15 +765,14 @@ class TestPropertySheet:
self.assertEquals(person.getLastName(), None) self.assertEquals(person.getLastName(), None)
self.assertEquals(person.getLastName('foo'), 'foo') self.assertEquals(person.getLastName('foo'), 'foo')
self.assertEquals(person.getLastName(default='foo'), 'foo') self.assertEquals(person.getLastName(default='foo'), 'foo')
# FIXME: default not supported with getProperty and Getter methods
# test static method through getProperty # test static method through getProperty
#self.assertEquals(person.getProperty('first_name'), None) self.assertEquals(person.getProperty('first_name'), None)
#self.assertEquals(person.getProperty('first_name', 'foo'), 'foo') self.assertEquals(person.getProperty('first_name', 'foo'), 'foo')
#self.assertEquals(person.getProperty('first_name', d='foo'), 'foo') self.assertEquals(person.getProperty('first_name', d='foo'), 'foo')
# test dynamic method through getProperty # test dynamic method through getProperty
#self.assertEquals(person.getProperty('last_name'), None) self.assertEquals(person.getProperty('last_name'), None)
#self.assertEquals(person.getProperty('last_name', 'foo'), 'foo') self.assertEquals(person.getProperty('last_name', 'foo'), 'foo')
#self.assertEquals(person.getProperty('last_name', d='foo'), 'foo') self.assertEquals(person.getProperty('last_name', d='foo'), 'foo')
# test simple property through getProperty # test simple property through getProperty
property_name = 'XXXthis_property_does_not_exist123123' property_name = 'XXXthis_property_does_not_exist123123'
self.assertEquals(person.getProperty(property_name), None) self.assertEquals(person.getProperty(property_name), None)
...@@ -796,12 +795,10 @@ class TestPropertySheet: ...@@ -796,12 +795,10 @@ class TestPropertySheet:
self.assertEquals('default', person.getDummyPsProp('default')) self.assertEquals('default', person.getDummyPsProp('default'))
# using getProperty # using getProperty
self.assertEquals('ps_default', person.getProperty('dummy_ps_prop')) self.assertEquals('ps_default', person.getProperty('dummy_ps_prop'))
# FIXME: default not supported with getProperty and Getter methods self.assertEquals('default', person.getProperty('dummy_ps_prop', 'default'))
# XXX self.assertEquals('default', person.getProperty('dummy_ps_prop', 'default'))
# None can be a default value too # None can be a default value too
# FIXME: default not supported with getProperty and Getter methods self.assertEquals(None, person.getProperty('dummy_ps_prop', None))
# XXX self.assertEquals(None, person.getProperty('dummy_ps_prop', None))
self.assertEquals(None, person.getDummyPsProp(None)) self.assertEquals(None, person.getDummyPsProp(None))
# once the value has been set, there's no default # 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