From 17f0cc1dba7843ad15bf9f640b5d565dccea570b Mon Sep 17 00:00:00 2001 From: DonHaul Date: Fri, 16 Aug 2024 11:10:43 +0200 Subject: [PATCH] workflows: display tickets with its url when fetching workflow * ref: cern-sis/issues-inspire#519 --- backoffice/.envs/local/.django | 2 ++ .../backoffice/workflows/api/serializers.py | 20 +++++++++++++++---- backoffice/backoffice/workflows/models.py | 4 +++- .../backoffice/workflows/tests/test_views.py | 18 ++++++++++++++++- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/backoffice/.envs/local/.django b/backoffice/.envs/local/.django index 61d95d79..a0fd784e 100644 --- a/backoffice/.envs/local/.django +++ b/backoffice/.envs/local/.django @@ -22,3 +22,5 @@ OPENSEARCH_INDEX_PREFIX=backoffice-backend-local # Airflow AIRFLOW_BASE_URL=http://airflow-webserver:8080 AIRFLOW_TOKEN=YWlyZmxvdzphaXJmbG93 + +SERVICENOW_URL=https://cerntraining.service-now.com diff --git a/backoffice/backoffice/workflows/api/serializers.py b/backoffice/backoffice/workflows/api/serializers.py index cabb7cb4..8452342d 100644 --- a/backoffice/backoffice/workflows/api/serializers.py +++ b/backoffice/backoffice/workflows/api/serializers.py @@ -1,3 +1,5 @@ +from os import environ + from django_elasticsearch_dsl_drf.serializers import DocumentSerializer from drf_spectacular.utils import OpenApiExample, extend_schema_serializer from rest_framework import serializers @@ -7,15 +9,25 @@ from backoffice.workflows.models import Workflow, WorkflowTicket -class WorkflowSerializer(serializers.ModelSerializer): +class WorkflowTicketSerializer(serializers.ModelSerializer): + ticket_url = serializers.SerializerMethodField() + class Meta: - model = Workflow + model = WorkflowTicket fields = "__all__" + def get_ticket_url(self, obj): + return ( + f"{environ.get('SERVICENOW_URL')}" + f"/nav_to.do?uri=/u_request_fulfillment.do?sys_id={obj.ticket_id}" + ) + + +class WorkflowSerializer(serializers.ModelSerializer): + tickets = WorkflowTicketSerializer(many=True, read_only=True) -class WorkflowTicketSerializer(serializers.ModelSerializer): class Meta: - model = WorkflowTicket + model = Workflow fields = "__all__" diff --git a/backoffice/backoffice/workflows/models.py b/backoffice/backoffice/workflows/models.py index 265641cb..cd63f476 100644 --- a/backoffice/backoffice/workflows/models.py +++ b/backoffice/backoffice/workflows/models.py @@ -34,7 +34,9 @@ class Workflow(models.Model): class WorkflowTicket(models.Model): - workflow_id = models.ForeignKey(Workflow, on_delete=models.CASCADE) + workflow_id = models.ForeignKey( + Workflow, related_name="tickets", on_delete=models.CASCADE + ) ticket_id = models.CharField( max_length=32, null=False, blank=False ) # in SNOW it's GUID diff --git a/backoffice/backoffice/workflows/tests/test_views.py b/backoffice/backoffice/workflows/tests/test_views.py index 0c8737dd..9127ada4 100644 --- a/backoffice/backoffice/workflows/tests/test_views.py +++ b/backoffice/backoffice/workflows/tests/test_views.py @@ -14,7 +14,10 @@ from rest_framework.test import APIClient from backoffice.workflows import airflow_utils -from backoffice.workflows.api.serializers import WorkflowTicketSerializer +from backoffice.workflows.api.serializers import ( + WorkflowSerializer, + WorkflowTicketSerializer, +) from backoffice.workflows.constants import WORKFLOW_DAGS, StatusChoices, WorkflowType from backoffice.workflows.models import WorkflowTicket @@ -75,6 +78,16 @@ def test_list_anonymous(self): self.assertEqual(response.status_code, 403) + def test_tickets(self): + WorkflowTicket.objects.create( + workflow_id=self.workflow, ticket_id="123", ticket_type="author_create_user" + ) + workflow_data = WorkflowSerializer(self.workflow).data + + assert "tickets" in workflow_data + assert "ticket_id" in workflow_data["tickets"][0] + assert "ticket_type" in workflow_data["tickets"][0] + class TestWorkflowSearchViewSet(BaseTransactionTestCase): endpoint = "/api/workflows/search/" @@ -219,6 +232,9 @@ def test_get_ticket_happy_flow(self): assert response.status_code == 200 assert response.data == WorkflowTicketSerializer(self.workflow_ticket).data + def test_ticket_url(self): + assert "ticket_url" in WorkflowTicketSerializer(self.workflow_ticket).data + def test_create_missing_params(self): self.api_client.force_authenticate(user=self.curator) response = self.api_client.post(