Skip to content
Snippets Groups Projects
Commit 40617fc2 authored by Fabio Vitello's avatar Fabio Vitello
Browse files

TMSS-234: POST API to copy scheduling_unit_draft

POST API /scheduling_unit_draft/{scheduling_unit_draft_id}/copy/ create a copy of a scheduling unit draft.
If no scheduling_set_id is specified as post parameter (or an invalid id is provided) the new scheduling unit draft will belong to the same scheduling_set.
parent 74a8ee47
No related branches found
No related tags found
1 merge request!181Resolve TMSS-234
......@@ -248,7 +248,12 @@ class SchedulingUnitDraftSerializer(RelationalHyperlinkedModelSerializer):
extra_fields = ['scheduling_unit_blueprints', 'task_drafts']
class SchedulingUnitDraftCopySerializer(SchedulingUnitDraftSerializer):
class Meta(SchedulingUnitDraftSerializer.Meta):
fields = ['copy_reason']
extra_fields =['scheduling_set_id']
read_only_fields = ['scheduling_unit_blueprints','task_drafts']
class SchedulingUnitDraftCopyFromSchedulingSetSerializer(SchedulingUnitDraftSerializer):
class Meta(SchedulingUnitDraftSerializer.Meta):
fields = ['copy_reason']
read_only_fields = ['scheduling_unit_blueprints','task_drafts']
......
......@@ -45,6 +45,8 @@ def copy_scheduling_unit_draft(scheduling_unit_draft: models.SchedulingUnitDraft
scheduling_unit_draft_copy.name="%s (Copy)" % (scheduling_unit_draft.name,)
scheduling_unit_draft_copy.description="%s (Copy from %s)" % (scheduling_unit_draft.description or "<no description>",scheduling_unit_draft.name,)
scheduling_unit_draft_copy.scheduling_set=scheduling_set_dst
scheduling_unit_draft_copy.save()
scheduling_unit_draft_copy.task_drafts.set(task_drafts)
scheduling_unit_draft_copy.scheduling_unit_blueprints.set(scheduling_unit_blueprints)
......
......@@ -26,6 +26,8 @@ from lofar.common.datetimeutils import formatDatetime
from lofar.sas.tmss.tmss.tmssapp.tasks import *
from lofar.sas.tmss.tmss.tmssapp.subtasks import *
from django.urls import resolve, get_script_prefix,Resolver404
import json
import logging
......@@ -245,12 +247,10 @@ class SchedulingUnitDraftViewSet(LOFARViewSet):
return Response(serializers.SchedulingUnitBlueprintSerializer(scheduling_unit_blueprint, context={'request':request}).data,
status=status.HTTP_201_CREATED,
headers={'Location': scheduling_unit_blueprint_path})
""""
@swagger_auto_schema(responses={201: 'The Copied SchedulingUnitDraft, see Location in Response header',
403: 'forbidden'},
operation_description="Copy this SchedulingUnitDraft into a new one.")
#@action(methods=['get'], detail=True, url_name="copy_scheduling_unit_draft", url_path="copy_scheduling_unit_draft(?:/(?P<scheduling_set_id>[0-9]+))?", name="Copy Scheduling Unit Draft")
@action(methods=['post'], detail=True, url_name="copy_scheduling_unit_draft", name="Copy Scheduling Unit Draft")
def copy_scheduling_unit_draft(self, request, scheduling_set = None, pk=None, *args, **kwargs):
......@@ -276,7 +276,7 @@ class SchedulingUnitDraftViewSet(LOFARViewSet):
return Response(serializers.SchedulingUnitDraftSerializer(scheduling_unit_draft_copy, context={'request':request}).data,
status=status.HTTP_201_CREATED,
headers={'Location': scheduling_unit_draft_copy_path})
"""
@swagger_auto_schema(responses={201: 'The Created Task Draft, see Location in Response header',
403: 'forbidden'},
......@@ -304,10 +304,58 @@ class SchedulingUnitDraftNestedViewSet(LOFARNestedViewSet):
return models.SchedulingUnitDraft.objects.all()
#Used to copy Scheduling Unit Draft from Scheduling Unit Draft
class SchedulingUnitDraftCopyViewSet(LOFARCopyViewSet):
queryset = models.SchedulingUnitDraft.objects.all()
serializer_class = serializers.SchedulingUnitDraftCopySerializer
def create(self, request, *args, **kwargs):
if 'scheduling_unit_draft_id' in kwargs:
scheduling_unit_draft = get_object_or_404(models.SchedulingUnitDraft, pk=kwargs['scheduling_unit_draft_id'])
scheduling_set = scheduling_unit_draft.scheduling_set
body_unicode = request.body.decode('utf-8')
body_data = json.loads(body_unicode)
copy_reason = body_data.get('copy_reason', None)
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
scheduling_set_id = body_data.get('scheduling_set_id', None)
logger.info(scheduling_set_id)
if scheduling_set_id is not None:
try:
scheduling_set = models.SchedulingSet.objects.get(id=scheduling_set_id)
except ObjectDoesNotExist:
logger.info("scheduling Set does not exist.")
logger.info(scheduling_set.__dict__)
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
base_path = scheduling_unit_draft_path[:scheduling_unit_draft_path.find('/scheduling_unit_draft')]
scheduling_unit_draft_copy_path = '%s/scheduling_unit_draft/%s/' % (base_path, scheduling_unit_draft_copy.id,)
# return a response with the new serialized SchedulingUnitBlueprintSerializer, and a Location to the new instance in the header
return Response(serializers.SchedulingUnitDraftSerializer(scheduling_unit_draft_copy, context={'request':request}).data,
status=status.HTTP_201_CREATED,
headers={'Location': scheduling_unit_draft_copy_path})
else:
content = {'Error': 'scheduling_unit_draft_id is missing'}
return Response(content, status=status.HTTP_404_NOT_FOUND)
#Used to copy Scheduling Unit Draft from Scheduling set
class SchedulingUnitDraftCopyFromSchedulingSetViewSet(LOFARCopyViewSet):
queryset = models.SchedulingUnitDraft.objects.all()
serializer_class = serializers.SchedulingUnitDraftCopyFromSchedulingSetSerializer
def get_queryset(self):
if 'scheduling_set_id' in self.kwargs:
......@@ -318,10 +366,6 @@ class SchedulingUnitDraftCopyViewSet(LOFARCopyViewSet):
def create(self, request, *args, **kwargs):
logger.info(kwargs)
# return a response with the new serialized SchedulingUnitBlueprintSerializer, and a Location to the new instance in the header
if 'scheduling_set_id' in kwargs:
scheduling_set = get_object_or_404(models.SchedulingSet, pk=kwargs['scheduling_set_id'])
scheduling_unit_drafts = scheduling_set.scheduling_unit_drafts.all()
......@@ -341,7 +385,6 @@ class SchedulingUnitDraftCopyViewSet(LOFARCopyViewSet):
scheduling_unit_draft_copy_path=[]
for scheduling_unit_draft in scheduling_unit_drafts:
logger.info(scheduling_unit_draft.__dict__)
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
......
......@@ -129,7 +129,8 @@ router.register(r'task_blueprint/(?P<task_blueprint_id>\d+)/task_relation_bluepr
router.register(r'task_blueprint/(?P<task_blueprint_id>\d+)/subtask', viewsets.SubtaskNestedViewSet)
# copy
router.register(r'scheduling_set/(?P<scheduling_set_id>\d+)/copy_scheduling_unit_draft', viewsets.SchedulingUnitDraftCopyViewSet)
router.register(r'scheduling_set/(?P<scheduling_set_id>\d+)/copy_scheduling_unit_draft', viewsets.SchedulingUnitDraftCopyFromSchedulingSetViewSet)
router.register(r'scheduling_unit_draft/(?P<scheduling_unit_draft_id>\d+)/copy', viewsets.SchedulingUnitDraftCopyViewSet)
# SCHEDULING
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment