diff --git a/SAS/TMSS/backend/src/tmss/tmssapp/viewsets/specification.py b/SAS/TMSS/backend/src/tmss/tmssapp/viewsets/specification.py index 18b2b7a90538b4ad92e247dd79bc03119f022c40..98a8b4011fc1c25103750b7c450c6417e1d3ece2 100644 --- a/SAS/TMSS/backend/src/tmss/tmssapp/viewsets/specification.py +++ b/SAS/TMSS/backend/src/tmss/tmssapp/viewsets/specification.py @@ -560,7 +560,9 @@ class TaskDraftCopyViewSet(LOFARCopyViewSet): serializer_class = serializers.TaskDraftSerializer @swagger_auto_schema(responses={201: 'The new Task Draft', - 403: 'forbidden'}, + 400: 'bad request', + 403: 'forbidden', + 404: 'not found'}, operation_description="Copy this Task Draft to a new Task Draft") def create(self, request, *args, **kwargs): if 'id' in kwargs: @@ -570,15 +572,20 @@ class TaskDraftCopyViewSet(LOFARCopyViewSet): body_data = json.loads(body_unicode) copy_reason = body_data.get('copy_reason', None) + if copy_reason == None: + content = {'Error': 'copy_reason is missing'} + return Response(content, status=status.HTTP_400_BAD_REQUEST) try: copy_reason_obj = models.CopyReason.objects.get(value=copy_reason) except ObjectDoesNotExist: logger.info("CopyReason matching query does not exist.") - #if a non valid copy_reason is specified, set copy_reason to None - copy_reason = None + #if a non valid copy_reason is specified, raise a 404 error + content = {'Error': 'CopyReason matching query does not exist.'} + return Response(content, status=status.HTTP_404_NOT_FOUND) - task_draft_copy = copy_task_draft(task_draft,copy_reason) + # TODO: Update copy_task_draft() accordingly if needed. + task_draft_copy = copy_task_draft(task_draft,copy_reason_obj) # url path magic to construct the new task_draft_path url @@ -601,7 +608,9 @@ class SchedulingUnitDraftCopyViewSet(LOFARCopyViewSet): serializer_class = serializers.SchedulingUnitDraftCopySerializer @swagger_auto_schema(responses={201: 'The new SchedulingUnitDraft', - 403: 'forbidden'}, + 400: 'bad request', + 403: 'forbidden', + 404: 'not found'}, operation_description="Copy a SchedulingUnitDraft to a new SchedulingUnitDraft") def create(self, request, *args, **kwargs): if 'id' in kwargs: @@ -612,13 +621,17 @@ class SchedulingUnitDraftCopyViewSet(LOFARCopyViewSet): body_data = json.loads(body_unicode) copy_reason = body_data.get('copy_reason', None) + if copy_reason == None: + content = {'Error': 'copy_reason is missing'} + return Response(content, status=status.HTTP_400_BAD_REQUEST) try: copy_reason_obj = models.CopyReason.objects.get(value=copy_reason) except ObjectDoesNotExist: logger.info("CopyReason matching query does not exist.") - #if a non valid copy_reason is specified, set copy_reason to None - copy_reason = None + # if a non valid copy_reason is specified, raise a 404 error + content = {'Error': 'CopyReason matching query does not exist.'} + return Response(content, status=status.HTTP_404_NOT_FOUND) scheduling_set_id = body_data.get('scheduling_set_id', None) logger.info(scheduling_set_id) @@ -628,6 +641,7 @@ class SchedulingUnitDraftCopyViewSet(LOFARCopyViewSet): except ObjectDoesNotExist: logger.info("scheduling Set does not exist.") + # TODO: Change parameter from copy_reason to copy_reason_obj and update copy_scheduling_unit_draft() accordingly. scheduling_unit_draft_copy = copy_scheduling_unit_draft(scheduling_unit_draft,scheduling_set,copy_reason) # url path magic to construct the new scheduling_unit_draft_path url scheduling_unit_draft_path = request._request.path @@ -655,7 +669,9 @@ class SchedulingUnitDraftCopyFromSchedulingSetViewSet(LOFARCopyViewSet): return models.SchedulingUnitDraft.objects.all() @swagger_auto_schema(responses={201: "The SchedulingUnitSet which will also contain the created new draft(s)", - 403: 'forbidden'}, + 400: 'bad request', + 403: 'forbidden', + 404: 'not found'}, operation_description="Copy the SchedulingUnitDraft(s) in this SchedulingUnitSet to new SchedulingUnitDraft(s)") def create(self, request, *args, **kwargs): if 'id' in kwargs: @@ -665,18 +681,22 @@ class SchedulingUnitDraftCopyFromSchedulingSetViewSet(LOFARCopyViewSet): body_unicode = request.body.decode('utf-8') body_data = json.loads(body_unicode) - copy_reason = body_data.get('copy_reason', None) + if copy_reason == None: + content = {'Error': 'copy_reason is missing'} + return Response(content, status=status.HTTP_400_BAD_REQUEST) try: copy_reason_obj = models.CopyReason.objects.get(value=copy_reason) except ObjectDoesNotExist: logger.info("CopyReason matching query does not exist.") - #if a non valid copy_reason is specified, set copy_reason to None - copy_reason = None + # if a non valid copy_reason is specified, raise a 404 error + content = {'Error': 'CopyReason matching query does not exist.'} + return Response(content, status=status.HTTP_404_NOT_FOUND) scheduling_unit_draft_copy_path=[] for scheduling_unit_draft in scheduling_unit_drafts: + # TODO: Change parameter from copy_reason to copy_reason_obj and update copy_scheduling_unit_draft() accordingly. scheduling_unit_draft_copy = copy_scheduling_unit_draft(scheduling_unit_draft,scheduling_set,copy_reason) # url path magic to construct the new scheduling_unit_draft url copy_scheduling_unit_draft_path = request._request.path @@ -694,7 +714,9 @@ class SchedulingUnitBlueprintCopyToSchedulingUnitDraftViewSet(LOFARCopyViewSet): serializer_class = serializers.SchedulingUnitBlueprintCopyToSchedulingUnitDraftSerializer @swagger_auto_schema(responses={201: "The new SchedulingUnitDraft copied from this SchedulingUnitBlueprint", - 403: 'forbidden'}, + 400: 'bad request', + 403: 'forbidden', + 404: 'not found'}, operation_description="Copy the SchedulingUnitBlueprint to a new SchedulingUnitDraft") def create(self, request, *args, **kwargs): @@ -704,17 +726,21 @@ class SchedulingUnitBlueprintCopyToSchedulingUnitDraftViewSet(LOFARCopyViewSet): body_unicode = request.body.decode('utf-8') body_data = json.loads(body_unicode) - copy_reason = body_data.get('copy_reason', None) + if copy_reason == None: + content = {'Error': 'copy_reason is missing'} + return Response(content, status=status.HTTP_400_BAD_REQUEST) try: copy_reason_obj = models.CopyReason.objects.get(value=copy_reason) except ObjectDoesNotExist: logger.info("CopyReason matching query does not exist.") - #if a non valid copy_reason is specified, set copy_reason to None - copy_reason = None + # if a non valid copy_reason is specified, raise a 404 error + content = {'Error': 'CopyReason matching query does not exist.'} + return Response(content, status=status.HTTP_404_NOT_FOUND) - scheduling_unit_draft = create_scheduling_unit_draft_from_scheduling_unit_blueprint(scheduling_unit_blueprint,copy_reason) + # TODO: Update create_scheduling_unit_draft_from_scheduling_unit_blueprint() accordingly if needed. + scheduling_unit_draft = create_scheduling_unit_draft_from_scheduling_unit_blueprint(scheduling_unit_blueprint,copy_reason_obj) # return a response with the new serialized scheduling_unit_blueprint (with references to the created task_blueprint(s) and (scheduled) subtasks) return Response(serializers.SchedulingUnitDraftSerializer(scheduling_unit_draft, context={'request':request}).data, @@ -729,11 +755,31 @@ class TaskBlueprintCopyToTaskDraftViewSet(LOFARCopyViewSet): serializer_class = serializers.SchedulingUnitBlueprintCopyToSchedulingUnitDraftSerializer @swagger_auto_schema(responses={201: "The new TaskDraft created from this TaskBlueprint", - 403: 'forbidden'}, + 400: 'bad request', + 403: 'forbidden', + 404: 'not found'}, operation_description="Copy this TaskBlueprint into a new TaskDraft.") def create(self, request, *args, **kwargs): if 'id' in kwargs: task_blueprint = get_object_or_404(models.TaskBlueprint, pk=kwargs['id']) + + body_unicode = request.body.decode('utf-8') + body_data = json.loads(body_unicode) + + copy_reason = body_data.get('copy_reason', None) + if copy_reason == None: + content = {'Error': 'copy_reason is missing'} + return Response(content, status=status.HTTP_400_BAD_REQUEST) + + try: + copy_reason_obj = models.CopyReason.objects.get(value=copy_reason) + except ObjectDoesNotExist: + logger.info("CopyReason matching query does not exist.") + #if a non valid copy_reason is specified, raise a 404 error + content = {'Error': 'CopyReason matching query does not exist.'} + return Response(content, status=status.HTTP_404_NOT_FOUND) + + # TODO: Add copy_reason_obj and update the copy_task_blueprint_to_task_draft() accordingly. task_draft = copy_task_blueprint_to_task_draft(task_blueprint) # return a response with the new serialized scheduling_unit_blueprint (with references to the created task_blueprint(s) and (scheduled) subtasks)