hooks/workflow.py
changeset 6142 8bc6eac1fac1
parent 5877 0c7b7b76a84f
child 6757 bc878ec35794
equal deleted inserted replaced
6141:b8287e54b528 6142:8bc6eac1fac1
   133         if wftr is None:
   133         if wftr is None:
   134             # inconsistency detected
   134             # inconsistency detected
   135             qname = role_name('to_state', 'subject')
   135             qname = role_name('to_state', 'subject')
   136             msg = session._("state doesn't belong to entity's current workflow")
   136             msg = session._("state doesn't belong to entity's current workflow")
   137             raise ValidationError(self.trinfo.eid, {'to_state': msg})
   137             raise ValidationError(self.trinfo.eid, {'to_state': msg})
   138         tostate = wftr.get_exit_point(forentity, trinfo['to_state'])
   138         tostate = wftr.get_exit_point(forentity, trinfo.cw_attr_cache['to_state'])
   139         if tostate is not None:
   139         if tostate is not None:
   140             # reached an exit point
   140             # reached an exit point
   141             msg = session._('exiting from subworkflow %s')
   141             msg = session._('exiting from subworkflow %s')
   142             msg %= session._(iworkflowable.current_workflow.name)
   142             msg %= session._(iworkflowable.current_workflow.name)
   143             session.transaction_data[(forentity.eid, 'subwfentrytr')] = True
   143             session.transaction_data[(forentity.eid, 'subwfentrytr')] = True
   183     def __call__(self):
   183     def __call__(self):
   184         session = self._cw
   184         session = self._cw
   185         entity = self.entity
   185         entity = self.entity
   186         # first retreive entity to which the state change apply
   186         # first retreive entity to which the state change apply
   187         try:
   187         try:
   188             foreid = entity['wf_info_for']
   188             foreid = entity.cw_attr_cache['wf_info_for']
   189         except KeyError:
   189         except KeyError:
   190             qname = role_name('wf_info_for', 'subject')
   190             qname = role_name('wf_info_for', 'subject')
   191             msg = session._('mandatory relation')
   191             msg = session._('mandatory relation')
   192             raise ValidationError(entity.eid, {qname: msg})
   192             raise ValidationError(entity.eid, {qname: msg})
   193         forentity = session.entity_from_eid(foreid)
   193         forentity = session.entity_from_eid(foreid)
   211         swtr = session.transaction_data.pop((forentity.eid, 'subwfentrytr'), None)
   211         swtr = session.transaction_data.pop((forentity.eid, 'subwfentrytr'), None)
   212         cowpowers = (session.user.is_in_group('managers')
   212         cowpowers = (session.user.is_in_group('managers')
   213                      or not session.write_security)
   213                      or not session.write_security)
   214         # no investigate the requested state change...
   214         # no investigate the requested state change...
   215         try:
   215         try:
   216             treid = entity['by_transition']
   216             treid = entity.cw_attr_cache['by_transition']
   217         except KeyError:
   217         except KeyError:
   218             # no transition set, check user is a manager and destination state
   218             # no transition set, check user is a manager and destination state
   219             # is specified (and valid)
   219             # is specified (and valid)
   220             if not cowpowers:
   220             if not cowpowers:
   221                 qname = role_name('by_transition', 'subject')
   221                 qname = role_name('by_transition', 'subject')
   222                 msg = session._('mandatory relation')
   222                 msg = session._('mandatory relation')
   223                 raise ValidationError(entity.eid, {qname: msg})
   223                 raise ValidationError(entity.eid, {qname: msg})
   224             deststateeid = entity.get('to_state')
   224             deststateeid = entity.cw_attr_cache.get('to_state')
   225             if not deststateeid:
   225             if not deststateeid:
   226                 qname = role_name('by_transition', 'subject')
   226                 qname = role_name('by_transition', 'subject')
   227                 msg = session._('mandatory relation')
   227                 msg = session._('mandatory relation')
   228                 raise ValidationError(entity.eid, {qname: msg})
   228                 raise ValidationError(entity.eid, {qname: msg})
   229             deststate = wf.state_by_eid(deststateeid)
   229             deststate = wf.state_by_eid(deststateeid)
   245                         'tr': session._(tr.name), 'st': session._(fromstate.name)}
   245                         'tr': session._(tr.name), 'st': session._(fromstate.name)}
   246                     raise ValidationError(entity.eid, {qname: msg})
   246                     raise ValidationError(entity.eid, {qname: msg})
   247                 if not tr.may_be_fired(foreid):
   247                 if not tr.may_be_fired(foreid):
   248                     msg = session._("transition may not be fired")
   248                     msg = session._("transition may not be fired")
   249                     raise ValidationError(entity.eid, {qname: msg})
   249                     raise ValidationError(entity.eid, {qname: msg})
   250             if entity.get('to_state'):
   250             deststateeid = entity.cw_attr_cache.get('to_state')
   251                 deststateeid = entity['to_state']
   251             if deststateeid is not None:
   252                 if not cowpowers and deststateeid != tr.destination(forentity).eid:
   252                 if not cowpowers and deststateeid != tr.destination(forentity).eid:
   253                     qname = role_name('by_transition', 'subject')
   253                     qname = role_name('by_transition', 'subject')
   254                     msg = session._("transition isn't allowed")
   254                     msg = session._("transition isn't allowed")
   255                     raise ValidationError(entity.eid, {qname: msg})
   255                     raise ValidationError(entity.eid, {qname: msg})
   256                 if swtr is None:
   256                 if swtr is None:
   260                         msg = session._("state doesn't belong to entity's workflow")
   260                         msg = session._("state doesn't belong to entity's workflow")
   261                         raise ValidationError(entity.eid, {qname: msg})
   261                         raise ValidationError(entity.eid, {qname: msg})
   262             else:
   262             else:
   263                 deststateeid = tr.destination(forentity).eid
   263                 deststateeid = tr.destination(forentity).eid
   264         # everything is ok, add missing information on the trinfo entity
   264         # everything is ok, add missing information on the trinfo entity
   265         entity['from_state'] = fromstate.eid
   265         entity.cw_edited['from_state'] = fromstate.eid
   266         entity['to_state'] = deststateeid
   266         entity.cw_edited['to_state'] = deststateeid
   267         nocheck = session.transaction_data.setdefault('skip-security', set())
   267         nocheck = session.transaction_data.setdefault('skip-security', set())
   268         nocheck.add((entity.eid, 'from_state', fromstate.eid))
   268         nocheck.add((entity.eid, 'from_state', fromstate.eid))
   269         nocheck.add((entity.eid, 'to_state', deststateeid))
   269         nocheck.add((entity.eid, 'to_state', deststateeid))
   270         _FireAutotransitionOp(session, entity=forentity)
   270         _FireAutotransitionOp(session, entity=forentity)
   271 
   271 
   276     __select__ = WorkflowHook.__select__ & is_instance('TrInfo')
   276     __select__ = WorkflowHook.__select__ & is_instance('TrInfo')
   277     events = ('after_add_entity',)
   277     events = ('after_add_entity',)
   278 
   278 
   279     def __call__(self):
   279     def __call__(self):
   280         trinfo = self.entity
   280         trinfo = self.entity
   281         _change_state(self._cw, trinfo['wf_info_for'],
   281         rcache = trinfo.cw_attr_cache
   282                       trinfo['from_state'], trinfo['to_state'])
   282         _change_state(self._cw, rcache['wf_info_for'], rcache['from_state'],
   283         forentity = self._cw.entity_from_eid(trinfo['wf_info_for'])
   283                       rcache['to_state'])
       
   284         forentity = self._cw.entity_from_eid(rcache['wf_info_for'])
   284         iworkflowable = forentity.cw_adapt_to('IWorkflowable')
   285         iworkflowable = forentity.cw_adapt_to('IWorkflowable')
   285         assert iworkflowable.current_state.eid == trinfo['to_state']
   286         assert iworkflowable.current_state.eid == rcache['to_state']
   286         if iworkflowable.main_workflow.eid != iworkflowable.current_workflow.eid:
   287         if iworkflowable.main_workflow.eid != iworkflowable.current_workflow.eid:
   287             _SubWorkflowExitOp(self._cw, forentity=forentity, trinfo=trinfo)
   288             _SubWorkflowExitOp(self._cw, forentity=forentity, trinfo=trinfo)
   288 
   289 
   289 
   290 
   290 class CheckInStateChangeAllowed(WorkflowHook):
   291 class CheckInStateChangeAllowed(WorkflowHook):