From 2c9dfa515c6d1869cb3935f0e50e8b0ab0dcd4cb Mon Sep 17 00:00:00 2001
From: Ayush Tiwari <ayush.tiwari@nexedi.com>
Date: Thu, 6 Oct 2016 17:49:27 +0200
Subject: [PATCH] ERP5 Base: Refactor addition of new objects for erp5 objects

It's better to create new objects using same script rather than
keeping 2 scripts that try to do the same thing: Folder_create
(script used to add in case we create new object using the icon next to actions bar)
did not support the getRedirectParameter API. We now do all the work
related to adding new object in Folder_create itself.


Test Result : https://nexedi.erp5.net/test_result_module/20161006-39C681E1
Reviewed by: @jm

/reviewed-on https://lab.nexedi.com/nexedi/erp5/merge_requests/177
---
 .../erp5_xhtml_style/Base_doAction.py         | 15 +++---------
 .../Base_getRedirectParameterDictAfterAdd.py  |  2 +-
 .../erp5_xhtml_style/Folder_create.py         | 24 ++++++++++++-------
 .../erp5_xhtml_style/Folder_create.xml        |  2 +-
 product/ERP5Type/Base.py                      | 10 ++------
 5 files changed, 22 insertions(+), 31 deletions(-)

diff --git a/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Base_doAction.py b/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Base_doAction.py
index 8c9e981298..92e09bc320 100644
--- a/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Base_doAction.py
+++ b/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Base_doAction.py
@@ -34,18 +34,9 @@ if doAction0 in ('object', 'workflow', 'folder'):
     kw['dialog_category'] = 'object_action'
 # Otherwise, check if this is an automatic menu (add)
 elif doAction0 == 'add':
-  type_name = ' '.join(Base_doAction[1:])
-  new_content = context.newContent(portal_type=type_name,
-                                   container=context)
-  preserved_parameter_dict['portal_status_message'] = Base_translateString("Object created.")
-  preserved_parameter_dict['editable_mode'] = 1
-  try:
-    tmp_kw = new_content.getRedirectParameterDictAfterAdd(context, **kw)
-  except AttributeError:
-    # Maybe the Product is not upgraded yet.
-    tmp_kw = new_content.Base_getRedirectParameterDictAfterAdd(context, **kw)
-  redirect_url = tmp_kw.pop('redirect_url', new_content.absolute_url())
-  kw.update(tmp_kw)
+  return context.Folder_create(' '.join(Base_doAction[1:]),
+                               preserved_parameter_dict,
+                               **kw)
 # Otherwise, check if this is an automatic menu (template)
 elif doAction0 == 'template':
   template_relative_url = ' '.join(Base_doAction[1:])
diff --git a/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Base_getRedirectParameterDictAfterAdd.py b/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Base_getRedirectParameterDictAfterAdd.py
index 278e8c6e56..c5df0950ef 100644
--- a/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Base_getRedirectParameterDictAfterAdd.py
+++ b/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Base_getRedirectParameterDictAfterAdd.py
@@ -1,2 +1,2 @@
 # This is the default method for a redirection after being added.
-return dict(redirect_url=context.absolute_url(), selection_index=None, selection_name=None)
+return dict(selection_index=None, selection_name=None)
diff --git a/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Folder_create.py b/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Folder_create.py
index e1898bd6f2..86081dff43 100644
--- a/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Folder_create.py
+++ b/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Folder_create.py
@@ -1,12 +1,18 @@
 Base_translateString = context.getPortalObject().Base_translateString
-allowed_type_list = context.getVisibleAllowedContentTypeList()
 
-if not allowed_type_list:
-  return context.ERP5Site_redirect(context.absolute_url(), keep_items={'portal_status_message': Base_translateString("You are not allowed to add new content in this context.")})
+if not type_name:
+  allowed_type_list = context.getVisibleAllowedContentTypeList()
+  if not allowed_type_list:
+    return context.Base_redirect(keep_items={'portal_status_message':
+      Base_translateString("You are not allowed to add new content in this context.")})
+  # newContent will add the first allowed type when we do not specify portal_type=
+  type_name = allowed_type_list[0]
 
-# newContent will add the first allowed type when we do not specify portal_type=
-new_object = context.newContent(portal_type=allowed_type_list[0])
-
-return context.ERP5Site_redirect(new_object.absolute_url(),
-                                 keep_items={'portal_status_message': Base_translateString("Object created."),
-                                             'editable_mode': 1})
+if keep_items is None:
+  keep_items =  {}
+new_content = context.newContent(portal_type=type_name)
+keep_items['portal_status_message'] = Base_translateString("Object created.")
+keep_items['editable_mode'] = 1
+kw = new_content.getRedirectParameterDictAfterAdd(context, **kw)
+redirect_url = kw.pop('redirect_url', None)
+return new_content.Base_redirect(redirect_url, keep_items=keep_items, **kw)
diff --git a/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Folder_create.xml b/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Folder_create.xml
index 10146c6176..29d49c4080 100644
--- a/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Folder_create.xml
+++ b/product/ERP5/bootstrap/erp5_xhtml_style/SkinTemplateItem/portal_skins/erp5_xhtml_style/Folder_create.xml
@@ -50,7 +50,7 @@
         </item>
         <item>
             <key> <string>_params</string> </key>
-            <value> <string></string> </value>
+            <value> <string>type_name=None, keep_items=None, **kw</string> </value>
         </item>
         <item>
             <key> <string>id</string> </key>
diff --git a/product/ERP5Type/Base.py b/product/ERP5Type/Base.py
index 422c55e1da..a882e60b37 100644
--- a/product/ERP5Type/Base.py
+++ b/product/ERP5Type/Base.py
@@ -2930,14 +2930,8 @@ class Base( CopyContainer,
   def getRedirectParameterDictAfterAdd(self, container, **kw):
     """Return a dict of parameters to specify where the user is redirected
     to after a new object is added in the UI."""
-    method = self._getTypeBasedMethod('getRedirectParameterDictAfterAdd',
-                                      'Base_getRedirectParameterDictAfterAdd')
-    if method is not None:
-      return method(container, **kw)
-
-    # XXX this should not happen, unless the Business Template is broken.
-    return dict(redirect_url=container.absolute_url(),
-                selection_index=None, selection_name=None)
+    method = self._getTypeBasedMethod('getRedirectParameterDictAfterAdd')
+    return method(container, **kw)
 
   security.declareProtected(Permissions.ModifyPortalContent, 'setGuid')
   def setGuid(self):
-- 
2.30.9