From 7831e81e65c16727179fc5fc22aa2218615ed602 Mon Sep 17 00:00:00 2001 From: DonHaul Date: Tue, 20 Aug 2024 15:57:32 +0200 Subject: [PATCH] decisions: added to endpoint to resolve --- backoffice/backoffice/workflows/api/views.py | 31 +++++++++---------- .../backoffice/workflows/tests/test_views.py | 12 +++++-- workflows/tests/__init__.py | 0 workflows/tests/test_author_create_tasks.py | 20 ------------ 4 files changed, 24 insertions(+), 39 deletions(-) delete mode 100644 workflows/tests/__init__.py delete mode 100644 workflows/tests/test_author_create_tasks.py diff --git a/backoffice/backoffice/workflows/api/views.py b/backoffice/backoffice/workflows/api/views.py index 7da3f604c..297089440 100644 --- a/backoffice/backoffice/workflows/api/views.py +++ b/backoffice/backoffice/workflows/api/views.py @@ -43,6 +43,16 @@ logger = logging.getLogger(__name__) +def add_decision(workflow_id, user, action): + serializer_class = DecisionSerializer + data = {"workflow": workflow_id, "user": user, "action": action} + + serializer = serializer_class(data=data) + if serializer.is_valid(raise_exception=True): + serializer.save() + return Response(serializer.data, status=status.HTTP_201_CREATED) + + class WorkflowViewSet(viewsets.ModelViewSet): queryset = Workflow.objects.all() serializer_class = WorkflowSerializer @@ -103,25 +113,11 @@ def create(self, request, *args, **kwargs): class DecisionViewSet(viewsets.ModelViewSet): queryset = Decision.objects.all() - serializer_class = DecisionSerializer - - def get_queryset(self): - status = self.request.query_params.get("status") - if status: - return self.queryset.filter(status__status=status) - return self.queryset def create(self, request, *args, **kwargs): - data = { - "workflow": request.data["workflow_id"], - "user": request.user, - "action": request.data["action"], - } - - serializer = self.serializer_class(data=data) - if serializer.is_valid(raise_exception=True): - serializer.save() - return Response(serializer.data, status=status.HTTP_201_CREATED) + return add_decision( + request.data["workflow_id"], request.user, request.data["action"] + ) class AuthorWorkflowViewSet(viewsets.ViewSet): @@ -190,6 +186,7 @@ def resolve(self, request, pk=None): ResolutionDags[serializer.validated_data["value"]], pk, ) + add_decision(pk, request.user, serializer.validated_data["value"]) return airflow_utils.trigger_airflow_dag( ResolutionDags[serializer.validated_data["value"]].label, pk, extra_data diff --git a/backoffice/backoffice/workflows/tests/test_views.py b/backoffice/backoffice/workflows/tests/test_views.py index a3978eaa8..dd83a0908 100644 --- a/backoffice/backoffice/workflows/tests/test_views.py +++ b/backoffice/backoffice/workflows/tests/test_views.py @@ -324,7 +324,8 @@ def test_create_author(self): @pytest.mark.vcr() def test_accept_author(self): self.api_client.force_authenticate(user=self.curator) - data = {"create_ticket": True, "value": "accept"} + action = "accept" + data = {"create_ticket": True, "value": action} response = self.api_client.post( reverse("api:workflows-authors-resolve", kwargs={"pk": self.workflow.id}), @@ -333,6 +334,9 @@ def test_accept_author(self): ) self.assertEqual(response.status_code, 200) + self.assertEqual( + Decision.objects.filter(workflow=self.workflow.id)[0].action, action + ) airflow_utils.delete_workflow_dag( WORKFLOW_DAGS[WorkflowType.AUTHOR_CREATE].approve, self.workflow.id @@ -341,7 +345,8 @@ def test_accept_author(self): @pytest.mark.vcr() def test_reject_author(self): self.api_client.force_authenticate(user=self.curator) - data = {"create_ticket": True, "value": "reject"} + action = "reject" + data = {"create_ticket": True, "value": action} response = self.api_client.post( reverse("api:workflows-authors-resolve", kwargs={"pk": self.workflow.id}), @@ -350,6 +355,9 @@ def test_reject_author(self): ) self.assertEqual(response.status_code, 200) + self.assertEqual( + Decision.objects.filter(workflow=self.workflow.id)[0].action, action + ) airflow_utils.delete_workflow_dag( WORKFLOW_DAGS[WorkflowType.AUTHOR_CREATE].reject, self.workflow.id diff --git a/workflows/tests/__init__.py b/workflows/tests/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/workflows/tests/test_author_create_tasks.py b/workflows/tests/test_author_create_tasks.py deleted file mode 100644 index 048c16e05..000000000 --- a/workflows/tests/test_author_create_tasks.py +++ /dev/null @@ -1,20 +0,0 @@ -import pytest -from dags.author.author_create.shared_tasks import ( - create_decision_on_curation_choice, -) - - -class TestAuthorCreate: - context = { - "params": { - "workflow_id": "ecaa62db-1326-43cf-8885-da96c544af42", - "data": { - "value": "create", - }, - } - } - - @pytest.mark.vcr() - def test_create_decision_on_curation_choice(self): - result = create_decision_on_curation_choice.function(**self.context) - assert result.status_code == 201