From b734935f2526fce60d80b425e31044cf42361c1b Mon Sep 17 00:00:00 2001
From: Jorrit Schaap <schaap@astron.nl>
Date: Mon, 17 Aug 2020 10:14:12 +0200
Subject: [PATCH] TMSS-215: added model SchedulingUnitObservingStrategyTemplate
 which will hold templates describing observation stratefies for
 schedulingunits based on referenced SchedulingUnitTemplates

---
 .../tmss/tmssapp/migrations/0001_initial.py   | 19 ++++++++++++++++++-
 .../src/tmss/tmssapp/models/specification.py  |  6 ++++++
 .../tmss/tmssapp/serializers/specification.py |  6 ++++++
 .../tmss/tmssapp/viewsets/specification.py    | 15 +++++++++++++++
 SAS/TMSS/src/tmss/urls.py                     |  1 +
 5 files changed, 46 insertions(+), 1 deletion(-)

diff --git a/SAS/TMSS/src/tmss/tmssapp/migrations/0001_initial.py b/SAS/TMSS/src/tmss/tmssapp/migrations/0001_initial.py
index b04ecd0b93b..40dd21f84f0 100644
--- a/SAS/TMSS/src/tmss/tmssapp/migrations/0001_initial.py
+++ b/SAS/TMSS/src/tmss/tmssapp/migrations/0001_initial.py
@@ -1,4 +1,4 @@
-# Generated by Django 2.2.12 on 2020-08-04 12:35
+# Generated by Django 2.2.12 on 2020-08-17 07:24
 
 from django.conf import settings
 import django.contrib.postgres.fields
@@ -450,6 +450,19 @@ class Migration(migrations.Migration):
                 'abstract': False,
             },
         ),
