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