[workflow] add method on IWorkflowable to set initial state of an entity (closes #1686357) stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 19 May 2011 15:56:49 +0200
branchstable
changeset 7401 fded980bbd65
parent 7396 8345ee2c2ea6
child 7403 344aa07dd0f2
[workflow] add method on IWorkflowable to set initial state of an entity (closes #1686357)
entities/wfobjs.py
--- a/entities/wfobjs.py	Thu May 19 10:35:20 2011 +0200
+++ b/entities/wfobjs.py	Thu May 19 15:56:49 2011 +0200
@@ -595,7 +595,7 @@
             stateeid = statename.eid
         else:
             if not isinstance(statename, basestring):
-                warn('[3.5] give a state name', DeprecationWarning)
+                warn('[3.5] give a state name', DeprecationWarning, stacklevel=2)
                 state = self.current_workflow.state_by_eid(statename)
             else:
                 state = self.current_workflow.state_by_name(statename)
@@ -605,3 +605,20 @@
             stateeid = state.eid
         # XXX try to find matching transition?
         return self._add_trinfo(comment, commentformat, tr and tr.eid, stateeid)
+
+    def set_initial_state(self, statename):
+        """set a newly created entity's state to the given state (name or entity)
+        in entity's workflow. This is useful if you don't want it to be the
+        workflow's initial state.
+        """
+        assert self.current_workflow
+        if hasattr(statename, 'eid'):
+            stateeid = statename.eid
+        else:
+            state = self.current_workflow.state_by_name(statename)
+            if state is None:
+                raise WorkflowException('not a %s state: %s' % (self.__regid__,
+                                                                statename))
+            stateeid = state.eid
+        self._cw.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
+                         {'x': self.entity.eid, 's': stateeid})