438 except KeyError: |
438 except KeyError: |
439 msg = session._('mandatory relation') |
439 msg = session._('mandatory relation') |
440 raise ValidationError(entity.eid, {'wf_info_for': msg}) |
440 raise ValidationError(entity.eid, {'wf_info_for': msg}) |
441 forentity = session.entity_from_eid(foreid) |
441 forentity = session.entity_from_eid(foreid) |
442 # then check it has a workflow set |
442 # then check it has a workflow set |
443 wf = forentity.current_workflow |
443 if session.transaction_data.get((forentity.eid, 'customwf')): |
|
444 wfeid = session.transaction_data[(forentity.eid, 'customwf')] |
|
445 wf = session.entity_from_eid(wfeid) |
|
446 else: |
|
447 wf = forentity.current_workflow |
444 if wf is None: |
448 if wf is None: |
445 msg = session._('related entity has no workflow set') |
449 msg = session._('related entity has no workflow set') |
446 raise ValidationError(entity.eid, {None: msg}) |
450 raise ValidationError(entity.eid, {None: msg}) |
447 # then check it has a state set |
451 # then check it has a state set |
448 fromstate = forentity.current_state |
452 fromstate = forentity.current_state |
501 entity = self.entity |
505 entity = self.entity |
502 # if there is an initial state and the entity's state is not set, |
506 # if there is an initial state and the entity's state is not set, |
503 # use the initial state as a default state |
507 # use the initial state as a default state |
504 pendingeids = session.transaction_data.get('pendingeids', ()) |
508 pendingeids = session.transaction_data.get('pendingeids', ()) |
505 if not entity.eid in pendingeids and not entity.in_state and \ |
509 if not entity.eid in pendingeids and not entity.in_state and \ |
506 entity.current_workflow: |
510 entity.main_workflow: |
507 state = entity.current_workflow.initial |
511 state = entity.main_workflow.initial |
508 if state: |
512 if state: |
509 # use super session to by-pass security checks |
513 # use super session to by-pass security checks |
510 session.super_session.add_relation(entity.eid, 'in_state', |
514 session.super_session.add_relation(entity.eid, 'in_state', |
511 state.eid) |
515 state.eid) |
512 |
516 |
522 session = self.session |
526 session = self.session |
523 pendingeids = session.transaction_data.get('pendingeids', ()) |
527 pendingeids = session.transaction_data.get('pendingeids', ()) |
524 if self.eid in pendingeids: |
528 if self.eid in pendingeids: |
525 return |
529 return |
526 entity = session.entity_from_eid(self.eid) |
530 entity = session.entity_from_eid(self.eid) |
527 # notice that enforcment that new workflow apply to the entity's type is |
531 # check custom workflow has not been rechanged to another one in the same |
528 # done by schema rule, no need to check it here |
532 # transaction |
529 if entity.current_workflow.eid == self.wfeid: |
533 mainwf = entity.main_workflow |
530 deststate = entity.current_workflow.initial |
534 if mainwf.eid == self.wfeid: |
|
535 deststate = mainwf.initial |
531 if not deststate: |
536 if not deststate: |
532 msg = session._('workflow has no initial state') |
537 msg = session._('workflow has no initial state') |
533 raise ValidationError(entity.eid, {'custom_workflow': msg}) |
538 raise ValidationError(entity.eid, {'custom_workflow': msg}) |
534 if entity.current_workflow.state_by_eid(entity.current_state.eid): |
539 if mainwf.state_by_eid(entity.current_state.eid): |
535 # nothing to do |
540 # nothing to do |
536 return |
541 return |
537 # if there are no history, simply go to new workflow's initial state |
542 # if there are no history, simply go to new workflow's initial state |
538 if not entity.workflow_history: |
543 if not entity.workflow_history: |
539 if entity.current_state.eid != deststate.eid: |
544 if entity.current_state.eid != deststate.eid: |
540 _change_state(session, entity.eid, |
545 _change_state(session, entity.eid, |
541 entity.current_state.eid, deststate.eid) |
546 entity.current_state.eid, deststate.eid) |
542 return |
547 return |
543 msg = session._('workflow changed to "%s"') |
548 msg = session._('workflow changed to "%s"') |
544 msg %= entity.current_workflow.name |
549 msg %= session._(mainwf.name) |
545 entity.change_state(deststate.name, msg) |
550 session.transaction_data[(entity.eid, 'customwf')] = self.wfeid |
|
551 entity.change_state(deststate, msg) |
546 |
552 |
547 |
553 |
548 def set_custom_workflow(session, eidfrom, rtype, eidto): |
554 def set_custom_workflow(session, eidfrom, rtype, eidto): |
549 WorkflowChangedOp(session, eid=eidfrom, wfeid=eidto) |
555 WorkflowChangedOp(session, eid=eidfrom, wfeid=eidto) |
550 |
556 |