From 2d06f9d2b6ca3cd9ae883dee3ff00ac1da17f651 Mon Sep 17 00:00:00 2001 From: David Vogt Date: Thu, 31 Oct 2019 13:19:10 +0100 Subject: [PATCH] refactor: move is_completed local function to method The `is_completed(task)` function within the `update()` method of the CompleteWorkItemSerializer was confusing to read, as it wasn't clear about it's purpose (telling the caller whether the process can continue if all siblings are completed). Moving to a method for future subclassing, for example in the `Skip` serializer coming up next. --- caluma/workflow/serializers.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/caluma/workflow/serializers.py b/caluma/workflow/serializers.py index 299f3a5dd..bc6ee3b95 100644 --- a/caluma/workflow/serializers.py +++ b/caluma/workflow/serializers.py @@ -298,31 +298,36 @@ def validate(self, data): data["closed_by_group"] = user.group return super().validate(data) + def _can_continue(self, instance, task): + # If a "multiple instance" task has running siblings, the task is not completed + if task.is_multiple_instance: + return not instance.case.work_items.filter( + task=task, status=models.WorkItem.STATUS_READY + ).exists() + return instance.case.work_items.filter( + task=task, + status__in=( + models.WorkItem.STATUS_COMPLETED, + models.WorkItem.STATUS_SKIPPED, + ), + ).exists() + @transaction.atomic def update(self, instance, validated_data): - def is_completed(task): - # If a "multiple instance" task has running siblings, the task is not completed - if task.is_multiple_instance: - return not instance.case.work_items.filter( - task=task, status=models.WorkItem.STATUS_READY - ).exists() - return instance.case.work_items.filter( - task=task, status=models.WorkItem.STATUS_COMPLETED - ).exists() instance = super().update(instance, validated_data) user = self.context["request"].user case = instance.case - if not is_completed(instance.task): + if not self._can_continue(instance, instance.task): return instance flow = models.Flow.objects.filter(task_flows__task=instance.task_id).first() flow_referenced_tasks = models.Task.objects.filter(task_flows__flow=flow) - all_complete = False not in [ - is_completed(task) for task in flow_referenced_tasks - ] + all_complete = all( + self._can_continue(instance, task) for task in flow_referenced_tasks + ) if flow and all_complete: jexl = FlowJexl()