From a5f23b7c549e89f8967ea6dcb2b2f1173ed71df5 Mon Sep 17 00:00:00 2001
From: Sebastien Robin <seb@nexedi.com>
Date: Thu, 13 Apr 2006 12:00:25 +0000
Subject: [PATCH] added support for dynamic default_XXX related key

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@6577 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5Catalog/CatalogTool.py           | 23 +++++++++++++++-----
 product/ERP5Catalog/tests/testERP5Catalog.py |  8 +++++++
 2 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/product/ERP5Catalog/CatalogTool.py b/product/ERP5Catalog/CatalogTool.py
index dd5dd22e497..0b14f047b42 100644
--- a/product/ERP5Catalog/CatalogTool.py
+++ b/product/ERP5Catalog/CatalogTool.py
@@ -529,39 +529,50 @@ class CatalogTool (UniqueObject, ZCatalog, CMFCoreCatalogTool, ActiveObject):
     security.declarePrivate('getDynamicRelatedKeyList')
     def getDynamicRelatedKeyList(self, sql_catalog_id=None,**kw):
       """
-      Return the list of related keys.
+      Return the list of dynamic related keys.
       This method will try to automatically generate new related key
       by looking at the category tree.
+
+      For exemple it will generate:
+      destination_title | category,catalog/title/z_related_destination
+      default_destination_title | category,catalog/title/z_related_destination
       """
       if len(kw)>0:
         # import pdb;pdb.set_trace()
         pass
       related_key_list = []
       base_cat_id_list = self.portal_categories.getBaseCategoryList()
+      default_string = 'default_'
       for key in kw.keys():
+        prefix = ''
+        if key.startswith(default_string):
+          key = key[len(default_string):]
+          prefix = default_string
         splitted_key = key.split('_')
+        # look from the end of the key from the beginning if we
+        # can find 'title', or 'portal_type'...
         for i in range(1,len(splitted_key))[::-1]:
           expected_base_cat_id = '_'.join(splitted_key[0:i])
           if expected_base_cat_id!='parent' and \
              expected_base_cat_id in base_cat_id_list:
             # We have found a base_category
             end_key = '_'.join(splitted_key[i:])
-            if end_key in ('title','uid','description','id'):
-              related_key_list.append('%s | category,catalog/%s/z_related_%s' %
-                                          (key,end_key,expected_base_cat_id))
+            # accept only some catalog columns
+            if end_key in ('title','uid','description','id','portal_type'):
+              related_key_list.append('%s%s | category,catalog/%s/z_related_%s' %
+                                        (prefix,key,end_key,expected_base_cat_id))
 
       return related_key_list
 
     def _aq_dynamic(self, name):
       """
       Automatic related key generation.
-      Will generate z_related_destination if possible
+      Will generate z_related_[base_category_id] if possible
       """
       aq_base_name = getattr(aq_base(self), name, None)
       if aq_base_name == None:
         DYNAMIC_METHOD_NAME = 'z_related_'
         method_name_length = len(DYNAMIC_METHOD_NAME)
-
         zope_security = '__roles__'
         if (name.startswith(DYNAMIC_METHOD_NAME) and \
           (not name.endswith(zope_security))):
diff --git a/product/ERP5Catalog/tests/testERP5Catalog.py b/product/ERP5Catalog/tests/testERP5Catalog.py
index 58530169001..9233647052d 100644
--- a/product/ERP5Catalog/tests/testERP5Catalog.py
+++ b/product/ERP5Catalog/tests/testERP5Catalog.py
@@ -700,5 +700,13 @@ class TestERP5Catalog(ERP5TypeTestCase):
     organisation_list = [x.getObject() for x in 
                          module.searchFolder(group_description='a')]
     self.assertEquals(organisation_list,[organisation])
+    # Try to get the organisation with the group description 'c'
+    organisation_list = [x.getObject() for x in 
+                         module.searchFolder(group_description='c')]
+    self.assertEquals(organisation_list,[])
+    # Try to get the organisation with the default group description 'c'
+    organisation_list = [x.getObject() for x in 
+                         module.searchFolder(default_group_description='c')]
+    self.assertEquals(organisation_list,[])
 
 
-- 
2.30.9