From 3a76f8dbdccd317497c8a80d19026dfb844922ea Mon Sep 17 00:00:00 2001
From: Romain Courteaud <romain@nexedi.com>
Date: Fri, 15 Sep 2006 15:13:19 +0000
Subject: [PATCH] Modify Folder_delete. The trash button does not allow anymore
 to remove object from the database when there is a workflow associated to
 this object (excepted for edit_workflow). In such case, there must be a
 workflow transition called "delete_action", which changes the state of the
 object to "deleted".

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@10004 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 .../portal_skins/erp5_core/Folder_delete.xml  | 153 ++++++++++++++----
 product/ERP5/bootstrap/erp5_core/bt/revision  |   2 +-
 2 files changed, 127 insertions(+), 28 deletions(-)

diff --git a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/Folder_delete.xml b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/Folder_delete.xml
index b2cb2f4a03..7f746f79f1 100644
--- a/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/Folder_delete.xml
+++ b/product/ERP5/bootstrap/erp5_core/SkinTemplateItem/portal_skins/erp5_core/Folder_delete.xml
@@ -106,42 +106,101 @@ if error:\n
 elif uids is not None:\n
   # Check if there is some related objets.\n
   object_used = 0\n
-  for uid in uids:\n
-    object = context.portal_catalog(uid=uid)[0].getObject()\n
-    if Object_hasRelation(object):\n
-      object_used += 1\n
-  if object_used>0:\n
-      if object_used == 1:\n
-        message = N_("Sorry,+1+Item+Is+In+Use.")\n
-        qs = \'?portal_status_message=%s\' % message\n
-      else:\n
-        message = N_("Sorry,+${count}+Items+Are+In+Use.",\n
-                     mapping={\'count\': repr(object_used)})\n
-        qs = \'?portal_status_message=%s\' % message  \n
+\n
+  object_list = [x.getObject() for x in context.portal_catalog(uid=uids)]\n
+  object_used = sum([Object_hasRelation(x) for x in object_list])\n
+\n
+  if object_used > 0:\n
+    if object_used == 1:\n
+      message = N_("Sorry,+1+Item+Is+In+Use.")\n
+    else:\n
+      message = N_("Sorry,+${count}+Items+Are+In+Use.",\n
+                   mapping={\'count\': repr(object_used)})\n
+    qs = \'?portal_status_message=%s\' % message  \n
   else:\n
-    try : \n
-      object_ids = \', \'.join([x.getObject().getId()\n
-                              for x in context.portal_catalog(uid=uids)])\n
-      context.manage_delObjects(uids=uids, REQUEST=REQUEST)\n
+\n
+    # Do not delete objects which have a workflow history    \n
+    object_to_remove_list = []\n
+    object_to_delete_list = []\n
+\n
+    for object in object_list:\n
+\n
+      workflow_id_list = [x.id for x in object.portal_workflow.getWorkflowsFor(object)]\n
+      # XXX Remove edit_workflow, which should no prevent deletion\n
+      try:\n
+        workflow_id_list.remove("edit_workflow")\n
+      except ValueError:\n
+        pass\n
+\n
+      # If a workflow manage a history, \n
+      # object should not be removed, but only put in state deleted\n
+      for wf_id in workflow_id_list:\n
+        # It is important to use the history variable\n
+        try:\n
+          list_history_item = context.portal_workflow.getInfoFor(\n
+                        ob=object, name="history", wf_id=wf_id)\n
+        # XXX Can not use WorkflowException\n
+        except ConflictError:\n
+          raise\n
+        except:\n
+          pass\n
+        else:\n
+          object_to_delete_list.append(object)\n
+          break\n
+      else:\n
+        object_to_remove_list.append(object)\n
+\n
+    # Remove some objects\n
+    try: \n
+      if object_to_remove_list != []:\n
+        context.manage_delObjects(\n
+                      uids=[x.getUid() for x in object_to_remove_list],\n
+                      REQUEST=REQUEST)\n
+    except ConflictError: \n
+      raise\n
+    except Exception, message : \n
+      qs = \'?portal_status_message=%s\' % message\n
+    else:\n
+      object_ids = [x.getId() for x in object_to_remove_list]\n
       comment = N_(\'Deleted objects: ${object_ids}\',\n
                    mapping={\'object_ids\': object_ids})\n
       if \'edit_action\' in [x.get(\'id\', \'\') for x in\n
-                           context.portal_workflow.getActionsFor(context)] :\n
+                           context.portal_workflow.getActionsFor(context)]:\n
         # record object deletion in workflow history\n
         context.getPortalObject().portal_workflow.doActionFor(\n
                       context,\n
                       \'edit_action\',\n
                       comment=comment)\n
+\n
       message = N_("Deleted.")\n
+\n
+      # Change workflow state of others objects\n
+      not_deleted_count = 0\n
+      for object in object_to_delete_list:\n
+        try:\n
+          context.getPortalObject().portal_workflow.doActionFor(\n
+                      object,\n
+                      \'delete_action\')\n
+        except ConflictError:\n
+          raise\n
+        except:\n
+          not_deleted_count += 1\n
+          pass\n
+\n
+      # Generate message\n
+      if not_deleted_count == 1:\n
+        message = N_("Sorry+You+Can+Not+Delete+${count}+Item.",\n
+                     mapping={\'count\': not_deleted_count})\n
+      elif not_deleted_count > 1:\n
+        message = N_("Sorry+You+Can+Not+Delete+${count}+Items.",\n
+                     mapping={\'count\': not_deleted_count})\n
       qs = \'?portal_status_message=%s\' % message\n
-    except ConflictError : raise\n
-    except Exception, message : \n
-      qs = \'?portal_status_message=%s\' % message\n
+\n
 else:\n
   message = N_("Please+select+one+or+more+items+first.")\n
   qs = \'?portal_status_message=%s\' % message\n
 \n
-return REQUEST.RESPONSE.redirect( ret_url + qs )\n
+return REQUEST.RESPONSE.redirect("%s%s" % (ret_url, qs))\n
 
 
 ]]></string> </value>
@@ -152,12 +211,24 @@ return REQUEST.RESPONSE.redirect( ret_url + qs )\n
               <none/>
             </value>
         </item>
+        <item>
+            <key> <string>_dav_writelocks</string> </key>
+            <value>
+              <persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
+            </value>
+        </item>
         <item>
             <key> <string>_filepath</string> </key>
             <value>
               <none/>
             </value>
         </item>
+        <item>
+            <key> <string>_owner</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
         <item>
             <key> <string>_params</string> </key>
             <value> <string>form_id=\'\',selection_index=None,object_uid=None,selection_name=None,field_id=None,uids=None,cancel_url=\'\',listbox_uid=[],md5_object_uid_list=\'\'</string> </value>
@@ -210,17 +281,24 @@ return REQUEST.RESPONSE.redirect( ret_url + qs )\n
                             <string>message</string>
                             <string>None</string>
                             <string>object_used</string>
-                            <string>_getiter_</string>
-                            <string>uid</string>
-                            <string>_getitem_</string>
-                            <string>object</string>
-                            <string>repr</string>
                             <string>append</string>
                             <string>$append0</string>
+                            <string>_getiter_</string>
                             <string>x</string>
+                            <string>object_list</string>
+                            <string>sum</string>
+                            <string>repr</string>
+                            <string>object_to_remove_list</string>
+                            <string>object_to_delete_list</string>
+                            <string>object</string>
+                            <string>workflow_id_list</string>
+                            <string>ValueError</string>
+                            <string>wf_id</string>
+                            <string>list_history_item</string>
+                            <string>Exception</string>
                             <string>object_ids</string>
                             <string>comment</string>
-                            <string>Exception</string>
+                            <string>not_deleted_count</string>
                           </tuple>
                         </value>
                     </item>
@@ -262,4 +340,25 @@ return REQUEST.RESPONSE.redirect( ret_url + qs )\n
       </dictionary>
     </pickle>
   </record>
+  <record id="2" aka="AAAAAAAAAAI=">
+    <pickle>
+      <tuple>
+        <tuple>
+          <string>Persistence</string>
+          <string>PersistentMapping</string>
+        </tuple>
+        <none/>
+      </tuple>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>_container</string> </key>
+            <value>
+              <dictionary/>
+            </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
 </ZopeData>
diff --git a/product/ERP5/bootstrap/erp5_core/bt/revision b/product/ERP5/bootstrap/erp5_core/bt/revision
index 832332893a..3d9aebb2cc 100644
--- a/product/ERP5/bootstrap/erp5_core/bt/revision
+++ b/product/ERP5/bootstrap/erp5_core/bt/revision
@@ -1 +1 @@
-67
\ No newline at end of file
+68
\ No newline at end of file
-- 
2.30.9