Make sure the on entry action of the initial state of workflows executes on document creation.

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
Roberto Rosario
2018-08-14 04:50:44 -04:00
parent 5cd7ac88d6
commit b3ef018d6c
4 changed files with 26 additions and 10 deletions
+1 -1
View File
@@ -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)
+21 -3
View File
@@ -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