[workflow] new 'fire_transition_if_possible' method on IWorkflowable to fire a transition if it's possible stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 19 May 2011 17:53:01 +0200
branchstable
changeset 7404 02a786d7ebce
parent 7403 344aa07dd0f2
child 7405 8c752d113ebb
[workflow] new 'fire_transition_if_possible' method on IWorkflowable to fire a transition if it's possible
entities/wfobjs.py
--- a/entities/wfobjs.py	Thu May 19 17:53:00 2011 +0200
+++ b/entities/wfobjs.py	Thu May 19 17:53:01 2011 +0200
@@ -572,18 +572,31 @@
             kwargs['to_state'] = self._cw.entity_from_eid(tseid)
         return self._cw.create_entity('TrInfo', **kwargs)
 
-    def fire_transition(self, tr, comment=None, commentformat=None):
-        """change the entity's state by firing transition of the given name in
-        entity's workflow
-        """
+    def _get_transition(self, tr):
         assert self.current_workflow
         if isinstance(tr, basestring):
             _tr = self.current_workflow.transition_by_name(tr)
             assert _tr is not None, 'not a %s transition: %s' % (
                 self.__regid__, tr)
             tr = _tr
+        return tr
+
+    def fire_transition(self, tr, comment=None, commentformat=None):
+        """change the entity's state by firing given transition (name or entity)
+        in entity's workflow
+        """
+        tr = self._get_transition(tr)
         return self._add_trinfo(comment, commentformat, tr.eid)
 
+    def fire_transition_if_possible(self, tr, comment=None, commentformat=None):
+        """change the entity's state by firing given transition (name or entity)
+        in entity's workflow if this transition is possible
+        """
+        tr = self._get_transition(tr)
+        if any(tr_ for tr_ in self.possible_transitions()
+               if tr_.eid == tr.eid):
+            self.fire_transition(tr)
+
     def change_state(self, statename, comment=None, commentformat=None, tr=None):
         """change the entity's state to the given state (name or entity) in
         entity's workflow. This method should only by used by manager to fix an