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 8452342d7..4ec077126 100644 --- a/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/api/serializers.py +++ b/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/api/serializers.py @@ -11,6 +11,7 @@ class WorkflowTicketSerializer(serializers.ModelSerializer): ticket_url = serializers.SerializerMethodField() + workflow_id = serializers.PrimaryKeyRelatedField(queryset=Workflow.objects.all()) class Meta: model = WorkflowTicket diff --git a/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/api/views.py b/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/api/views.py index 064d7229f..f37e7adf5 100644 --- a/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/api/views.py +++ b/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/api/views.py @@ -54,6 +54,8 @@ def get_queryset(self): class WorkflowTicketViewSet(viewsets.ViewSet): + serializer_class = WorkflowTicketSerializer + def retrieve(self, request, *args, **kwargs): workflow_id = kwargs.get("pk") ticket_type = request.query_params.get("ticket_type") @@ -68,7 +70,7 @@ def retrieve(self, request, *args, **kwargs): workflow_ticket = WorkflowTicket.objects.get( workflow_id=workflow_id, ticket_type=ticket_type ) - serializer = WorkflowTicketSerializer(workflow_ticket) + serializer = self.serializer_class(workflow_ticket) return Response(serializer.data) except WorkflowTicket.DoesNotExist: return Response( @@ -77,27 +79,11 @@ def retrieve(self, request, *args, **kwargs): ) def create(self, request, *args, **kwargs): - workflow_id = request.data.get("workflow_id") - ticket_type = request.data.get("ticket_type") - ticket_id = request.data.get("ticket_id") - - if not all([workflow_id, ticket_type, ticket_id]): - return Response( - {"error": "Workflow_id, ticket_id and ticket_type are required."}, - status=status.HTTP_400_BAD_REQUEST, - ) + serializer = self.serializer_class(data=request.data) - try: - workflow = Workflow.objects.get(id=workflow_id) - workflow_ticket = WorkflowTicket.objects.create( - workflow_id=workflow, ticket_id=ticket_id, ticket_type=ticket_type - ) - serializer = WorkflowTicketSerializer(workflow_ticket) + if serializer.is_valid(raise_exception=True): + serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) - except Exception as e: - return Response( - {"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR - ) class AuthorWorkflowViewSet(viewsets.ViewSet): @@ -267,7 +253,7 @@ def __init__(self, *args, **kwargs): filter_fields = { "status": "status", "workflow_type": "workflow_type", - "is_update": "is_update" + "is_update": "is_update", } ordering_fields = {"_updated_at": "_updated_at"} diff --git a/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/documents.py b/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/documents.py index 3db73dc8b..62e2aa061 100644 --- a/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/documents.py +++ b/backoffice-repo/backoffice-repo/backoffice/backoffice/workflows/documents.py @@ -28,7 +28,7 @@ class WorkflowDocument(Document): "value": fields.KeywordField(), "current": fields.BooleanField(), } - ) + ), } ) status = fields.KeywordField() 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 a1bcbdade..bfe70f936 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 @@ -242,8 +242,9 @@ def test_create_missing_params(self): ) assert response.status_code == 400 - assert response.data == { - "error": "Workflow_id, ticket_id and ticket_type are required." + assert response.json() == { + "workflow_id": ["This field is required."], + "ticket_id": ["This field is required."], } def test_create_happy_flow(self): @@ -404,25 +405,13 @@ def setUpClass(cls): Workflow.objects.update_or_create( data={ "ids": [ - { - "value": "0000-0003-3302-3333", - "schema": "ORCID" - }, - { - "value": "CastleFrank", - "schema": "INSPIRE BAI" - } + {"value": "0000-0003-3302-3333", "schema": "ORCID"}, + {"value": "CastleFrank", "schema": "INSPIRE BAI"}, ], - "name": { - "value": "Castle, Frank", - "preferred_name": "Frank Castle" - }, + "name": {"value": "Castle, Frank", "preferred_name": "Frank Castle"}, "email_addresses": [ - { - "value": "frank.castle@someting.ch", - "current": True - } - ] + {"value": "frank.castle@someting.ch", "current": True} + ], }, status=StatusChoices.APPROVAL, core=True, @@ -432,25 +421,13 @@ def setUpClass(cls): Workflow.objects.update_or_create( data={ "ids": [ - { - "value": "0000-0003-3302-2222", - "schema": "ORCID" - }, - { - "value": "SmithJohn", - "schema": "INSPIRE BAI" - } + {"value": "0000-0003-3302-2222", "schema": "ORCID"}, + {"value": "SmithJohn", "schema": "INSPIRE BAI"}, ], - "name": { - "value": "Smith, John", - "preferred_name": "John Smith" - }, + "name": {"value": "Smith, John", "preferred_name": "John Smith"}, "email_addresses": [ - { - "value": "john.smith@someting.ch", - "current": True - } - ] + {"value": "john.smith@someting.ch", "current": True} + ], }, status=StatusChoices.RUNNING, core=True,