3

I have a simple linear workflow where a single task will have a list of approvers that it must pass through (linearly, each in turn) for confirmation before being completed.

I have designed this to use the User model for approver, with 2 additional models:

  • A Task model to govern details of individual tasks in a workflow; and
  • a TaskStep model to explicitly manage the relationship between the Tasks and Approver (rather than relying on Django to autopopulate).
    class Task(models.Model):
        title =  models.CharField(max_length=30)
        approvers = models.ManyToManyField(
            get_user_model(),
            related_name='approvers_taskstep_set',
            through='TaskStep'
        )

    class TaskStep(models.Model):
        approver = models.ForeignKey(
            get_user_model(), null=True, on_delete=models.SET_NULL
        )
        task = models.ForeignKey(Task, null=True, on_delete=models.SET_NULL)

I would like to use django-fsm to create a finite state machine to track the status of each task in the workflow.

I know I can do this easily if I pre-define the number of approvers in each workflow. E.g. if it were 3 I could simply put this in an integerfield and then create associated functions each approver would call:

// in Task(models.Model):
STATUS_APPROVER_1 = 0
STATUS_APPROVER_2 = 1
STATUS_APPROVER_3 = 2
STATUS_CHOICES = (
    (STATUS_APPROVER_1 , 'With approver 1'),
    (STATUS_APPROVER_2 , 'With approver 2'),
    (STATUS_APPROVER_3 , 'With approver 3'),
)

status = FSMIntegerField(
    choices=STATUS_CHOICES, default=STATUS_APPROVER_1, protected=True
)
...
@transition(field=status, source='STATUS_APPROVER_1', target=STATUS_APPROVER_2)
def advance_approver_1(self):
...

However, say that I wanted to allow users to define the number of approvers (and therefore the number of steps) themselves. How could I go about this? I would till keep this a linear progression, not parallel.

Is there a way to do this with django-fsm or am I looking at this wrong? I thought perhaps trying to dynamically create the STATUS_CHOICES proxy by looping though the number/length of approvers but I am not sure how to start.

Perhaps a lamba function in the source and target of the FSMIntegerField?

alias51
  • 8,178
  • 22
  • 94
  • 166

0 Answers0