diff --git a/HISTORY.rst b/HISTORY.rst index 2e325badbe..033f27a10b 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -33,6 +33,8 @@ wiki.mayan-edms.com - Unify template title rendering. - Add support for template subtitles. +- Make sure the on entry action of the initial state of workflows + executes on document creation. 3.0.1 (2018-07-08) ================= diff --git a/mayan/apps/document_states/managers.py b/mayan/apps/document_states/managers.py index f72aba5968..05ea9874b7 100644 --- a/mayan/apps/document_states/managers.py +++ b/mayan/apps/document_states/managers.py @@ -4,4 +4,4 @@ from django.db import models class WorkflowManager(models.Manager): def launch_for(self, document): for workflow in document.document_type.workflows.all(): - workflow.launch_for(document) + workflow.launch_for(document=document) diff --git a/mayan/apps/document_states/models.py b/mayan/apps/document_states/models.py index 5fbbaf6049..a2c0382241 100644 --- a/mayan/apps/document_states/models.py +++ b/mayan/apps/document_states/models.py @@ -76,7 +76,12 @@ class Workflow(models.Model): logger.info( 'Launching workflow %s for document %s', self, document ) - self.instances.create(document=document) + workflow_instance = self.instances.create(document=document) + initial_state = self.get_initial_state() + + if initial_state: + for action in initial_state.entry_actions.filter(enabled=True): + action.execute(context=workflow_instance.get_context()) except IntegrityError: logger.info( 'Workflow %s already launched for document %s', self, document @@ -352,6 +357,12 @@ class WorkflowInstance(models.Model): 'document_states:workflow_instance_detail', args=(str(self.pk),) ) + def get_context(self): + return { + 'document': self.document, 'workflow': self.workflow, + 'workflow_instance': self, + } + def get_current_state(self): """ Actual State - The current state of the workflow. If there are @@ -455,12 +466,19 @@ class WorkflowInstanceLogEntry(models.Model): def save(self, *args, **kwargs): result = super(WorkflowInstanceLogEntry, self).save(*args, **kwargs) + context = self.workflow_instance.get_context() + context.update( + { + 'action': action, + 'entry_log': self + } + ) for action in self.transition.origin_state.exit_actions.filter(enabled=True): - action.execute(context={'action': action, 'entry_log': self}) + action.execute(context=context) for action in self.transition.destination_state.entry_actions.filter(enabled=True): - action.execute(context={'action': action, 'entry_log': self}) + action.execute(context=context) return result diff --git a/mayan/apps/tags/workflow_actions.py b/mayan/apps/tags/workflow_actions.py index 9d2cfb902f..8afad617ba 100644 --- a/mayan/apps/tags/workflow_actions.py +++ b/mayan/apps/tags/workflow_actions.py @@ -55,9 +55,7 @@ class AttachTagAction(WorkflowAction): def execute(self, context): for tag in self.get_tags(): - tag.attach_to( - document=context['entry_log'].workflow_instance.document - ) + tag.attach_to(document=context['document']) class RemoveTagAction(AttachTagAction): @@ -75,6 +73,4 @@ class RemoveTagAction(AttachTagAction): def execute(self, context): for tag in self.get_tags(): - tag.remove_from( - document=context['entry_log'].workflow_instance.document - ) + tag.remove_from(document=context['document'])