Commit 387360da authored by Julien Muchembled's avatar Julien Muchembled

Add support for default value in category & value accessors

In Base._getAcquiredCategoryMembershipItemList, replace broken code by an exception.
parent d3eafb39
...@@ -1169,8 +1169,8 @@ class TestResource(ERP5TypeTestCase): ...@@ -1169,8 +1169,8 @@ class TestResource(ERP5TypeTestCase):
# Create resource # Create resource
resource_value = self.portal.getDefaultModule( resource_value = self.portal.getDefaultModule(
self.product_portal_type).newContent(portal_type=self.product_portal_type) self.product_portal_type).newContent(portal_type=self.product_portal_type)
resource_value.edit(quantity_unit_value_list=( resource_value.edit(quantity_unit_value_list=[
quantity_unit_gram, quantity_unit_liter)) quantity_unit_gram, quantity_unit_liter])
self.commit() self.commit()
self.assertEqual(resource_value.getDefaultQuantityUnitValue(), self.assertEqual(resource_value.getDefaultQuantityUnitValue(),
quantity_unit_gram) quantity_unit_gram)
...@@ -1206,8 +1206,8 @@ class TestResource(ERP5TypeTestCase): ...@@ -1206,8 +1206,8 @@ class TestResource(ERP5TypeTestCase):
# Change default quantity unit on resource # Change default quantity unit on resource
# Now liter is default quantity unit. # Now liter is default quantity unit.
resource_value.edit(quantity_unit_value_list=( resource_value.edit(quantity_unit_value_list=[
quantity_unit_liter, quantity_unit_gram)) quantity_unit_liter, quantity_unit_gram])
self.commit() self.commit()
# Check existing movement again and make sure that quantity # Check existing movement again and make sure that quantity
......
...@@ -129,11 +129,8 @@ class DefaultGetter(BaseGetter): ...@@ -129,11 +129,8 @@ class DefaultGetter(BaseGetter):
self._key = key self._key = key
def __call__(self, instance, *args, **kw): def __call__(self, instance, *args, **kw):
if len(args) > 0: if args:
default = args[0] kw['default'] = args[0]
kw['default'] = default
else:
default = None
return instance._getDefaultAcquiredCategoryMembership(self._key, **kw) return instance._getDefaultAcquiredCategoryMembership(self._key, **kw)
psyco.bind(__call__) psyco.bind(__call__)
...@@ -156,8 +153,9 @@ class ListGetter(BaseGetter): ...@@ -156,8 +153,9 @@ class ListGetter(BaseGetter):
self._key = key self._key = key
def __call__(self, instance, *args, **kw): def __call__(self, instance, *args, **kw):
if not kw.has_key('base'): if args:
kw['base'] = 0 kw['default'] = args[0]
kw.setdefault('base', 0)
return instance._getAcquiredCategoryMembershipList(self._key, **kw) return instance._getAcquiredCategoryMembershipList(self._key, **kw)
psyco.bind(__call__) psyco.bind(__call__)
...@@ -192,6 +190,8 @@ class ItemListGetter(BaseGetter): ...@@ -192,6 +190,8 @@ class ItemListGetter(BaseGetter):
self._key = key self._key = key
def __call__(self, instance, *args, **kw): def __call__(self, instance, *args, **kw):
if args:
kw['default'] = args[0]
return instance._getAcquiredCategoryMembershipItemList(self._key, base=0, **kw) return instance._getAcquiredCategoryMembershipItemList(self._key, base=0, **kw)
psyco.bind(__call__) psyco.bind(__call__)
......
...@@ -74,17 +74,12 @@ class ValueGetter(Base.Getter): ...@@ -74,17 +74,12 @@ class ValueGetter(Base.Getter):
def __call__(self, instance, *args, **kw): def __call__(self, instance, *args, **kw):
# We return the first available object in the list # We return the first available object in the list
if len(args) > 0:
default_result = args[0]
else:
default_result = self._default
o = None
for k in self._storage_id_list: for k in self._storage_id_list:
o = getattr(instance, k, None) o = getattr(instance, k, None)
if o is not None and (o.portal_type is None or if o is not None and (o.portal_type is None or
o.portal_type in self._portal_type): o.portal_type in self._portal_type):
return o return o
return default_result return args[0] if args else self._default
psyco.bind(__call__) psyco.bind(__call__)
...@@ -161,16 +156,12 @@ class Getter(Base.Getter): ...@@ -161,16 +156,12 @@ class Getter(Base.Getter):
def __call__(self, instance, *args, **kw): def __call__(self, instance, *args, **kw):
# We return the first available object in the list # We return the first available object in the list
if len(args) > 0:
default = args[0]
else:
default = None
o = None o = None
for k in self._storage_id_list: for k in self._storage_id_list:
o = getattr(instance, k, None) o = getattr(instance, k, None)
if o is not None and o.portal_type in self._portal_type: if o is not None and o.portal_type in self._portal_type:
return o.getProperty(self._acquired_property, default, **kw) return o.getProperty(self._acquired_property, *args, **kw)
return default return args[0] if args else self._default
psyco.bind(__call__) psyco.bind(__call__)
......
...@@ -26,10 +26,11 @@ ...@@ -26,10 +26,11 @@
# #
############################################################################## ##############################################################################
from operator import methodcaller
from Base import func_code, type_definition, list_types, ATTRIBUTE_PREFIX, Setter as BaseSetter, Getter as BaseGetter from Base import func_code, type_definition, list_types, ATTRIBUTE_PREFIX, Setter as BaseSetter, Getter as BaseGetter
from zLOG import LOG from zLOG import LOG
from Products.ERP5Type.PsycoWrapper import psyco from Products.ERP5Type.PsycoWrapper import psyco
from Products.ERP5Type.Utils import convertToUpperCase
class SetSetter(BaseSetter): class SetSetter(BaseSetter):
""" """
...@@ -119,6 +120,8 @@ class DefaultGetter(BaseGetter): ...@@ -119,6 +120,8 @@ class DefaultGetter(BaseGetter):
def __call__(self, instance, *args, **kw): def __call__(self, instance, *args, **kw):
if self._warning: if self._warning:
LOG("ERP5Type Deprecated Getter Id:",0, self._id) LOG("ERP5Type Deprecated Getter Id:",0, self._id)
if args:
kw['default'] = args[0]
return instance._getDefaultAcquiredValue(self._key, **kw) return instance._getDefaultAcquiredValue(self._key, **kw)
psyco.bind(__call__) psyco.bind(__call__)
...@@ -148,6 +151,8 @@ class ListGetter(BaseGetter): ...@@ -148,6 +151,8 @@ class ListGetter(BaseGetter):
def __call__(self, instance, *args, **kw): def __call__(self, instance, *args, **kw):
if self._warning: if self._warning:
LOG("ERP5Type Deprecated Getter Id:",0, self._id) LOG("ERP5Type Deprecated Getter Id:",0, self._id)
if args:
kw['default'] = args[0]
return instance._getAcquiredValueList(self._key, **kw) return instance._getAcquiredValueList(self._key, **kw)
psyco.bind(__call__) psyco.bind(__call__)
...@@ -158,105 +163,52 @@ class SetGetter(ListGetter): ...@@ -158,105 +163,52 @@ class SetGetter(ListGetter):
Gets a category value set Gets a category value set
""" """
def __call__(self, instance, *args, **kw): def __call__(self, instance, *args, **kw):
result_list = ListGetter.__call__(self, instance, *args, **kw) r = ListGetter.__call__(self, instance, **kw)
result_set = dict([(x, 0) for x in result_list]).keys() return list(set(r)) if r or not args else args[0]
return result_set
class DefaultTitleGetter(DefaultGetter):
def __call__(self, instance, *args, **kw):
o = DefaultGetter.__call__(self, instance, *args, **kw)
if o is None:
return None
return o.getTitle()
psyco.bind(__call__)
class TitleListGetter(ListGetter):
def __call__(self, instance, *args, **kw):
return [x.getTitle() for x in ListGetter.__call__(self, instance, *args, **kw)]
psyco.bind(__call__)
class TitleSetGetter(TitleListGetter):
"""
Gets a category value set
"""
def __call__(self, instance, *args, **kw):
result_list = TitleListGetter.__call__(self, instance, *args, **kw)
result_set = dict([(x, 0) for x in result_list]).keys()
return result_set
class DefaultTranslatedTitleGetter(DefaultGetter): def defMethodGetter(key, method=None):
key = convertToUpperCase(key)
name = 'Default%sGetter' % key
if method is None:
method = methodcaller('get' + key)
def __call__(self, instance, *args, **kw): def __call__(self, instance, *args, **kw):
o = DefaultGetter.__call__(self, instance, *args, **kw) o = DefaultGetter.__call__(self, instance, **kw)
if o is None: if o is None:
return None return args[0] if args else None
return o.getTranslatedTitle() return method(o)
psyco.bind(__call__)
class TranslatedTitleListGetter(ListGetter):
def __call__(self, instance, *args, **kw):
return [x.getTranslatedTitle() for x in ListGetter.__call__(self, instance, *args, **kw)]
psyco.bind(__call__) psyco.bind(__call__)
globals()[name] = type(name, (DefaultGetter,), {'__call__': __call__})
class TranslatedTitleSetGetter(TranslatedTitleListGetter): name = '%sListGetter' % key
"""
Gets a category value set
"""
def __call__(self, instance, *args, **kw):
result_list = TranslatedTitleListGetter.__call__(
self, instance, *args, **kw)
result_set = dict([(x, 0) for x in result_list]).keys()
return result_set
class DefaultReferenceGetter(DefaultGetter):
def __call__(self, instance, *args, **kw): def __call__(self, instance, *args, **kw):
o = DefaultGetter.__call__(self, instance, *args, **kw) r = ListGetter.__call__(self, instance, **kw)
if o is None: return map(method, r) if r or not args else args[0]
return None
return o.getReference()
psyco.bind(__call__) psyco.bind(__call__)
globals()[name] = type(name, (ListGetter,), {'__call__': __call__})
class ReferenceListGetter(ListGetter): name = '%sSetGetter' % key
def __call__(self, instance, *args, **kw): def __call__(self, instance, *args, **kw):
return [x.getReference() for x in ListGetter.__call__(self, instance, *args, **kw)] r = ListGetter.__call__(self, instance, **kw)
return list(set(method(x) for x in r)) if r or not args else args[0]
psyco.bind(__call__) psyco.bind(__call__)
globals()[name] = type(name, (ListGetter,), {'__call__': __call__})
class ReferenceSetGetter(ReferenceListGetter):
"""
Gets a category value set
"""
def __call__(self, instance, *args, **kw):
result_list = ReferenceListGetter.__call__(
self, instance, *args, **kw)
result_set = dict([(x, 0) for x in result_list]).keys()
return result_set
class DefaultUidGetter(DefaultGetter):
def __call__(self, instance, *args, **kw):
o = DefaultGetter.__call__(self, instance, *args, **kw)
if o is None:
return None
return o.getUid()
psyco.bind(__call__)
UidGetter = DefaultUidGetter
class UidListGetter(ListGetter): defMethodGetter('id')
def __call__(self, instance, *args, **kw): defMethodGetter('logical_path')
return [x.getUid() for x in ListGetter.__call__(self, instance, *args, **kw)] defMethodGetter('reference')
psyco.bind(__call__) defMethodGetter('title')
defMethodGetter('title_or_id')
defMethodGetter('translated_title')
defMethodGetter('uid')
defMethodGetter('translated_logical_path', methodcaller(
'getLogicalPath', item_method='getTranslatedTitle'))
class UidSetGetter(UidListGetter): IdGetter = DefaultIdGetter
""" TitleOrIdGetter = DefaultTitleOrIdGetter
Gets a category value set LogicalPathGetter = DefaultLogicalPathGetter
""" TranslatedLogicalPathGetter = DefaultTranslatedLogicalPathGetter
def __call__(self, instance, *args, **kw):
result_list = UidListGetter.__call__(
self, instance, *args, **kw)
result_set = dict([(x, 0) for x in result_list]).keys()
return result_set
class UidSetSetter(BaseSetter): class UidSetSetter(BaseSetter):
...@@ -318,80 +270,12 @@ class UidDefaultSetter(UidSetSetter): ...@@ -318,80 +270,12 @@ class UidDefaultSetter(UidSetSetter):
checked_permission=kw.get('checked_permission', None)) checked_permission=kw.get('checked_permission', None))
return (instance, ) return (instance, )
class DefaultIdGetter(DefaultGetter):
def __call__(self, instance, *args, **kw):
o = DefaultGetter.__call__(self, instance, *args, **kw)
if o is None:
return None
return o.getId()
psyco.bind(__call__)
IdGetter = DefaultIdGetter
class DefaultTitleOrIdGetter(DefaultGetter):
def __call__(self, instance, *args, **kw):
o = DefaultGetter.__call__(self, instance, *args, **kw)
if o is None:
return None
return o.getTitleOrId()
psyco.bind(__call__)
TitleOrIdGetter = DefaultTitleOrIdGetter
class DefaultLogicalPathGetter(DefaultGetter):
_item_method = 'getTitle'
def __call__(self, instance, *args, **kw):
o = DefaultGetter.__call__(self, instance, *args, **kw)
if o is None:
return None
return o.getLogicalPath(item_method=self._item_method)
psyco.bind(__call__)
LogicalPathGetter = DefaultLogicalPathGetter
class DefaultTranslatedLogicalPathGetter(DefaultLogicalPathGetter):
_item_method = "getTranslatedTitle"
TranslatedLogicalPathGetter = DefaultTranslatedLogicalPathGetter
class IdListGetter(ListGetter):
def __call__(self, instance, *args, **kw):
return [x.getId() for x in ListGetter.__call__(self, instance, *args, **kw)]
psyco.bind(__call__)
class IdSetGetter(IdListGetter):
"""
Gets a category value set
"""
def __call__(self, instance, *args, **kw):
result_list = IdListGetter.__call__(
self, instance, *args, **kw)
result_set = dict([(x, 0) for x in result_list]).keys()
return result_set
class LogicalPathListGetter(ListGetter):
_item_method = 'getTitle'
def __call__(self, instance, *args, **kw):
return [x.getLogicalPath(item_method=self._item_method) for x in
ListGetter.__call__(self, instance, *args, **kw)]
class LogicalPathSetGetter(LogicalPathListGetter):
"""
Gets a category value set
"""
def __call__(self, instance, *args, **kw):
result_list = LogicalPathListGetter.__call__(
self, instance, *args, **kw)
result_set = dict([(x, 0) for x in result_list]).keys()
return result_set
class DefaultPropertyGetter(DefaultGetter): class DefaultPropertyGetter(DefaultGetter):
def __call__(self, instance, key, *args, **kw): def __call__(self, instance, key, *args, **kw):
o = DefaultGetter.__call__(self, instance, *args, **kw) o = DefaultGetter.__call__(self, instance, **kw)
if o is None: if o is None:
return None return args[0] if args else None
return o.getProperty(key) return o.getProperty(key)
psyco.bind(__call__) psyco.bind(__call__)
...@@ -400,7 +284,8 @@ PropertyGetter = DefaultPropertyGetter ...@@ -400,7 +284,8 @@ PropertyGetter = DefaultPropertyGetter
class PropertyListGetter(ListGetter): class PropertyListGetter(ListGetter):
def __call__(self, instance, key, *args, **kw): def __call__(self, instance, key, *args, **kw):
return [x.getProperty(key) for x in ListGetter.__call__(self, instance, *args, **kw)] r = ListGetter.__call__(self, instance, **kw)
return [x.getProperty(key) for x in r] if r or not args else args[0]
psyco.bind(__call__) psyco.bind(__call__)
class PropertySetGetter(PropertyListGetter): class PropertySetGetter(PropertyListGetter):
...@@ -408,7 +293,5 @@ class PropertySetGetter(PropertyListGetter): ...@@ -408,7 +293,5 @@ class PropertySetGetter(PropertyListGetter):
Gets a category value set Gets a category value set
""" """
def __call__(self, instance, *args, **kw): def __call__(self, instance, *args, **kw):
result_list = PropertyListGetter.__call__( r = PropertyListGetter.__call__(self, instance, **kw)
self, instance, *args, **kw) return list(set(r)) if r or not args else args[0]
result_set = dict([(x, 0) for x in result_list]).keys()
return result_set
...@@ -1884,61 +1884,56 @@ class Base( CopyContainer, ...@@ -1884,61 +1884,56 @@ class Base( CopyContainer,
checked_permission=None) checked_permission=None)
self.reindexObject() self.reindexObject()
def _getDefaultValue(self, id, spec=(), filter=None, portal_type=(), checked_permission=None): def _getDefaultValue(self, id, spec=(), filter=None, default=_MARKER, **kw):
path = self._getDefaultCategoryMembership(id, spec=spec, filter=filter, path = self._getDefaultCategoryMembership(id, base=1, spec=spec,
portal_type=portal_type,base=1, filter=filter, **kw)
checked_permission=checked_permission) if path:
if path is None:
return None
else:
return self._getCategoryTool().resolveCategory(path) return self._getCategoryTool().resolveCategory(path)
if default is not _MARKER:
return default
security.declareProtected(Permissions.AccessContentsInformation, security.declareProtected(Permissions.AccessContentsInformation,
'getDefaultValue') 'getDefaultValue')
getDefaultValue = _getDefaultValue getDefaultValue = _getDefaultValue
def _getValueList(self, id, spec=(), filter=None, portal_type=(), checked_permission=None): def _getValueList(self, id, spec=(), filter=None, default=_MARKER, **kw):
ref_list = [] ref_list = []
for path in self._getCategoryMembershipList(id, spec=spec, filter=filter, for path in self._getCategoryMembershipList(id, base=1, spec=spec,
portal_type=portal_type, base=1, filter=filter, **kw):
checked_permission=checked_permission): category = self._getCategoryTool().resolveCategory(path)
# LOG('_getValueList',0,str(path)) if category is not None:
try: ref_list.append(category)
value = self._getCategoryTool().resolveCategory(path) return ref_list if ref_list or default is _MARKER else default
if value is not None: ref_list.append(value)
except ConflictError:
raise
except:
LOG("ERP5Type WARNING",0,"category %s has no object value" % path, error=sys.exc_info())
return ref_list
security.declareProtected(Permissions.AccessContentsInformation, security.declareProtected(Permissions.AccessContentsInformation,
'getValueList') 'getValueList')
getValueList = _getValueList getValueList = _getValueList
def _getDefaultAcquiredValue(self, id, spec=(), filter=None, portal_type=(), def _getDefaultAcquiredValue(self, id, spec=(), filter=None, portal_type=(),
evaluate=1, checked_permission=None, **kw): evaluate=1, checked_permission=None,
default=None, **kw):
path = self._getDefaultAcquiredCategoryMembership(id, spec=spec, filter=filter, path = self._getDefaultAcquiredCategoryMembership(id, spec=spec, filter=filter,
portal_type=portal_type, base=1, portal_type=portal_type, base=1,
checked_permission=checked_permission, checked_permission=checked_permission,
**kw) **kw)
if path is None: if path:
return None
else:
return self._getCategoryTool().resolveCategory(path) return self._getCategoryTool().resolveCategory(path)
if default is not _MARKER:
return default
security.declareProtected(Permissions.AccessContentsInformation, security.declareProtected(Permissions.AccessContentsInformation,
'getDefaultAcquiredValue') 'getDefaultAcquiredValue')
getDefaultAcquiredValue = _getDefaultAcquiredValue getDefaultAcquiredValue = _getDefaultAcquiredValue
def _getAcquiredValueList(self, id, spec=(), filter=None, **kw): def _getAcquiredValueList(self, id, spec=(), filter=None, default=_MARKER,
**kw):
ref_list = [] ref_list = []
for path in self._getAcquiredCategoryMembershipList(id, base=1, for path in self._getAcquiredCategoryMembershipList(id, base=1,
spec=spec, filter=filter, **kw): spec=spec, filter=filter, **kw):
category = self._getCategoryTool().resolveCategory(path) category = self._getCategoryTool().resolveCategory(path)
if category is not None: if category is not None:
ref_list.append(category) ref_list.append(category)
return ref_list return ref_list if ref_list or default is _MARKER else default
security.declareProtected(Permissions.AccessContentsInformation, security.declareProtected(Permissions.AccessContentsInformation,
'getAcquiredValueList') 'getAcquiredValueList')
...@@ -2082,27 +2077,31 @@ class Base( CopyContainer, ...@@ -2082,27 +2077,31 @@ class Base( CopyContainer,
self.reindexObject() self.reindexObject()
def _getCategoryMembershipList(self, category, spec=(), filter=None, def _getCategoryMembershipList(self, category, spec=(), filter=None,
portal_type=(), base=0, keep_default=1, checked_permission=None, **kw): portal_type=(), base=0, keep_default=1, checked_permission=None,
default=_MARKER, **kw):
""" """
This returns the list of categories for an object This returns the list of categories for an object
""" """
return self._getCategoryTool().getCategoryMembershipList(self, category, r = self._getCategoryTool().getCategoryMembershipList(self, category,
spec=spec, filter=filter, portal_type=portal_type, base=base, spec=spec, filter=filter, portal_type=portal_type, base=base,
keep_default=keep_default, checked_permission=checked_permission, **kw) keep_default=keep_default, checked_permission=checked_permission, **kw)
return r if r or default is _MARKER else default
security.declareProtected( Permissions.AccessContentsInformation, 'getCategoryMembershipList' ) security.declareProtected( Permissions.AccessContentsInformation, 'getCategoryMembershipList' )
getCategoryMembershipList = _getCategoryMembershipList getCategoryMembershipList = _getCategoryMembershipList
def _getAcquiredCategoryMembershipList(self, category, spec=(), filter=None, def _getAcquiredCategoryMembershipList(self, category, spec=(), filter=None,
portal_type=(), base=0, keep_default=1, checked_permission=None, **kw): portal_type=(), base=0, keep_default=1, checked_permission=None,
default=_MARKER, **kw):
""" """
Returns the list of acquired categories Returns the list of acquired categories
""" """
return self._getCategoryTool().getAcquiredCategoryMembershipList(self, r = self._getCategoryTool().getAcquiredCategoryMembershipList(self,
category, spec=spec, filter=filter, category, spec=spec, filter=filter,
portal_type=portal_type, base=base, portal_type=portal_type, base=base,
keep_default=keep_default, keep_default=keep_default,
checked_permission=checked_permission, **kw ) checked_permission=checked_permission, **kw )
return r if r or default is _MARKER else default
security.declareProtected( Permissions.AccessContentsInformation, security.declareProtected( Permissions.AccessContentsInformation,
'getAcquiredCategoryMembershipList' ) 'getAcquiredCategoryMembershipList' )
...@@ -2117,29 +2116,17 @@ class Base( CopyContainer, ...@@ -2117,29 +2116,17 @@ class Base( CopyContainer,
def _getAcquiredCategoryMembershipItemList(self, category, spec=(), def _getAcquiredCategoryMembershipItemList(self, category, spec=(),
filter=None, portal_type=(), base=0, method_id=None, sort_id='default', filter=None, portal_type=(), base=0, method_id=None, sort_id='default',
checked_permission=None): checked_permission=None, default=_MARKER):
# Standard behaviour - should be OK if method_id or sort_id not in (None, 'default'):
# sort_id should be None for not sort - default behaviour in other methods raise NotImplementedError
if method_id is None and sort_id in (None, 'default'): membership_list = self._getAcquiredCategoryMembershipList(category,
membership_list = self._getAcquiredCategoryMembershipList(category,
spec = spec, filter=filter, portal_type=portal_type, base=base, spec = spec, filter=filter, portal_type=portal_type, base=base,
checked_permission=checked_permission) checked_permission=checked_permission)
if membership_list or default is _MARKER:
if sort_id == 'default': if sort_id == 'default':
membership_list.sort() membership_list.sort()
return [(x, x) for x in membership_list] return [(x, x) for x in membership_list]
# Advanced behaviour XXX This is new and needs to be checked return [] if default is _MARKER else default
membership_list = self._getAcquiredCategoryMembershipList(category,
spec = spec, filter=filter, portal_type=portal_type, base=1,
checked_permission=checked_permission)
result = []
for path in membership_list:
value = self._getCategoryTool().resolveCategory(path)
if value is not None:
result += [value]
result.sort(key=lambda x: getattr(x,sort_id)())
if method_id is None:
return [(x, x) for x in membership_list]
return [(x,getattr(x, method_id)()) for x in membership_list]
def _getDefaultCategoryMembership(self, category, spec=(), filter=None, def _getDefaultCategoryMembership(self, category, spec=(), filter=None,
portal_type=(), base=0, default=None, checked_permission=None, **kw): portal_type=(), base=0, default=None, checked_permission=None, **kw):
......
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