Proof of concept of the workflow instance context
Add support for workflow instance JSON context. Add support for two step workflow transition. Add support for dynamic form creation for transition execution. Signed-off-by: Roberto Rosario <roberto.rosario@mayan-edms.com>
This commit is contained in:
@@ -257,8 +257,8 @@ class WorkflowState(models.Model):
|
||||
def save(self, *args, **kwargs):
|
||||
# Solve issue #557 "Break workflows with invalid input"
|
||||
# without using a migration.
|
||||
# Remove blank=True, remove this, and create a migration in the next
|
||||
# minor version.
|
||||
# TODO: Remove blank=True, remove this, and create a migration in the
|
||||
# next minor version.
|
||||
|
||||
try:
|
||||
self.completion = int(self.completion)
|
||||
@@ -363,6 +363,44 @@ class WorkflowTransition(models.Model):
|
||||
return self.label
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class WorkflowTransitionField(models.Model):
|
||||
transition = models.ForeignKey(
|
||||
on_delete=models.CASCADE, related_name='fields',
|
||||
to=WorkflowTransition, verbose_name=_('Transition')
|
||||
)
|
||||
name = models.CharField(
|
||||
help_text=_(
|
||||
'The name that will be used to identify this field in other parts '
|
||||
'of the workflow system.'
|
||||
), max_length=128, verbose_name=_('Internal name')
|
||||
)
|
||||
label = models.CharField(
|
||||
help_text=_(
|
||||
'The field name that will be shown on the user interface.'
|
||||
), max_length=128, verbose_name=_('Label'))
|
||||
help_text = models.TextField(
|
||||
blank=True, help_text=_(
|
||||
'An optional message that will help users better understand the '
|
||||
'purpose of the field and data to provide.'
|
||||
), verbose_name=_('Help text')
|
||||
)
|
||||
required = models.BooleanField(
|
||||
default=False, help_text=_(
|
||||
'Whether this fields needs to be filled out or not to proceed.'
|
||||
), verbose_name=_('Required')
|
||||
)
|
||||
#TODO: widget, widget kwargs
|
||||
|
||||
class Meta:
|
||||
unique_together = ('transition', 'name')
|
||||
verbose_name = _('Workflow transition trigger event')
|
||||
verbose_name_plural = _('Workflow transitions trigger events')
|
||||
|
||||
def __str__(self):
|
||||
return self.label
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class WorkflowTransitionTriggerEvent(models.Model):
|
||||
transition = models.ForeignKey(
|
||||
@@ -392,6 +430,9 @@ class WorkflowInstance(models.Model):
|
||||
on_delete=models.CASCADE, related_name='workflows', to=Document,
|
||||
verbose_name=_('Document')
|
||||
)
|
||||
context = models.TextField(
|
||||
blank=True, verbose_name=_('Backend data')
|
||||
)
|
||||
|
||||
class Meta:
|
||||
ordering = ('workflow',)
|
||||
@@ -402,15 +443,30 @@ class WorkflowInstance(models.Model):
|
||||
def __str__(self):
|
||||
return force_text(self.workflow)
|
||||
|
||||
def do_transition(self, transition, user=None, comment=None):
|
||||
try:
|
||||
if transition in self.get_current_state().origin_transitions.all():
|
||||
self.log_entries.create(
|
||||
comment=comment or '', transition=transition, user=user
|
||||
)
|
||||
except AttributeError:
|
||||
# No initial state has been set for this workflow
|
||||
pass
|
||||
def do_transition(self, transition, extra_data=None, user=None, comment=None):
|
||||
with transaction.atomic():
|
||||
try:
|
||||
if transition in self.get_current_state().origin_transitions.all():
|
||||
self.log_entries.create(
|
||||
comment=comment or '', transition=transition, user=user
|
||||
)
|
||||
if extra_data:
|
||||
data = self.loads()
|
||||
data.update(extra_data)
|
||||
self.dumps(data=data)
|
||||
except AttributeError:
|
||||
# No initial state has been set for this workflow
|
||||
pass
|
||||
|
||||
# TODO: execute transition event target = document,
|
||||
# action_object = self
|
||||
|
||||
def dumps(self, data):
|
||||
"""
|
||||
Serialize the context data.
|
||||
"""
|
||||
self.context = json.dumps(data)
|
||||
self.save()
|
||||
|
||||
def get_absolute_url(self):
|
||||
return reverse(
|
||||
@@ -420,10 +476,12 @@ class WorkflowInstance(models.Model):
|
||||
)
|
||||
|
||||
def get_context(self):
|
||||
return {
|
||||
context = {
|
||||
'document': self.document, 'workflow': self.workflow,
|
||||
'workflow_instance': self,
|
||||
}
|
||||
context['workflow_instance_context'] = self.loads()
|
||||
return context
|
||||
|
||||
def get_current_state(self):
|
||||
"""
|
||||
@@ -489,6 +547,12 @@ class WorkflowInstance(models.Model):
|
||||
"""
|
||||
return WorkflowTransition.objects.none()
|
||||
|
||||
def loads(self):
|
||||
"""
|
||||
Deserialize the context data.
|
||||
"""
|
||||
return json.loads(self.context or '{}')
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class WorkflowInstanceLogEntry(models.Model):
|
||||
@@ -529,31 +593,32 @@ class WorkflowInstanceLogEntry(models.Model):
|
||||
raise ValidationError(_('Not a valid transition choice.'))
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
result = super(WorkflowInstanceLogEntry, self).save(*args, **kwargs)
|
||||
context = self.workflow_instance.get_context()
|
||||
context.update(
|
||||
{
|
||||
'entry_log': self
|
||||
}
|
||||
)
|
||||
|
||||
for action in self.transition.origin_state.exit_actions.filter(enabled=True):
|
||||
with transaction.atomic():
|
||||
result = super(WorkflowInstanceLogEntry, self).save(*args, **kwargs)
|
||||
context = self.workflow_instance.get_context()
|
||||
context.update(
|
||||
{
|
||||
'action': action,
|
||||
'entry_log': self
|
||||
}
|
||||
)
|
||||
action.execute(context=context)
|
||||
|
||||
for action in self.transition.destination_state.entry_actions.filter(enabled=True):
|
||||
context.update(
|
||||
{
|
||||
'action': action,
|
||||
}
|
||||
)
|
||||
action.execute(context=context)
|
||||
for action in self.transition.origin_state.exit_actions.filter(enabled=True):
|
||||
context.update(
|
||||
{
|
||||
'action': action,
|
||||
}
|
||||
)
|
||||
action.execute(context=context)
|
||||
|
||||
return result
|
||||
for action in self.transition.destination_state.entry_actions.filter(enabled=True):
|
||||
context.update(
|
||||
{
|
||||
'action': action,
|
||||
}
|
||||
)
|
||||
action.execute(context=context)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
class WorkflowRuntimeProxy(Workflow):
|
||||
|
||||
Reference in New Issue
Block a user