From fb52f4a5238fde10ebd1f9cc62150f9c2d8c7c09 Mon Sep 17 00:00:00 2001
From: Sebastien Robin <seb@nexedi.com>
Date: Fri, 8 Jul 2016 14:42:11 +0000
Subject: [PATCH] erp5_core: optimize jump to related object when jumping to a
 module

Avoid getting all related values when we jump to a module.
---
 .../erp5_core/Base_jumpToRelatedObject.py     | 29 ++++++++++++++-----
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/Base_jumpToRelatedObject.py b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/Base_jumpToRelatedObject.py
index 37bd8623f3..891ac6d783 100644
--- a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/Base_jumpToRelatedObject.py
+++ b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/Base_jumpToRelatedObject.py
@@ -10,16 +10,33 @@ else:
 
 # FIXME: performance problem getting *all* related documents URL is not scalable.
 getter_base_name = ''.join([x.capitalize() for x in base_category.split('_')])
+
+if same_type(portal_type, ''):
+  portal_type = [portal_type]
+
 if related:
+  # Try to see if we have already many visible results with the catalog and if we are
+  # in the case we should jump to a module (in which case we would also use the catalog
+  # to display result). This can avoid retrieving all objects. It would be better to have
+  # get[Category]RelatedList returning a lazy list directly.
+  if len(portal_type) == 1:
+    catalog_list = portal.portal_catalog(portal_type=portal_type, limit=2,
+                                       **{'default_%s_uid' % base_category: relation.getUid()})
+    if len(catalog_list) == 2:
+      related_list = catalog_list
+      module_id = portal.getDefaultModuleId(portal_type[0], None)
+      if module_id is not None:
+        module = portal.getDefaultModule(portal_type[0])
+        return module.Base_redirect(
+                 'view', keep_items={'default_%s_uid' % base_category: relation.getUid(),
+                                     'ignore_hide_rows': 1,
+                                     'reset': 1})
   search_method = getattr(relation, 'get%sRelatedList' % getter_base_name)
 else:
   search_method = getattr(relation, 'get%sList' % getter_base_name)
 
 related_list = search_method(portal_type = portal_type)
 
-if same_type(portal_type, ''):
-  portal_type = [portal_type]
-
 relation_found = 0
 if len(related_list) == 0:
   url = context.absolute_url()
@@ -61,11 +78,7 @@ else:
     module_id = portal.getDefaultModuleId(portal_type[0], None)
     if module_id is not None:
       module = portal.getDefaultModule(portal_type[0])
-      if related:
-        return module.Base_redirect(
-                 'view', keep_items={'default_%s_uid' % base_category: relation.getUid(),
-                                     'ignore_hide_rows': 1,
-                                     'reset': 1})
+      # related case already done, we have to do the non related case
       # We can not pass parameters through url, otherwise we might have too long urls (if many uids).
       # Therefore check if we are in usual case of module form having a listbox, and update
       # selection for it
-- 
2.30.9