entities/wfobjs.py
brancholdstable
changeset 7676 cc3987eb793c
parent 7404 02a786d7ebce
child 7406 e772a2c57b00
equal deleted inserted replaced
7388:dc319ece0bd6 7676:cc3987eb793c
   570             kwargs['by_transition'] = self._cw.entity_from_eid(treid)
   570             kwargs['by_transition'] = self._cw.entity_from_eid(treid)
   571         if tseid is not None:
   571         if tseid is not None:
   572             kwargs['to_state'] = self._cw.entity_from_eid(tseid)
   572             kwargs['to_state'] = self._cw.entity_from_eid(tseid)
   573         return self._cw.create_entity('TrInfo', **kwargs)
   573         return self._cw.create_entity('TrInfo', **kwargs)
   574 
   574 
   575     def fire_transition(self, tr, comment=None, commentformat=None):
   575     def _get_transition(self, tr):
   576         """change the entity's state by firing transition of the given name in
       
   577         entity's workflow
       
   578         """
       
   579         assert self.current_workflow
   576         assert self.current_workflow
   580         if isinstance(tr, basestring):
   577         if isinstance(tr, basestring):
   581             _tr = self.current_workflow.transition_by_name(tr)
   578             _tr = self.current_workflow.transition_by_name(tr)
   582             assert _tr is not None, 'not a %s transition: %s' % (
   579             assert _tr is not None, 'not a %s transition: %s' % (
   583                 self.__regid__, tr)
   580                 self.__regid__, tr)
   584             tr = _tr
   581             tr = _tr
       
   582         return tr
       
   583 
       
   584     def fire_transition(self, tr, comment=None, commentformat=None):
       
   585         """change the entity's state by firing given transition (name or entity)
       
   586         in entity's workflow
       
   587         """
       
   588         tr = self._get_transition(tr)
   585         return self._add_trinfo(comment, commentformat, tr.eid)
   589         return self._add_trinfo(comment, commentformat, tr.eid)
       
   590 
       
   591     def fire_transition_if_possible(self, tr, comment=None, commentformat=None):
       
   592         """change the entity's state by firing given transition (name or entity)
       
   593         in entity's workflow if this transition is possible
       
   594         """
       
   595         tr = self._get_transition(tr)
       
   596         if any(tr_ for tr_ in self.possible_transitions()
       
   597                if tr_.eid == tr.eid):
       
   598             self.fire_transition(tr)
   586 
   599 
   587     def change_state(self, statename, comment=None, commentformat=None, tr=None):
   600     def change_state(self, statename, comment=None, commentformat=None, tr=None):
   588         """change the entity's state to the given state (name or entity) in
   601         """change the entity's state to the given state (name or entity) in
   589         entity's workflow. This method should only by used by manager to fix an
   602         entity's workflow. This method should only by used by manager to fix an
   590         entity's state when their is no matching transition, otherwise
   603         entity's state when their is no matching transition, otherwise
   593         assert self.current_workflow
   606         assert self.current_workflow
   594         if hasattr(statename, 'eid'):
   607         if hasattr(statename, 'eid'):
   595             stateeid = statename.eid
   608             stateeid = statename.eid
   596         else:
   609         else:
   597             if not isinstance(statename, basestring):
   610             if not isinstance(statename, basestring):
   598                 warn('[3.5] give a state name', DeprecationWarning)
   611                 warn('[3.5] give a state name', DeprecationWarning, stacklevel=2)
   599                 state = self.current_workflow.state_by_eid(statename)
   612                 state = self.current_workflow.state_by_eid(statename)
   600             else:
   613             else:
   601                 state = self.current_workflow.state_by_name(statename)
   614                 state = self.current_workflow.state_by_name(statename)
   602             if state is None:
   615             if state is None:
   603                 raise WorkflowException('not a %s state: %s' % (self.__regid__,
   616                 raise WorkflowException('not a %s state: %s' % (self.__regid__,
   604                                                                 statename))
   617                                                                 statename))
   605             stateeid = state.eid
   618             stateeid = state.eid
   606         # XXX try to find matching transition?
   619         # XXX try to find matching transition?
   607         return self._add_trinfo(comment, commentformat, tr and tr.eid, stateeid)
   620         return self._add_trinfo(comment, commentformat, tr and tr.eid, stateeid)
       
   621 
       
   622     def set_initial_state(self, statename):
       
   623         """set a newly created entity's state to the given state (name or entity)
       
   624         in entity's workflow. This is useful if you don't want it to be the
       
   625         workflow's initial state.
       
   626         """
       
   627         assert self.current_workflow
       
   628         if hasattr(statename, 'eid'):
       
   629             stateeid = statename.eid
       
   630         else:
       
   631             state = self.current_workflow.state_by_name(statename)
       
   632             if state is None:
       
   633                 raise WorkflowException('not a %s state: %s' % (self.__regid__,
       
   634                                                                 statename))
       
   635             stateeid = state.eid
       
   636         self._cw.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s',
       
   637                          {'x': self.entity.eid, 's': stateeid})