+        migrations.CreateModel(
+            name='SchedulingUnitObservingStrategyTemplate',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('tags', django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=128), blank=True, default=list, help_text='User-defined search keywords for object.', size=8)),
+                ('created_at', models.DateTimeField(auto_now_add=True, help_text='Moment of object creation.')),
+                ('updated_at', models.DateTimeField(auto_now=True, help_text='Moment of last object update.')),
+                ('name', models.CharField(help_text='Human-readable name of this object.', max_length=128)),
+                ('description', models.CharField(help_text='A longer description of this object.', max_length=255)),
+                ('version', models.CharField(help_text='Version of this template (with respect to other templates of the same name).', max_length=128)),
+                ('schema', django.contrib.postgres.fields.jsonb.JSONField(help_text='Schema for the configurable parameters needed to use this template.')),
+            ],
+        ),
         migrations.CreateModel(
             name='SchedulingUnitTemplate',
             fields=[
@@ -955,6 +968,10 @@ class Migration(migrations.Migration):
             model_name='schedulingunittemplate',
             constraint=models.UniqueConstraint(fields=('name', 'version'), name='SchedulingUnitTemplate_unique_name_version'),
         ),
+        migrations.AddConstraint(
+            model_name='schedulingunitobservingstrategytemplate',
+            constraint=models.UniqueConstraint(fields=('name', 'version'), name='SchedulingUnitObservingStrategyTemplate_unique_name_version'),
+        ),
         migrations.AddField(
             model_name='schedulingunitdraft',
             name='copies',
diff --git a/SAS/TMSS/src/tmss/tmssapp/models/specification.py b/SAS/TMSS/src/tmss/tmssapp/models/specification.py
index 60b95bb0dff..0014401fe00 100644
--- a/SAS/TMSS/src/tmss/tmssapp/models/specification.py
+++ b/SAS/TMSS/src/tmss/tmssapp/models/specification.py
@@ -232,6 +232,12 @@ class DefaultGeneratorTemplate(BasicCommon):
     template = ForeignKey("GeneratorTemplate", on_delete=PROTECT)
 
 
+class SchedulingUnitObservingStrategyTemplate(Template):
+    class Meta:
+        # TODO: move up to the abstract base class and replace with django 3.0 UniqueConstraint(... name='%*class)s_unique_name_version)
+        constraints = [UniqueConstraint(fields=['name', 'version'], name='SchedulingUnitObservingStrategyTemplate_unique_name_version')]
+
+
 class SchedulingUnitTemplate(Template):
     class Meta:
         # TODO: move up to the abstract base class and replace with django 3.0 UniqueConstraint(... name='%*class)s_unique_name_version)
diff --git a/SAS/TMSS/src/tmss/tmssapp/serializers/specification.py b/SAS/TMSS/src/tmss/tmssapp/serializers/specification.py
index f034bc0acc0..fa02074c4e6 100644
--- a/SAS/TMSS/src/tmss/tmssapp/serializers/specification.py
+++ b/SAS/TMSS/src/tmss/tmssapp/serializers/specification.py
@@ -100,6 +100,12 @@ class DefaultGeneratorTemplateSerializer(RelationalHyperlinkedModelSerializer):
         fields = '__all__'
 
 
+class SchedulingUnitObservingStrategyTemplateSerializer(RelationalHyperlinkedModelSerializer):
+    class Meta:
+        model = models.SchedulingUnitObservingStrategyTemplate
+        fields = '__all__'
+
+
 class SchedulingUnitTemplateSerializer(RelationalHyperlinkedModelSerializer):
     class Meta:
         model = models.SchedulingUnitTemplate
diff --git a/SAS/TMSS/src/tmss/tmssapp/viewsets/specification.py b/SAS/TMSS/src/tmss/tmssapp/viewsets/specification.py
index aa83ab8741c..7d37f660191 100644
--- a/SAS/TMSS/src/tmss/tmssapp/viewsets/specification.py
+++ b/SAS/TMSS/src/tmss/tmssapp/viewsets/specification.py
@@ -50,6 +50,21 @@ class DefaultGeneratorTemplateViewSet(LOFARViewSet):
     queryset = models.DefaultGeneratorTemplate.objects.all()
     serializer_class = serializers.DefaultGeneratorTemplateSerializer
 
+
+class SchedulingUnitObservingStrategyTemplateViewSet(LOFARViewSet):
+    queryset = models.SchedulingUnitObservingStrategyTemplate.objects.all()
+    serializer_class = serializers.SchedulingUnitObservingStrategyTemplateSerializer
+
+    @swagger_auto_schema(responses={200: 'JSON object with all the defaults from the schema filled in',
+                                    403: 'forbidden'},
+                         operation_description="Get a JSON object with all the defaults from the schema filled in.")
+    @action(methods=['get'], detail=True)
+    def default_specification(self, request, pk=None):
+        template = get_object_or_404(models.SchedulingUnitObservingStrategyTemplate, pk=pk)
+        spec = get_default_json_object_for_schema(template.schema)
+        return JsonResponse(spec)
+
+
 class SchedulingUnitTemplateFilter(filters.FilterSet):
     class Meta:
         model = models.SchedulingUnitTemplate
diff --git a/SAS/TMSS/src/tmss/urls.py b/SAS/TMSS/src/tmss/urls.py
index 6f9ea2167eb..66957eeaa41 100644
--- a/SAS/TMSS/src/tmss/urls.py
+++ b/SAS/TMSS/src/tmss/urls.py
@@ -93,6 +93,7 @@ router.register(r'quantity', viewsets.QuantityViewSet)
 
 # templates
 router.register(r'generator_template', viewsets.GeneratorTemplateViewSet)
+router.register(r'scheduling_unit_observing_strategy_template', viewsets.SchedulingUnitObservingStrategyTemplateViewSet)
 router.register(r'scheduling_unit_template', viewsets.SchedulingUnitTemplateViewSet)
 router.register(r'task_template', viewsets.TaskTemplateViewSet)
 router.register(r'task_relation_selection_template', viewsets.TaskRelationSelectionTemplateViewSet)
-- 
GitLab