diff --git a/backoffice/backoffice/workflows/api/serializers.py b/backoffice/backoffice/workflows/api/serializers.py index 4883d0b2..bf7bf9f5 100644 --- a/backoffice/backoffice/workflows/api/serializers.py +++ b/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/backoffice/workflows/models.py b/backoffice/backoffice/workflows/models.py index 590c831d..bd0be800 100644 --- a/backoffice/backoffice/workflows/models.py +++ b/backoffice/backoffice/workflows/models.py @@ -36,7 +36,7 @@ class Workflow(models.Model): class WorkflowTicket(models.Model): - workflow_id = models.ForeignKey( + workflow = models.ForeignKey( Workflow, related_name="tickets", on_delete=models.CASCADE ) ticket_id = models.CharField( @@ -49,7 +49,11 @@ 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", + related_name="decisions", + db_column="email", + on_delete=models.CASCADE, ) workflow = models.ForeignKey(Workflow, on_delete=models.CASCADE) action = models.CharField(max_length=30, choices=DECISION_CHOICES) diff --git a/backoffice/backoffice/workflows/tests/test_views.py b/backoffice/backoffice/workflows/tests/test_views.py index a0ac4aab..e61a7747 100644 --- a/backoffice/backoffice/workflows/tests/test_views.py +++ b/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 "email" in workflow_data["decisions"][0] + @pytest.mark.vcr() def test_delete(self): self.api_client.force_authenticate(user=self.curator)