From d394bdf07446d649c5cce765689cb6ae4274a05d Mon Sep 17 00:00:00 2001 From: DonHaul Date: Fri, 23 Aug 2024 14:47:16 +0200 Subject: [PATCH] #draft backoffice: added decision connecion in serializer * ref: cern-sis/issues-inspire/issues/518 --- .../backoffice/workflows/api/serializers.py | 13 ++++---- .../0010_alter_decision_workflow_and_more.py | 31 +++++++++++++++++++ .../backoffice/backoffice/workflows/models.py | 9 ++++-- .../backoffice/workflows/tests/test_views.py | 10 ++++++ 4 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/migrations/0010_alter_decision_workflow_and_more.py diff --git a/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/api/serializers.py b/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/api/serializers.py index 4883d0b20..bf7bf9f57 100644 --- a/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/api/serializers.py +++ b/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/api/serializers.py @@ -24,19 +24,20 @@ def get_ticket_url(self, obj): ) -class WorkflowSerializer(serializers.ModelSerializer): - tickets = WorkflowTicketSerializer(many=True, read_only=True) +class DecisionSerializer(serializers.ModelSerializer): + workflow = serializers.PrimaryKeyRelatedField(queryset=Workflow.objects.all()) class Meta: - model = Workflow + model = Decision fields = "__all__" -class DecisionSerializer(serializers.ModelSerializer): - workflow = serializers.PrimaryKeyRelatedField(queryset=Workflow.objects.all()) +class WorkflowSerializer(serializers.ModelSerializer): + tickets = WorkflowTicketSerializer(many=True, read_only=True) + decisions = DecisionSerializer(many=True, read_only=True) class Meta: - model = Decision + model = Workflow fields = "__all__" diff --git a/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/migrations/0010_alter_decision_workflow_and_more.py b/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/migrations/0010_alter_decision_workflow_and_more.py new file mode 100644 index 000000000..a9cefea0e --- /dev/null +++ b/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/migrations/0010_alter_decision_workflow_and_more.py @@ -0,0 +1,31 @@ +# Generated by Django 4.2.6 on 2024-08-23 13:02 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("workflows", "0009_decision"), + ] + + operations = [ + migrations.AlterField( + model_name="decision", + name="workflow", + field=models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="decisions", + to="workflows.workflow", + ), + ), + migrations.AlterField( + model_name="workflowticket", + name="workflow_id", + field=models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="tickets", + to="workflows.workflow", + ), + ), + ] diff --git a/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/models.py b/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/models.py index 590c831db..d4fb37049 100644 --- a/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/models.py +++ b/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/models.py @@ -49,9 +49,14 @@ class WorkflowTicket(models.Model): class Decision(models.Model): user = models.ForeignKey( - User, to_field="email", db_column="email", on_delete=models.CASCADE + User, + to_field="email", + db_column="email", + on_delete=models.CASCADE, + ) + workflow = models.ForeignKey( + Workflow, related_name="decisions", on_delete=models.CASCADE ) - workflow = models.ForeignKey(Workflow, on_delete=models.CASCADE) action = models.CharField(max_length=30, choices=DECISION_CHOICES) _created_at = models.DateTimeField(auto_now_add=True) diff --git a/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/tests/test_views.py b/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/tests/test_views.py index a0ac4aab9..d9395e95d 100644 --- a/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/tests/test_views.py +++ b/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/tests/test_views.py @@ -22,6 +22,7 @@ from backoffice.workflows.constants import ( WORKFLOW_DAGS, AuthorCreateDags, + ResolutionDags, StatusChoices, WorkflowType, ) @@ -100,6 +101,15 @@ def test_tickets(self): assert "ticket_id" in workflow_data["tickets"][0] assert "ticket_type" in workflow_data["tickets"][0] + def test_decisions(self): + Decision.objects.create( + workflow=self.workflow, user=self.user, action=ResolutionDags.accept + ) + workflow_data = WorkflowSerializer(self.workflow).data + assert "decisions" in workflow_data + assert "action" in workflow_data["decisions"][0] + assert "user" in workflow_data["decisions"][0] + @pytest.mark.vcr() def test_delete(self): self.api_client.force_authenticate(user=self.curator)