From c37b6f07babab99cef9f6c61c13cfb2e1800ab82 Mon Sep 17 00:00:00 2001
From: Julien Muchembled <jm@nexedi.com>
Date: Fri, 7 Jan 2011 13:42:24 +0000
Subject: [PATCH] Do not initialize history for workflows that have no state at
 all

Since commit 38864 (hide worklist if for portal types that are not chained to
the workflow), we may want to create workflows that only contain worklists,
without polluting histories.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@42116 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5Type/patches/DCWorkflow.py | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/product/ERP5Type/patches/DCWorkflow.py b/product/ERP5Type/patches/DCWorkflow.py
index 508f27ad98..6d4e381b85 100644
--- a/product/ERP5Type/patches/DCWorkflow.py
+++ b/product/ERP5Type/patches/DCWorkflow.py
@@ -308,21 +308,22 @@ def DCWorkflowDefinition_executeTransition(self, ob, tdef=None, kwargs=None):
     validation_exc = None
 
     # Figure out the old and new states.
-    old_sdef = self._getWorkflowStateOf(ob)
-    old_state = old_sdef.getId()
+    old_state = self._getWorkflowStateOf(ob, True)
     if tdef is None:
         new_state = self.initial_state
+        if not new_state:
+            # Do nothing if there is no initial state. We may want to create
+            # workflows with no state at all, only for worklists.
+            return
         former_status = {}
     else:
-        new_state = tdef.new_state_id
-        if not new_state:
-            # Stay in same state.
-            new_state = old_state
+        new_state = tdef.new_state_id or old_state
         former_status = self._getStatusOf(ob)
-    new_sdef = self.states.get(new_state, None)
-    if new_sdef is None:
-        raise WorkflowException, (
-            'Destination state undefined: ' + new_state)
+    old_sdef = self.states[old_state]
+    try:
+        new_sdef = self.states[new_state]
+    except KeyError:
+        raise WorkflowException('Destination state undefined: ' + new_state)
 
     # Execute the "before" script.
     before_script_success = 1
-- 
2.30.9