Commit 4a543453 authored by Sebastien Robin's avatar Sebastien Robin

* make getPropertyList and setPropertyList working for mono valued

  properties :
- added method setPropertyList
- make getPropertyList working on a mono valued property. It will
  return a list of one element (the value of the property)
- make setPropertyList working on a mono valued property, in such
  case a list of one element must be given
- do not generate List methods for mono valued properties any more. 
  List methods were generated for mono valued properties only when 
  some acquisition parameters were defined
- add unit test for getPropertyList and setPropertyList


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@25485 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 221bdc54
...@@ -1361,6 +1361,23 @@ class Base( CopyContainer, ...@@ -1361,6 +1361,23 @@ class Base( CopyContainer,
except TypeError: except TypeError:
pass pass
return method(**kw) return method(**kw)
# Try a mono valued accessor if it is available
# and return it as a list
if accessor_name.endswith('List'):
mono_valued_accessor_name = accessor_name[:-4]
if hasattr(aq_portal_type[aq_key], mono_valued_accessor_name):
method = getattr(self, mono_valued_accessor_name)
# We have a monovalued property
if d is _MARKER:
result = method(**kw)
else:
try:
result = method(d, **kw)
except TypeError:
result = method(**kw)
if not isinstance(result, (list, tuple)):
result = [result]
return result
else: else:
if d is not _MARKER: if d is not _MARKER:
return ERP5PropertyManager.getProperty(self, key, d=d, **kw) return ERP5PropertyManager.getProperty(self, key, d=d, **kw)
...@@ -1372,6 +1389,12 @@ class Base( CopyContainer, ...@@ -1372,6 +1389,12 @@ class Base( CopyContainer,
""" """
return self.getProperty('%s_list' % key, d=d) return self.getProperty('%s_list' % key, d=d)
security.declareProtected( Permissions.ModifyPortalContent, 'setPropertyList' )
def setPropertyList(self, key, value, **kw):
"""Same as setProperty, but for list properties.
"""
return self.setProperty('%s_list' % key, value, **kw)
security.declareProtected( Permissions.ModifyPortalContent, 'setProperty' ) security.declareProtected( Permissions.ModifyPortalContent, 'setProperty' )
def setProperty(self, key, value, type='string', **kw): def setProperty(self, key, value, type='string', **kw):
""" """
...@@ -1422,6 +1445,24 @@ class Base( CopyContainer, ...@@ -1422,6 +1445,24 @@ class Base( CopyContainer,
if getattr(aq_portal_type[aq_key], public_accessor_name, None) is not None: if getattr(aq_portal_type[aq_key], public_accessor_name, None) is not None:
method = getattr(self, public_accessor_name) method = getattr(self, public_accessor_name)
return method(value, **kw) return method(value, **kw)
# Try a mono valued setter if it is available
# and call it
if accessor_name.endswith('List'):
mono_valued_accessor_name = accessor_name[:-4]
mono_valued_public_accessor_name = public_accessor_name[:-4]
method = None
if hasattr(aq_portal_type[aq_key], mono_valued_accessor_name):
method = getattr(self, mono_valued_accessor_name)
elif hasattr(aq_portal_type[aq_key], mono_valued_public_accessor_name):
method = getattr(self, mono_valued_public_accessor_name)
if method is not None:
if isinstance(value, (list, tuple)):
value_len = len(value)
if value_len == 1:
mono_value = value[0]
return method(mono_value, **kw)
raise TypeError, \
"A mono valued property must be set with a list of len 1"
# Finaly use standard PropertyManager # Finaly use standard PropertyManager
#LOG("Changing attr: ",0, key) #LOG("Changing attr: ",0, key)
# If we are here, this means we do not use a property that # If we are here, this means we do not use a property that
......
...@@ -1503,21 +1503,22 @@ def createDefaultAccessors(property_holder, id, prop = None, ...@@ -1503,21 +1503,22 @@ def createDefaultAccessors(property_holder, id, prop = None,
if not hasattr(property_holder, accessor_name) or prop.get('override',0): if not hasattr(property_holder, accessor_name) or prop.get('override',0):
property_holder.registerAccessor(accessor_name, id, Acquired.DefaultGetter, accessor_args) property_holder.registerAccessor(accessor_name, id, Acquired.DefaultGetter, accessor_args)
# List Getter # List Getter
accessor_name = 'get' + UpperCase(id) + 'List' if prop['type'] in list_types or prop.get('multivalued', 0):
if not hasattr(property_holder, accessor_name) or prop.get('override',0): accessor_name = 'get' + UpperCase(id) + 'List'
property_holder.registerAccessor(accessor_name, id, Acquired.ListGetter, accessor_args) if not hasattr(property_holder, accessor_name) or prop.get('override',0):
property_holder.declareProtected( read_permission, accessor_name ) property_holder.registerAccessor(accessor_name, id, Acquired.ListGetter, accessor_args)
accessor_name = '_baseGet' + UpperCase(id) + 'List' property_holder.declareProtected( read_permission, accessor_name )
if not hasattr(property_holder, accessor_name) or prop.get('override',0): accessor_name = '_baseGet' + UpperCase(id) + 'List'
property_holder.registerAccessor(accessor_name, id, Acquired.ListGetter, accessor_args) if not hasattr(property_holder, accessor_name) or prop.get('override',0):
# Set Getter property_holder.registerAccessor(accessor_name, id, Acquired.ListGetter, accessor_args)
accessor_name = 'get' + UpperCase(id) + 'Set' # Set Getter
if not hasattr(property_holder, accessor_name) or prop.get('override',0): accessor_name = 'get' + UpperCase(id) + 'Set'
property_holder.registerAccessor(accessor_name, id, Acquired.SetGetter, accessor_args) if not hasattr(property_holder, accessor_name) or prop.get('override',0):
property_holder.declareProtected( read_permission, accessor_name ) property_holder.registerAccessor(accessor_name, id, Acquired.SetGetter, accessor_args)
accessor_name = '_baseGet' + UpperCase(id) + 'Set' property_holder.declareProtected( read_permission, accessor_name )
if not hasattr(property_holder, accessor_name) or prop.get('override',0): accessor_name = '_baseGet' + UpperCase(id) + 'Set'
property_holder.registerAccessor(accessor_name, id, Acquired.SetGetter, accessor_args) if not hasattr(property_holder, accessor_name) or prop.get('override',0):
property_holder.registerAccessor(accessor_name, id, Acquired.SetGetter, accessor_args)
if prop['type'] == 'content': if prop['type'] == 'content':
#LOG('Value Object Accessor', 0, prop['id']) #LOG('Value Object Accessor', 0, prop['id'])
# Base Getter # Base Getter
......
...@@ -2019,6 +2019,7 @@ class TestPropertySheet: ...@@ -2019,6 +2019,7 @@ class TestPropertySheet:
{ 'id' : 'wrapped_region_title', { 'id' : 'wrapped_region_title',
'description' : 'The title of the region', 'description' : 'The title of the region',
'type' : 'string', 'type' : 'string',
'multivalued' : 1,
'acquisition_base_category' : ('region',), 'acquisition_base_category' : ('region',),
'acquisition_portal_type' : ('Category', ), 'acquisition_portal_type' : ('Category', ),
'acquisition_copy_value' : 0, 'acquisition_copy_value' : 0,
...@@ -2504,6 +2505,83 @@ class TestPropertySheet: ...@@ -2504,6 +2505,83 @@ class TestPropertySheet:
self.assertTrue(type_info in container.allowedContentTypes()) self.assertTrue(type_info in container.allowedContentTypes())
container.newContent(portal_type='Test Add Permission Document') container.newContent(portal_type='Test Add Permission Document')
def testPropertyListWithMonoValuedProperty(self):
"""
Check that we can use setPropertyList and getPropertyList
on a mono valued property
"""
self._addProperty('Person',
''' { 'id': 'foo_bar',
'type': 'string',
'mode': 'rw', }''')
person = self.getPersonModule().newContent(portal_type='Person')
email = person.newContent(portal_type='Email')
self.assertEquals(None, getattr(person, 'getFooBarList', None))
self.assertEquals(person.getFooBar(), None)
self.assertFalse(person.hasProperty('foo_bar'))
self.assertEquals(person.getProperty('foo_bar'), None)
self.assertEquals(person.getPropertyList('foo_bar'), [None])
person.setFooBar('foo')
self.assertEquals(person.getProperty('foo_bar'), 'foo')
self.assertEquals(person.getPropertyList('foo_bar'), ['foo'])
person.setFooBar(None)
self.assertEquals(person.getProperty('foo_bar'), None)
person.setPropertyList('foo_bar', ['bar'])
self.assertEquals(person.getProperty('foo_bar'), 'bar')
self.assertEquals(person.getPropertyList('foo_bar'), ['bar'])
self.assertRaises(TypeError, person.setPropertyList, 'foo_bar',
['a', 'b'])
def testPropertyListOnMonoValuedAcquiredProperty(self,quiet=quiet, run=run_all_test):
"""
Check that we can use setPropertyList and getPropertyList
on a mono valued acquired property
"""
self._addProperty('Person',
''' { 'id': 'foo_bar',
'type': 'string',
'mode': 'rw', }''')
self._addProperty('Email',
''' { 'id': 'foo_bar',
'type': 'string',
'acquired_property_id': ('description', ),
'acquisition_base_category': ( 'parent', ),
'acquisition_portal_type' : ( 'Person', ),
'acquisition_copy_value' : 0,
'acquisition_mask_value' : 1,
'acquisition_accessor_id' : 'getFooBar',
'acquisition_depends' : None,
'mode': 'rw', }''')
person = self.getPersonModule().newContent(portal_type='Person')
email = person.newContent(portal_type='Email')
self.assertEquals(email.getPropertyList('foo_bar'), [None])
person.setPropertyList('foo_bar', ['foo'])
self.assertEquals(email.getPropertyList('foo_bar'), ['foo'])
email.setPropertyList('foo_bar', ['bar'])
self.assertEquals(email.getPropertyList('foo_bar'), ['bar'])
email.setPropertyList('foo_bar', [None])
self.assertEquals(email.getPropertyList('foo_bar'), ['foo'])
person.setPropertyList('foo_bar', [None])
self.assertEquals(email.getPropertyList('foo_bar'), [None])
def testPropertyListWithMultiValuedProperty(self):
"""
Check that we can use setPropertyList and getPropertyList
on a multi valued property
"""
self._addProperty('Person',
''' { 'id': 'foo_bar',
'type': 'string',
'multivalued': 1,
'mode': 'rw', }''')
person = self.getPersonModule().newContent(portal_type='Person')
# We have None, like test_list_accessors
self.assertEquals(person.getFooBarList(), None)
self.assertEquals(person.getPropertyList('foo_bar'), None)
person.setPropertyList('foo_bar', ['foo', 'bar'])
self.assertEquals(person.getPropertyList('foo_bar'), ['foo', 'bar'])
person.setPropertyList('foo_bar', [])
self.assertEquals(person.getFooBarList(), [])
class TestAccessControl(ERP5TypeTestCase): class TestAccessControl(ERP5TypeTestCase):
# Isolate test in a dedicaced class in order not to break other tests # Isolate test in a dedicaced class in order not to break other tests
......
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