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

refactor completion of tasks #1282

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 1 addition & 25 deletions app/apps/cases/signals.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import logging

from apps.cases.models import Case, CaseClose, CaseState, CitizenReport
from apps.workflow.models import CaseWorkflow
from apps.workflow.tasks import task_create_citizen_report_worflow_for_case
from apps.cases.models import Case, CaseState
from django.db.models.signals import post_save, pre_save
from django.dispatch import receiver

Expand Down Expand Up @@ -30,25 +28,3 @@ def start_workflow_for_case(sender, instance, created, **kwargs):
if created:
CaseState.objects.get_or_create(case=instance)
task_create_main_worflow_for_case.delay(case_id=instance.id, data=data)


@receiver(post_save, sender=CitizenReport, dispatch_uid="complete_citizen_report_task")
def complete_citizen_report_task(sender, instance, created, **kwargs):
if kwargs.get("raw"):
return
if instance.case_user_task_id != "-1" and created:
CaseWorkflow.complete_user_task(instance.case_user_task_id, {})
if created:
task_create_citizen_report_worflow_for_case.delay(instance.id)


@receiver(post_save, sender=CaseClose)
def close_case(sender, instance, created, **kwargs):
if kwargs.get("raw"):
return
if instance.case_user_task_id != "-1" and created:
CaseState.objects.get_or_create(
case=instance.case,
status=CaseState.CaseStateChoice.AFGESLOTEN,
)
instance.case.close_case()
20 changes: 20 additions & 0 deletions app/apps/cases/task_completion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import logging

from apps.workflow.models import CaseWorkflow
from apps.workflow.tasks import task_create_citizen_report_worflow_for_case
from django.db import transaction

logger = logging.getLogger(__name__)


def complete_citizen_report_task(serializer):
try:
with transaction.atomic():
citizen_report = serializer.create(serializer.validated_data)
CaseWorkflow.complete_user_task(
citizen_report.case_user_task_id, {}, wait=True, timeout=15
)
task_create_citizen_report_worflow_for_case.delay(citizen_report.id)
except Exception as e:
logger.error(f"Error completing task {citizen_report.case_user_task_id}: {e}")
raise e
19 changes: 0 additions & 19 deletions app/apps/cases/tests/tests_signals.py

This file was deleted.

4 changes: 3 additions & 1 deletion app/apps/cases/views/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,9 @@ def citizen_reports(self, request, pk):
context={"request": request},
)
if serializer.is_valid():
serializer.create(serializer.validated_data)
from apps.cases.task_completion import complete_citizen_report_task

complete_citizen_report_task(serializer)
return Response(
data="CitizenReport added",
status=status.HTTP_200_OK,
Expand Down
19 changes: 19 additions & 0 deletions app/apps/cases/views/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
CitizenReportAnonomizedSerializer,
)
from apps.users.permissions import CanCloseCase, rest_permission_classes_for_top
from django.db import transaction
from rest_framework import mixins, viewsets
from rest_framework.permissions import SAFE_METHODS
from rest_framework.response import Response

logger = logging.getLogger(__name__)

Expand All @@ -36,6 +38,23 @@ def get_permissions(self):
self.permission_classes.append(CanCloseCase)
return super(CaseCloseViewSet, self).get_permissions()

def create(self, request):
with transaction.atomic():
serializer = self.serializer_class(
data=request.data,
context={"request": request},
)
if serializer.is_valid():
case_close = serializer.create(serializer.validated_data)
CaseState.objects.get_or_create(
case=case_close.case,
status=CaseState.CaseStateChoice.AFGESLOTEN,
)
case_close.case.close_case()
return Response(serializer.data)
else:
return Response(serializer.errors, status=400)


class CaseCloseResultViewSet(viewsets.GenericViewSet, mixins.ListModelMixin):
serializer_class = CaseCloseResultSerializer
Expand Down
3 changes: 0 additions & 3 deletions app/apps/debriefings/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,3 @@

class DebriefingsConfig(AppConfig):
name = "apps.debriefings"

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

This file was deleted.

24 changes: 24 additions & 0 deletions app/apps/debriefings/task_completion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import logging

from apps.workflow.models import CaseWorkflow
from django.db import transaction

logger = logging.getLogger(__name__)


def complete_task_create_debrief(serializer):
with transaction.atomic():
debrief = serializer.create(serializer.validated_data)
try:
CaseWorkflow.complete_user_task(
debrief.case_user_task_id,
{
"violation": {
"value": debrief.violation,
}
},
wait=False,
)
except Exception as e:
logger.error(f"Error completing task {debrief.case_user_task_id}: {e}")
raise e
18 changes: 18 additions & 0 deletions app/apps/debriefings/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
DebriefingSerializer,
)
from apps.users.permissions import CanPerformTask, rest_permission_classes_for_top
from rest_framework import status
from rest_framework.mixins import CreateModelMixin, ListModelMixin
from rest_framework.permissions import SAFE_METHODS
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet

from .task_completion import complete_task_create_debrief

logger = logging.getLogger(__name__)


Expand All @@ -27,3 +31,17 @@ def get_permissions(self):
if self.request.method not in SAFE_METHODS:
self.permission_classes.append(CanPerformTask)
return super(DebriefingViewSet, self).get_permissions()

def create(self, request):
serializer = DebriefingCreateSerializer(
data=request.data,
context={"request": request},
)
if serializer.is_valid():
complete_task_create_debrief(serializer)
return Response(
data="Debrief added",
status=status.HTTP_201_CREATED,
)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
3 changes: 0 additions & 3 deletions app/apps/decisions/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,3 @@

class DecisionsConfig(AppConfig):
name = "apps.decisions"

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

This file was deleted.

34 changes: 34 additions & 0 deletions app/apps/decisions/task_completion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import logging

from apps.summons.models import Summon
from apps.workflow.models import CaseWorkflow
from django.db import transaction

logger = logging.getLogger(__name__)


def update_decision_with_summon(serializer):
with transaction.atomic():
try:
decision = serializer.create(serializer.validated_data)
task = CaseWorkflow.get_task_by_task_id(decision.case_user_task_id)
summon = Summon.objects.filter(
id=task.workflow.get_data().get("summon_id", {}).get("value", 0)
).first()

data = {
"type_besluit": {"value": decision.decision_type.workflow_option},
}
if summon:
decision.summon = summon
decision.save()
names = ", ".join([person.__str__() for person in summon.persons.all()])
data.update(
{
"names": {"value": f"{names}: {decision.decision_type.name}"},
}
)
CaseWorkflow.complete_user_task(task.id, data, wait=False)
except Exception as e:
logger.error(f"Error while completing task for decision: {e}")
raise e
19 changes: 19 additions & 0 deletions app/apps/decisions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet

from .task_completion import update_decision_with_summon

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -59,6 +61,23 @@ def get_desicions_with_sanction(self, request):
)
return Response(serializer.data)

def create(self, request, *args, **kwargs):
serializer = self.serializer_class(
data=request.data,
context={"request": request},
)
if serializer.is_valid():
update_decision_with_summon(serializer)
return Response(
data="Decision added",
status=status.HTTP_200_OK,
)
else:
return Response(
data=serializer.errors,
status=status.HTTP_400_BAD_REQUEST,
)


class DecisionTypeViewSet(GenericViewSet, ListModelMixin):
serializer_class = DecisionTypeSerializer
Expand Down
Loading
Loading