def handler_trigger_transition(sender, **kwargs): action = kwargs['instance'] Document = apps.get_model(app_label='documents', model_name='Document') WorkflowInstance = apps.get_model(app_label='document_states', model_name='WorkflowInstance') WorkflowTransition = apps.get_model(app_label='document_states', model_name='WorkflowTransition') trigger_transitions = WorkflowTransition.objects.filter( trigger_events__event_type__name=kwargs['instance'].verb) if isinstance(action.target, Document): workflow_instances = WorkflowInstance.objects.filter( workflow__transitions__in=trigger_transitions, document=action.target).distinct() elif isinstance(action.action_object, Document): workflow_instances = WorkflowInstance.objects.filter( workflow__transitions__in=trigger_transitions, document=action.action_object).distinct() else: workflow_instances = WorkflowInstance.objects.none() for workflow_instance in workflow_instances: # Select the first transition that is valid for this workflow state valid_transitions = list( set(trigger_transitions) & set(workflow_instance.get_transition_choices())) if valid_transitions: workflow_instance.do_transition( comment=_('Event trigger: %s') % EventType.get(name=action.verb).label, transition=valid_transitions[0])
def test_workflow_state_action_event_trigger(self): # actions 1 and 2 both trigger the transition event, to make this # test case independent of the order of execution of actions 1 and 2 state_1_action_data = json.dumps( obj={ 'document_label': TEST_DOCUMENT_EDIT_WORKFLOW_ACTION_TEXT_LABEL }) self.test_workflow_state_1.actions.create( label=TEST_WORKFLOW_STATE_ACTION_LABEL, action_path=TEST_DOCUMENT_EDIT_WORKFLOW_ACTION_DOTTED_PATH, action_data=state_1_action_data) self.test_workflow_state_1.actions.create( label=TEST_WORKFLOW_STATE_ACTION_LABEL_2, action_path=TEST_DOCUMENT_EDIT_WORKFLOW_ACTION_DOTTED_PATH, action_data=state_1_action_data) state_2_action_data = json.dumps( obj={ 'document_description': TEST_DOCUMENT_EDIT_WORKFLOW_ACTION_TEXT_DESCRIPTION }) self.test_workflow_state_2.actions.create( label=TEST_WORKFLOW_STATE_ACTION_LABEL, action_path=TEST_DOCUMENT_EDIT_WORKFLOW_ACTION_DOTTED_PATH, action_data=state_2_action_data) EventType.refresh() self.test_workflow_transition.trigger_events.create( event_type=event_document_properties_edit.get_stored_event_type()) self.test_workflow.launch_for(document=self.test_document) self.assertEqual(self.test_document.label, TEST_DOCUMENT_EDIT_WORKFLOW_ACTION_TEXT_LABEL) self.assertEqual(self.test_document.description, TEST_DOCUMENT_EDIT_WORKFLOW_ACTION_TEXT_DESCRIPTION)
def get_initial(self): obj = self.get_object() initial = [] # Return the queryset by name from the sorted list of the class event_type_ids = [event_type.id for event_type in EventType.all()] event_type_queryset = StoredEventType.objects.filter( name__in=event_type_ids) # Sort queryset in Python by namespace, then by label event_type_queryset = sorted(event_type_queryset, key=lambda x: (x.namespace, x.label)) for event_type in event_type_queryset: initial.append({ 'transition': obj, 'event_type': event_type, }) return initial
def dispatch(self, *args, **kwargs): EventType.refresh() return super(WorkflowTemplateTransitionTriggerEventListView, self).dispatch(*args, **kwargs)