Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove signals #35

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions app/apps/cases/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,3 @@
class CasesConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "apps.cases"

def ready(self):
import apps.cases.signals # noqa
14 changes: 0 additions & 14 deletions app/apps/cases/signals.py

This file was deleted.

6 changes: 5 additions & 1 deletion app/apps/cases/tests/tests_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from unittest.mock import patch
from django.core import management
from django.urls import reverse
from rest_framework import status
Expand Down Expand Up @@ -126,7 +127,10 @@ def _create_sample_document(self):
response = self.client.post(url, data=document_data, format="multipart")
return response.data

def _create_case(self):
# Django test create a test db, celery is unaware of that db so mock celery methods
@patch("apps.cases.views.CaseViewSet.start_workflow")
def _create_case(self, mock_start_workflow):
mock_start_workflow.return_value = "task_start_worflow: completed"
url = reverse("cases-list")
data = {"description": "Test case description"}

Expand Down
16 changes: 14 additions & 2 deletions app/apps/cases/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from django.shortcuts import get_object_or_404
from django.core.files.storage import default_storage
from django.http import FileResponse
from apps.workflow.tasks import task_create_main_worflow_for_case
from apps.workflow.tasks import task_start_worflow


class CaseViewSet(
Expand Down Expand Up @@ -59,8 +61,17 @@ def create(self, request):
contacts_data = validated_data.pop("contacts", [])
case = Case.objects.create(**validated_data)
Contact.process_contacts(case, contacts_data)
self.start_workflow(case)
return Response(CaseSerializer(case).data, status=201)

def start_workflow(self, case):
task = task_create_main_worflow_for_case.delay(case_id=case.id)
task.wait(timeout=None, interval=0.5)
start_workflow_task = task_start_worflow.delay(
CaseWorkflow.objects.get(case=case).id
)
start_workflow_task.wait(timeout=None, interval=0.5)

@action(
detail=False, methods=["post"], url_path="documents", name="cases-documents"
)
Expand Down Expand Up @@ -120,12 +131,13 @@ def start_process(self, request, pk):
instance = data["workflow_option_id"]

workflow_type = "sub_workflow"
CaseWorkflow.objects.create(
case_workflow = CaseWorkflow.objects.create(
case=case,
workflow_type=workflow_type,
workflow_message_name=instance.message_name,
)

task = task_start_worflow.delay(case_workflow.id)
task.wait(timeout=None, interval=0.5)
return Response(
data=f"Workflow has started {str(instance)}",
status=status.HTTP_200_OK,
Expand Down
3 changes: 2 additions & 1 deletion app/apps/workflow/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
task_start_subworkflow,
)
from .utils import get_initial_data_from_config
from django.utils.timezone import make_aware


class CaseWorkflow(models.Model):
Expand Down Expand Up @@ -193,7 +194,7 @@ def _create_user_tasks(self, wf):
name=task.task_spec.bpmn_name,
roles=[r.strip() for r in task.task_spec.lane.split(",")],
form=self._parse_task_spec_form(task.task_spec.form),
due_date=datetime.datetime.today(),
due_date=make_aware(datetime.datetime.today()),
case=self.case,
workflow=self,
)
Expand Down
32 changes: 2 additions & 30 deletions app/apps/workflow/signals.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
import copy

from apps.workflow.models import CaseUserTask, CaseWorkflow, GenericCompletedTask
from apps.workflow.tasks import task_start_worflow
from django.db.models.signals import post_save, pre_save
from apps.workflow.models import CaseWorkflow
from django.db.models.signals import pre_save
from django.dispatch import receiver
from .utils import get_latest_version_from_config
from .user_tasks import get_task_by_name


@receiver(
post_save,
sender=GenericCompletedTask,
dispatch_uid="complete_generic_user_task_and_create_new_user_tasks",
)
def complete_generic_user_task_and_create_new_user_tasks(
sender, instance, created, **kwargs
):
task = CaseUserTask.objects.filter(id=instance.case_user_task_id).first()
if created and task:
data = copy.deepcopy(instance.variables)
data.pop("mapped_form_data")
user_task_type = get_task_by_name(task.task_name)
user_task_instance = user_task_type(task)
data.update(user_task_instance.get_data())
CaseWorkflow.complete_user_task(task.id, data, wait=True)


@receiver(pre_save, sender=CaseWorkflow, dispatch_uid="case_workflow_pre_save")
Expand All @@ -43,9 +21,3 @@ def case_workflow_pre_save(sender, instance, **kwargs):
instance.workflow_version = get_latest_version_from_config(
instance.workflow_type
)


@receiver(post_save, sender=CaseWorkflow, dispatch_uid="start_workflow")
def start_workflow(sender, instance, created, **kwargs):
if created:
task_start_worflow.delay(instance.id)
14 changes: 14 additions & 0 deletions app/apps/workflow/task_completion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import copy
from apps.workflow.models import CaseUserTask, CaseWorkflow
from .user_tasks import get_task_by_name


def complete_generic_user_task_and_create_new_user_tasks(case_user_task):
task = CaseUserTask.objects.filter(id=case_user_task.case_user_task_id).first()
if task:
data = copy.deepcopy(case_user_task.variables)
data.pop("mapped_form_data")
user_task_type = get_task_by_name(task.task_name)
user_task_instance = user_task_type(task)
data.update(user_task_instance.get_data())
CaseWorkflow.complete_user_task(task.id, data, wait=True)
1 change: 0 additions & 1 deletion app/apps/workflow/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ def task_create_main_worflow_for_case(self, case_id, data={}):
workflow_message_name=None,
data=data,
)

return f"task_start_main_worflow_for_case: workflow id '{workflow_instance.id}', for case with id '{case_id}', created"


Expand Down
7 changes: 6 additions & 1 deletion app/apps/workflow/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import logging

from apps.workflow.task_completion import (
complete_generic_user_task_and_create_new_user_tasks,
)
from apps.workflow.utils import (
get_bpmn_models,
get_bpmn_file,
Expand Down Expand Up @@ -78,7 +82,8 @@ def complete_task(self, request):
)

try:
GenericCompletedTask.objects.create(**data)
case_user_task = GenericCompletedTask.objects.create(**data)
complete_generic_user_task_and_create_new_user_tasks(case_user_task)
return HttpResponse(
f"CaseUserTask {data['case_user_task_id']} has been completed"
)
Expand Down
Loading