Commit 518deb22 authored by Romain Courteaud's avatar Romain Courteaud

Add Dynamic Related Key based on related accessors


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@24792 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 2623ed1d
...@@ -174,20 +174,28 @@ class IndexableObjectWrapper(CMFCoreIndexableObjectWrapper): ...@@ -174,20 +174,28 @@ class IndexableObjectWrapper(CMFCoreIndexableObjectWrapper):
class RelatedBaseCategory(Method): class RelatedBaseCategory(Method):
"""A Dynamic Method to act as a related key. """A Dynamic Method to act as a related key.
""" """
def __init__(self, id,strict_membership=0): def __init__(self, id, strict_membership=0, related=0):
self._id = id self._id = id
self.strict_membership=strict_membership self.strict_membership=strict_membership
self.related = related
def __call__(self, instance, table_0, table_1, query_table='catalog', **kw): def __call__(self, instance, table_0, table_1, query_table='catalog', **kw):
"""Create the sql code for this related key.""" """Create the sql code for this related key."""
base_category_uid = instance.portal_categories._getOb(self._id).getUid() base_category_uid = instance.portal_categories._getOb(self._id).getUid()
expression_list = [] expression_list = []
append = expression_list.append append = expression_list.append
append('%s.uid = %s.category_uid' % (table_1,table_0)) if self.related:
if self.strict_membership: append('%s.uid = %s.uid' % (table_1,table_0))
append('AND %s.category_strict_membership = 1' % table_0) if self.strict_membership:
append('AND %s.base_category_uid = %s' % (table_0,base_category_uid)) append('AND %s.category_strict_membership = 1' % table_0)
append('AND %s.uid = %s.uid' % (table_0,query_table)) append('AND %s.base_category_uid = %s' % (table_0,base_category_uid))
append('AND %s.category_uid = %s.uid' % (table_0,query_table))
else:
append('%s.uid = %s.category_uid' % (table_1,table_0))
if self.strict_membership:
append('AND %s.category_strict_membership = 1' % table_0)
append('AND %s.base_category_uid = %s' % (table_0,base_category_uid))
append('AND %s.uid = %s.uid' % (table_0,query_table))
return ' '.join(expression_list) return ' '.join(expression_list)
class CatalogTool (UniqueObject, ZCatalog, CMFCoreCatalogTool, ActiveObject): class CatalogTool (UniqueObject, ZCatalog, CMFCoreCatalogTool, ActiveObject):
...@@ -864,17 +872,34 @@ class CatalogTool (UniqueObject, ZCatalog, CMFCoreCatalogTool, ActiveObject): ...@@ -864,17 +872,34 @@ class CatalogTool (UniqueObject, ZCatalog, CMFCoreCatalogTool, ActiveObject):
expected_base_cat_id in base_cat_id_list: expected_base_cat_id in base_cat_id_list:
# We have found a base_category # We have found a base_category
end_key = '_'.join(splitted_key[i:]) end_key = '_'.join(splitted_key[i:])
# accept only some catalog columns
if end_key in ('title', 'uid', 'description', 'reference', if end_key.startswith('related_'):
'relative_url', 'id', 'portal_type'): end_key = end_key[len('related_'):]
if strict: # accept only some catalog columns
related_key_list.append( if end_key in ('title', 'uid', 'description', 'reference',
'%s%s | category,catalog/%s/z_related_strict_%s' % 'relative_url', 'id', 'portal_type',
(prefix, key, end_key, expected_base_cat_id)) 'simulation_state'):
else: if strict:
related_key_list.append( related_key_list.append(
'%s%s | category,catalog/%s/z_related_%s' % '%s%s | category,catalog/%s/z_related_strict_%s_related' %
(prefix, key, end_key, expected_base_cat_id)) (prefix, key, end_key, expected_base_cat_id))
else:
related_key_list.append(
'%s%s | category,catalog/%s/z_related_%s_related' %
(prefix, key, end_key, expected_base_cat_id))
else:
# accept only some catalog columns
if end_key in ('title', 'uid', 'description', 'reference',
'relative_url', 'id', 'portal_type',
'simulation_state'):
if strict:
related_key_list.append(
'%s%s | category,catalog/%s/z_related_strict_%s' %
(prefix, key, end_key, expected_base_cat_id))
else:
related_key_list.append(
'%s%s | category,catalog/%s/z_related_%s' %
(prefix, key, end_key, expected_base_cat_id))
return related_key_list return related_key_list
...@@ -891,12 +916,23 @@ class CatalogTool (UniqueObject, ZCatalog, CMFCoreCatalogTool, ActiveObject): ...@@ -891,12 +916,23 @@ class CatalogTool (UniqueObject, ZCatalog, CMFCoreCatalogTool, ActiveObject):
zope_security = '__roles__' zope_security = '__roles__'
if (name.startswith(DYNAMIC_METHOD_NAME) and \ if (name.startswith(DYNAMIC_METHOD_NAME) and \
(not name.endswith(zope_security))): (not name.endswith(zope_security))):
if name.startswith(STRICT_DYNAMIC_METHOD_NAME):
base_category_id = name[len(STRICT_DYNAMIC_METHOD_NAME):] if name.endswith('_related'):
method = RelatedBaseCategory(base_category_id, strict_membership=1) if name.startswith(STRICT_DYNAMIC_METHOD_NAME):
base_category_id = name[len(STRICT_DYNAMIC_METHOD_NAME):-len('_related')]
method = RelatedBaseCategory(base_category_id,
strict_membership=1, related=1)
else:
base_category_id = name[len(DYNAMIC_METHOD_NAME):-len('_related')]
method = RelatedBaseCategory(base_category_id, related=1)
else: else:
base_category_id = name[len(DYNAMIC_METHOD_NAME):] if name.startswith(STRICT_DYNAMIC_METHOD_NAME):
method = RelatedBaseCategory(base_category_id) base_category_id = name[len(STRICT_DYNAMIC_METHOD_NAME):]
method = RelatedBaseCategory(base_category_id, strict_membership=1)
else:
base_category_id = name[len(DYNAMIC_METHOD_NAME):]
method = RelatedBaseCategory(base_category_id)
setattr(self.__class__, name, method) setattr(self.__class__, name, method)
klass = aq_base(self).__class__ klass = aq_base(self).__class__
if hasattr(klass, 'security'): if hasattr(klass, 'security'):
......
...@@ -2823,6 +2823,144 @@ VALUES ...@@ -2823,6 +2823,144 @@ VALUES
Query(site_title='=foo2'), Query(site_title='=foo2'),
operator='AND')) operator='AND'))
def test_SearchFolderWithRelatedDynamicRelatedKey(self,
quiet=quiet, run=run_all_test):
if not run: return
if not quiet:
message = 'Search Folder With Related Dynamic Related Key'
ZopeTestCase._print('\n%s ' % message)
LOG('Testing... ',0,message)
# Create some objects
portal = self.getPortal()
portal_category = self.getCategoryTool()
portal_category.group.manage_delObjects([x for x in
portal_category.group.objectIds()])
group_nexedi_category = portal_category.group\
.newContent( id = 'nexedi', title='Nexedi',
description='a')
group_nexedi_category2 = portal_category.group\
.newContent( id = 'storever', title='Storever',
description='b')
module = portal.getDefaultModule('Organisation')
organisation = module.newContent(portal_type='Organisation',
title='Nexedi Orga',
description='c')
organisation.setGroup('nexedi')
self.assertEquals(organisation.getGroupValue(), group_nexedi_category)
organisation2 = module.newContent(portal_type='Organisation',
title='Storever Orga',
description='d')
organisation2.setGroup('storever')
organisation2.setTitle('Organisation 2')
self.assertEquals(organisation2.getGroupValue(), group_nexedi_category2)
# Flush message queue
get_transaction().commit()
self.tic()
base_category = portal_category.group
# Try to get the category with the group related organisation title Nexedi
# Orga
category_list = [x.getObject() for x in
base_category.searchFolder(
group_related_title='Nexedi Orga')]
self.assertEquals(category_list, [group_nexedi_category])
category_list = [x.getObject() for x in
base_category.searchFolder(
default_group_related_title='Nexedi Orga')]
self.assertEquals(category_list, [group_nexedi_category])
# Try to get the category with the group related organisation id
category_list = [x.getObject() for x in
base_category.searchFolder(group_related_id='storever')]
self.assertEquals(category_list,[group_nexedi_category2])
# Try to get the category with the group related organisation description 'd'
category_list = [x.getObject() for x in
base_category.searchFolder(group_related_description='d')]
self.assertEquals(category_list,[group_nexedi_category2])
# Try to get the category with the group related organisation description
# 'e'
category_list = [x.getObject() for x in
base_category.searchFolder(group_related_description='e')]
self.assertEquals(category_list,[])
# Try to get the category with the default group related organisation description
# 'e'
category_list = [x.getObject() for x in
base_category.searchFolder(default_group_related_description='e')]
self.assertEquals(category_list,[])
# Try to get the category with the group related organisation relative_url
organisation_relative_url = organisation.getRelativeUrl()
category_list = [x.getObject() for x in
base_category.searchFolder(group_related_relative_url=organisation_relative_url)]
self.assertEquals(category_list, [group_nexedi_category])
# Try to get the category with the group related organisation uid
category_list = [x.getObject() for x in
base_category.searchFolder(group_related_uid=organisation.getUid())]
self.assertEquals(category_list, [group_nexedi_category])
# Try to get the category with the group related organisation id and title
# of the category
category_list = [x.getObject() for x in
base_category.searchFolder(group_related_id=organisation2.getId(),
title='Storever')]
self.assertEquals(category_list,[group_nexedi_category2])
def test_SearchFolderWithRelatedDynamicStrictRelatedKey(self,
quiet=quiet, run=run_all_test):
if not run: return
if not quiet:
message = 'Search Folder With Related Strict Dynamic Related Key'
ZopeTestCase._print('\n%s ' % message)
LOG('Testing... ',0,message)
# Create some objects
portal = self.getPortal()
portal_category = self.getCategoryTool()
portal_category.group.manage_delObjects([x for x in
portal_category.group.objectIds()])
group_nexedi_category = portal_category.group\
.newContent( id = 'nexedi', title='Nexedi',
description='a')
sub_group_nexedi = group_nexedi_category\
.newContent( id = 'erp5', title='ERP5',
description='b')
module = portal.getDefaultModule('Organisation')
organisation = module.newContent(portal_type='Organisation',
title='ERP5 Orga',
description='c')
organisation.setGroup('nexedi/erp5')
self.assertEquals(organisation.getGroupValue(), sub_group_nexedi)
organisation2 = module.newContent(portal_type='Organisation',
title='Nexedi Orga',
description='d')
organisation2.setGroup('nexedi')
# Flush message queue
get_transaction().commit()
self.tic()
base_category = portal_category.group
# Try to get the category with the group related organisation title Nexedi
# Orga
category_list = [x.getObject() for x in
base_category.portal_catalog(
strict_group_related_title='Nexedi Orga')]
self.assertEquals(category_list,[group_nexedi_category])
# Try to get the category with the group related organisation title ERP5
# Orga
category_list = [x.getObject() for x in
base_category.portal_catalog(
strict_group_related_title='ERP5 Orga')]
self.assertEquals(category_list,[sub_group_nexedi])
# Try to get the category with the group related organisation description d
category_list = [x.getObject() for x in
base_category.portal_catalog(
strict_group_related_description='d')]
self.assertEquals(category_list,[group_nexedi_category])
# Try to get the category with the group related organisation description c
category_list = [x.getObject() for x in
base_category.portal_catalog(
strict_group_related_description='c')]
self.assertEquals(category_list,[sub_group_nexedi])
def test_suite(): def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestERP5Catalog)) suite.addTest(unittest.makeSuite(TestERP5Catalog))
......
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