[wf] new method on WorkflowableMixIn to get the transition which had led to the current sub-workflow 3.5
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 25 Aug 2009 18:30:29 +0200
branch3.5
changeset 2990 f221ba8552ab
parent 2989 dfb3506647e4
child 2991 dab951c08896
[wf] new method on WorkflowableMixIn to get the transition which had led to the current sub-workflow
entities/wfobjs.py
--- a/entities/wfobjs.py	Tue Aug 25 18:29:36 2009 +0200
+++ b/entities/wfobjs.py	Tue Aug 25 18:30:29 2009 +0200
@@ -476,6 +476,26 @@
         # XXX try to find matching transition?
         return self._add_trinfo(comment, commentformat, tr and tr.eid, stateeid)
 
+    def subworkflow_input_transition(self):
+        """return the transition which has went through the current sub-workflow
+        """
+        if self.main_workflow.eid == self.current_workflow.eid:
+            return # doesn't make sense
+        subwfentries = []
+        for trinfo in reversed(self.workflow_history):
+            if (trinfo.transition and
+                trinfo.previous_state.workflow.eid != trinfo.new_state.workflow.eid):
+                # entering or leaving a subworkflow
+                if (subwfentries and
+                    subwfentries[-1].new_state.workflow.eid == trinfo.previous_state.workflow.eid):
+                    # leave
+                    del subwfentries[-1]
+                else:
+                    # enter
+                    subwfentries.append(trinfo)
+        if not subwfentries:
+            return None
+        return subwfentries[-1].transition
 
     def clear_all_caches(self):
         super(WorkflowableMixIn, self).clear_all_caches()