diff --git a/SAS/TMSS/src/tmss/tmssapp/viewsets/lofar_viewset.py b/SAS/TMSS/src/tmss/tmssapp/viewsets/lofar_viewset.py index d8da38f7ac126cb8644f22d336dc6c4c5c54d5ec..017d8e13c90b3b6f7e4dce84d8136e1553a38393 100644 --- a/SAS/TMSS/src/tmss/tmssapp/viewsets/lofar_viewset.py +++ b/SAS/TMSS/src/tmss/tmssapp/viewsets/lofar_viewset.py @@ -7,6 +7,7 @@ Adds the following functionality: from rest_framework import viewsets from drf_yasg.utils import swagger_auto_schema +from rest_framework import mixins class LOFARViewSet(viewsets.ModelViewSet): """ @@ -38,3 +39,15 @@ class LOFARViewSet(viewsets.ModelViewSet): def destroy(self, request, pk=None, **kwargs): return super(LOFARViewSet, self).destroy(request, pk, **kwargs) +class LOFARNestedViewSet(mixins.CreateModelMixin, + mixins.ListModelMixin, + #mixins.RetrieveModelMixin, + viewsets.GenericViewSet): + + @swagger_auto_schema(responses={403: 'forbidden'}) + def list(self, request, **kwargs): + return super(LOFARNestedViewSet, self).list(request, **kwargs) + + @swagger_auto_schema(responses={400: 'invalid specification', 403: 'forbidden'}) + def create(self, request, **kwargs): + return super(LOFARNestedViewSet, self).create(request, **kwargs) \ No newline at end of file diff --git a/SAS/TMSS/src/tmss/tmssapp/viewsets/scheduling.py b/SAS/TMSS/src/tmss/tmssapp/viewsets/scheduling.py index 8a94006668be0cdc1209348dd79f303a8089939f..1d5460eec3fbd65e9f4015cf78ccdaaede06fa5f 100644 --- a/SAS/TMSS/src/tmss/tmssapp/viewsets/scheduling.py +++ b/SAS/TMSS/src/tmss/tmssapp/viewsets/scheduling.py @@ -4,7 +4,7 @@ This file contains the viewsets (based on the elsewhere defined data models and from django.shortcuts import get_object_or_404 from rest_framework import viewsets -from .lofar_viewset import LOFARViewSet +from .lofar_viewset import LOFARViewSet, LOFARNestedViewSet from .. import models from .. import serializers from django_filters import rest_framework as filters @@ -140,13 +140,28 @@ class SubtaskViewSet(LOFARViewSet): filter_backends = (filters.DjangoFilterBackend,) filter_class = subTaskFilter + @swagger_auto_schema(responses={200: 'A LOFAR parset for this subtask', + 403: 'forbidden'}, + operation_description="Get a a LOFAR parset for the specifications of this subtask") + @action(methods=['get'], detail=True) + def default_specification(self, request, pk=None): + subtask = get_object_or_404(models.Subtask, pk=pk) + parset = convert_to_parset(subtask) + return HttpResponse(str(parset), content_type='text/plain') + + +class SubtaskNestedViewSet(LOFARNestedViewSet): + queryset = models.Subtask.objects.all() + serializer_class = serializers.SubtaskSerializer + filter_backends = (filters.DjangoFilterBackend,) + filter_class = subTaskFilter + def get_queryset(self): - if 'task_blueprint_pk' in self.kwargs: - task_blueprint = get_object_or_404(models.TaskBlueprint, pk=self.kwargs['task_blueprint_pk']) + if 'task_blueprint_id' in self.kwargs: + task_blueprint = get_object_or_404(models.TaskBlueprint, pk=self.kwargs['task_blueprint_id']) return task_blueprint.subtasks.all() - else: - return models.Subtask.objects.all() - + +""" @swagger_auto_schema(responses={200: 'A LOFAR parset for this subtask', 403: 'forbidden'}, operation_description="Get a a LOFAR parset for the specifications of this subtask") @@ -155,6 +170,7 @@ class SubtaskViewSet(LOFARViewSet): subtask = get_object_or_404(models.Subtask, pk=pk) parset = convert_to_parset(subtask) return HttpResponse(str(parset), content_type='text/plain') +""" class SubtaskInputViewSet(LOFARViewSet): queryset = models.SubtaskInput.objects.all() diff --git a/SAS/TMSS/src/tmss/tmssapp/viewsets/specification.py b/SAS/TMSS/src/tmss/tmssapp/viewsets/specification.py index f46858de037bff9221cc805fcbd9c9fc08b5d1ec..52000bd56b5546e008688504a144c76df74db7b7 100644 --- a/SAS/TMSS/src/tmss/tmssapp/viewsets/specification.py +++ b/SAS/TMSS/src/tmss/tmssapp/viewsets/specification.py @@ -13,13 +13,16 @@ from rest_framework.decorators import action from drf_yasg.utils import swagger_auto_schema -from lofar.sas.tmss.tmss.tmssapp.viewsets.lofar_viewset import LOFARViewSet +from lofar.sas.tmss.tmss.tmssapp.viewsets.lofar_viewset import LOFARViewSet, LOFARNestedViewSet from lofar.sas.tmss.tmss.tmssapp import models from lofar.sas.tmss.tmss.tmssapp import serializers from lofar.common.json_utils import get_default_json_object_for_schema + + + # This is required for keeping a user reference as ForeignKey in other models # (I think so that the HyperlinkedModelSerializer can generate a URI) class UserViewSet(ReadOnlyModelViewSet): @@ -119,12 +122,16 @@ class ProjectViewSet(LOFARViewSet): queryset = models.Project.objects.all() serializer_class = serializers.ProjectSerializer + +class ProjectNestedViewSet(LOFARNestedViewSet): + queryset = models.Project.objects.all() + serializer_class = serializers.ProjectSerializer + def get_queryset(self): - if 'cycle_pk' in self.kwargs: - cycle = get_object_or_404(models.Cycle, pk=self.kwargs['cycle_pk']) + if 'cycle_id' in self.kwargs: + cycle = get_object_or_404(models.Cycle, pk=self.kwargs['cycle_id']) return cycle.projects.all() - else: - return models.Project.objects.all() + class ProjectQuotaViewSet(LOFARViewSet): queryset = models.ProjectQuota.objects.all() @@ -145,60 +152,72 @@ class SchedulingUnitDraftViewSet(LOFARViewSet): queryset = models.SchedulingUnitDraft.objects.all() serializer_class = serializers.SchedulingUnitDraftSerializer + +class SchedulingUnitDraftNestedViewSet(LOFARNestedViewSet): + queryset = models.SchedulingUnitDraft.objects.all() + serializer_class = serializers.SchedulingUnitDraftSerializer + def get_queryset(self): - if 'scheduling_set_pk' in self.kwargs: - scheduling_set = get_object_or_404(models.SchedulingSet, pk=self.kwargs['scheduling_set_pk']) + if 'scheduling_set_id' in self.kwargs: + scheduling_set = get_object_or_404(models.SchedulingSet, pk=self.kwargs['scheduling_set_id']) return scheduling_set.scheduling_unit_drafts.all() - else: - return models.SchedulingUnitDraft.objects.all() - class SchedulingUnitBlueprintViewSet(LOFARViewSet): queryset = models.SchedulingUnitBlueprint.objects.all() serializer_class = serializers.SchedulingUnitBlueprintSerializer + +class SchedulingUnitBlueprintNestedViewSet(LOFARNestedViewSet): + queryset = models.SchedulingUnitBlueprint.objects.all() + serializer_class = serializers.SchedulingUnitBlueprintSerializer + def get_queryset(self): - if 'scheduling_unit_draft_pk' in self.kwargs: - scheduling_unit_draft = get_object_or_404(models.SchedulingUnitDraft, pk=self.kwargs['scheduling_unit_draft_pk']) + if 'scheduling_unit_draft_id' in self.kwargs: + scheduling_unit_draft = get_object_or_404(models.SchedulingUnitDraft, pk=self.kwargs['scheduling_unit_draft_id']) return scheduling_unit_draft.related_scheduling_unit_blueprint.all() - else: - return models.SchedulingUnitBlueprint.objects.all() - + class TaskDraftViewSet(LOFARViewSet): queryset = models.TaskDraft.objects.all() serializer_class = serializers.TaskDraftSerializer + +class TaskDraftNestedViewSet(LOFARNestedViewSet): + queryset = models.TaskDraft.objects.all() + serializer_class = serializers.TaskDraftSerializer + def get_queryset(self): - if 'scheduling_unit_draft_pk' in self.kwargs: - scheduling_unit_draft = get_object_or_404(models.SchedulingUnitDraft, pk=self.kwargs['scheduling_unit_draft_pk']) + if 'scheduling_unit_draft_id' in self.kwargs: + scheduling_unit_draft = get_object_or_404(models.SchedulingUnitDraft, pk=self.kwargs['scheduling_unit_draft_id']) return scheduling_unit_draft.task_drafts.all() - else: - return models.TaskDraft.objects.all() - class TaskBlueprintViewSet(LOFARViewSet): queryset = models.TaskBlueprint.objects.all() serializer_class = serializers.TaskBlueprintSerializer + +class TaskBlueprintNestedViewSet(LOFARNestedViewSet): + queryset = models.TaskBlueprint.objects.all() + serializer_class = serializers.TaskBlueprintSerializer def get_queryset(self): - if 'task_draft_pk' in self.kwargs: - task_draft = get_object_or_404(models.TaskDraft, pk=self.kwargs['task_draft_pk']) + if 'task_draft_id' in self.kwargs: + task_draft = get_object_or_404(models.TaskDraft, pk=self.kwargs['task_draft_id']) return task_draft.related_task_blueprint.all() - else: - return models.TaskBlueprint.objects.all() - class TaskRelationDraftViewSet(LOFARViewSet): queryset = models.TaskRelationDraft.objects.all() serializer_class = serializers.TaskRelationDraftSerializer + +class TaskRelationDraftNestedViewSet(LOFARNestedViewSet): + queryset = models.TaskRelationDraft.objects.all() + serializer_class = serializers.TaskRelationDraftSerializer + def get_queryset(self): - if 'task_draft_pk' in self.kwargs: - task_draft = get_object_or_404(models.TaskDraft, pk=self.kwargs['task_draft_pk']) + if 'task_draft_id' in self.kwargs: + task_draft = get_object_or_404(models.TaskDraft, pk=self.kwargs['task_draft_id']) return task_draft.produced_by.all() | task_draft.consumed_by.all() - else: - return models.TaskRelationDraft.objects.all() + class TaskRelationBlueprintViewSet(LOFARViewSet): @@ -206,15 +225,20 @@ class TaskRelationBlueprintViewSet(LOFARViewSet): serializer_class = serializers.TaskRelationBlueprintSerializer + +class TaskRelationBlueprintNestedViewSet(LOFARNestedViewSet): + queryset = models.TaskRelationBlueprint.objects.all() + serializer_class = serializers.TaskRelationBlueprintSerializer + + def get_queryset(self): - if 'task_blueprint_pk' in self.kwargs: - task_blueprint = get_object_or_404(models.TaskBlueprint, pk=self.kwargs['task_blueprint_pk']) + if 'task_blueprint_id' in self.kwargs: + task_blueprint = get_object_or_404(models.TaskBlueprint, pk=self.kwargs['task_blueprint_id']) return task_blueprint.produced_by.all() | task_blueprint.consumed_by.all() - elif 'task_relation_draft_pk' in self.kwargs: - task_relation_draft = get_object_or_404(models.TaskRelationDraft, pk=self.kwargs['task_relation_draft_pk']) + elif 'task_relation_draft_id' in self.kwargs: + task_relation_draft = get_object_or_404(models.TaskRelationDraft, pk=self.kwargs['task_relation_draft_id']) return task_relation_draft.related_task_relation_blueprint.all() - else: - return models.TaskRelationBlueprint.objects.all() + # --- JSON @@ -240,8 +264,8 @@ class TaskDraftViewSetJSONeditorOnline(LOFARViewSet): return name def get_queryset(self): - if 'scheduling_unit_draft_pk' in self.kwargs: - scheduling_unit_draft = get_object_or_404(models.SchedulingUnitDraft, pk=self.kwargs['scheduling_unit_draft_pk']) + if 'scheduling_unit_draft_id' in self.kwargs: + scheduling_unit_draft = get_object_or_404(models.SchedulingUnitDraft, pk=self.kwargs['scheduling_unit_draft_id']) return scheduling_unit_draft.task_drafts.all() else: return models.TaskDraft.objects.all() \ No newline at end of file diff --git a/SAS/TMSS/src/tmss/urls.py b/SAS/TMSS/src/tmss/urls.py index 054994d7f92c6f04764a746069910b5aab98b626..8e38bc37b51a75bc495616bf1aa56ab4cbc254b7 100644 --- a/SAS/TMSS/src/tmss/urls.py +++ b/SAS/TMSS/src/tmss/urls.py @@ -99,16 +99,16 @@ router.register(r'task_relation_draft', viewsets.TaskRelationDraftViewSet) router.register(r'task_relation_blueprint', viewsets.TaskRelationBlueprintViewSet) # nested -router.register(r'cycle/(?P<cycle_id>[\w\-]+)/project', viewsets.ProjectViewSet) -router.register(r'scheduling_set/(?P<scheduling_set_id>\d+)/scheduling_unit_draft', viewsets.SchedulingUnitDraftViewSet) -router.register(r'scheduling_unit_draft/(?P<scheduling_unit_draft_id>\d+)/scheduling_unit_blueprint', viewsets.SchedulingUnitBlueprintViewSet) -#router.register(r'scheduling_unit_draft/(?P<scheduling_unit_draft_id>\d+)/task_draft', viewsets.TaskDraftViewSet) # todo: default view, re-activate or remove the JSON editor one in bottom -router.register(r'task_draft/(?P<task_draft_id>\d+)/task_blueprint', viewsets.TaskBlueprintViewSet) -router.register(r'task_draft/(?P<task_draft_id>\d+)/task_relation_draft', viewsets.TaskRelationDraftViewSet) -router.register(r'task_relation_draft/(?P<task_relation_draft_id>\d+)/task_relation_blueprint', viewsets.TaskRelationBlueprintViewSet) -router.register(r'task_blueprint/(?P<task_blueprint_id>\d+)/task_relation_blueprint', viewsets.TaskRelationBlueprintViewSet) -router.register(r'task_blueprint/(?P<task_blueprint_id>\d+)/subtask', viewsets.SubtaskViewSet) -router.register(r'subtask/(?P<subtask_id>[\w\-]+)/state_log', viewsets.SubtaskStateLogViewSet) +router.register(r'cycle/(?P<cycle_id>[\w\-]+)/project', viewsets.ProjectNestedViewSet) +router.register(r'scheduling_set/(?P<scheduling_set_id>\d+)/scheduling_unit_draft', viewsets.SchedulingUnitDraftNestedViewSet) +router.register(r'scheduling_unit_draft/(?P<scheduling_unit_draft_id>\d+)/scheduling_unit_blueprint', viewsets.SchedulingUnitBlueprintNestedViewSet) +router.register(r'scheduling_unit_draft/(?P<scheduling_unit_draft_id>\d+)/task_draft', viewsets.TaskDraftNestedViewSet) # todo: default view, re-activate or remove the JSON editor one in bottom +router.register(r'task_draft/(?P<task_draft_id>\d+)/task_blueprint', viewsets.TaskBlueprintNestedViewSet) +router.register(r'task_draft/(?P<task_draft_id>\d+)/task_relation_draft', viewsets.TaskRelationDraftNestedViewSet) +router.register(r'task_relation_draft/(?P<task_relation_draft_id>\d+)/task_relation_blueprint', viewsets.TaskRelationBlueprintNestedViewSet) +router.register(r'task_blueprint/(?P<task_blueprint_id>\d+)/task_relation_blueprint', viewsets.TaskRelationBlueprintNestedViewSet) +router.register(r'task_blueprint/(?P<task_blueprint_id>\d+)/subtask', viewsets.SubtaskNestedViewSet) +#router.register(r'subtask/(?P<subtask_id>[\w\-]+)/state_log', viewsets.SubtaskStateLogViewSet) # SCHEDULING @@ -146,7 +146,8 @@ router.register(r'user', viewsets.UserViewSet) # JSON router.register(r'task_draft', viewsets.TaskDraftViewSetJSONeditorOnline) -router.register(r'scheduling_unit_draft/(?P<scheduling_unit_draft_id>\d+)/task_draft', viewsets.TaskDraftViewSetJSONeditorOnline) +#removed TMSS-177 +#router.register(r'scheduling_unit_draft/(?P<scheduling_unit_draft_id>\d+)/task_draft', viewsets.TaskDraftViewSetJSONeditorOnline) router.register(r'subtask', viewsets.SubtaskViewSetJSONeditorOnline) urlpatterns.extend(router.urls)