Skip to content

Commit

Permalink
refactor: move is_completed local function to method
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
David Vogt committed Oct 31, 2019
1 parent d869b2d commit 2d06f9d
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions caluma/workflow/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 2d06f9d

Please sign in to comment.