From 72ca76fc11ce4e6eb835d272bf8f7e070e1e6774 Mon Sep 17 00:00:00 2001
From: jkuensem <jkuensem@physik.uni-bielefeld.de>
Date: Tue, 14 Jul 2020 23:29:41 +0200
Subject: [PATCH] TMSS-241: Add viewsets, serializers for categories

---
 .../src/tmss/tmssapp/migrations/0001_initial.py    |  4 ++--
 SAS/TMSS/src/tmss/tmssapp/models/specification.py  | 14 +++++---------
 .../src/tmss/tmssapp/serializers/specification.py  | 12 ++++++++++++
 .../src/tmss/tmssapp/viewsets/specification.py     | 10 ++++++++++
 SAS/TMSS/src/tmss/urls.py                          |  2 ++
 5 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/SAS/TMSS/src/tmss/tmssapp/migrations/0001_initial.py b/SAS/TMSS/src/tmss/tmssapp/migrations/0001_initial.py
index 16cd0f81dbb..9b01a3da93f 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 3.0.6 on 2020-07-14 21:08
+# Generated by Django 3.0.6 on 2020-07-14 21:21
 
 from django.conf import settings
 import django.contrib.postgres.fields
@@ -1019,7 +1019,7 @@ class Migration(migrations.Migration):
         migrations.AddField(
             model_name='project',
             name='cycles',
-            field=models.ManyToManyField(help_text='Cycles to which this project belongs (NULLable).', null=True, related_name='projects', to='tmssapp.Cycle'),
+            field=models.ManyToManyField(help_text='Cycles to which this project belongs (NULLable).', related_name='projects', to='tmssapp.Cycle'),
         ),
         migrations.AddField(
             model_name='project',
diff --git a/SAS/TMSS/src/tmss/tmssapp/models/specification.py b/SAS/TMSS/src/tmss/tmssapp/models/specification.py
index 8a31ff69fd6..d5a83d89710 100644
--- a/SAS/TMSS/src/tmss/tmssapp/models/specification.py
+++ b/SAS/TMSS/src/tmss/tmssapp/models/specification.py
@@ -155,9 +155,8 @@ class PeriodCategory(AbstractChoice):
         The items in the Choices class below are automagically populated into the database via a data migration."""
 
     class Choices(Enum):
-        SINGLE = "single"
-        LONG = "long"
-        TERM = "term"
+        SINGLE_CYCLE = "single_cycle"
+        LONG_TERM = "long_term"
         UNBOUNDED = "unbounded"
 
 
@@ -167,11 +166,9 @@ class ProjectCategory(AbstractChoice):
 
     class Choices(Enum):
         REGULAR = "regular"
-        USER = "user"
-        SHARED = "shared"
-        SUPPORT = "support"
+        USER_SHARED_SUPPORT = "user_shared_support"
         COMMISSIONING = "commissioning"
-        DOT = "dot"
+        DOT = "ddt"
         TEST = "test"
 
 
@@ -279,8 +276,7 @@ class Cycle(NamedCommonPK):
 
 
 class Project(NamedCommonPK):
-    # todo: cycles should be protected since we have to manually decide to clean up projects with a cycle or keep them without cycle, however, ManyToManyField does not allow for that
-    cycles = ManyToManyField('Cycle', related_name='projects', null=True, help_text='Cycles to which this project belongs (NULLable).')
+    cycles = ManyToManyField('Cycle', related_name='projects', help_text='Cycles to which this project belongs (NULLable).')
     priority_rank = FloatField(null=False, help_text='Priority of this project w.r.t. other projects. Projects can interrupt observations of lower-priority projects.') # todo: add if needed: validators=[MinValueValidator(0.0), MaxValueValidator(1.0)]
     trigger_priority = IntegerField(default=1000, help_text='Priority of this project w.r.t. triggers.') # todo: verify meaning and add to help_text: "Triggers with higher priority than this threshold can interrupt observations of projects."
     can_trigger = BooleanField(default=False, help_text='True if this project is allowed to supply observation requests on the fly, possibly interrupting currently running observations (responsive telescope).')
diff --git a/SAS/TMSS/src/tmss/tmssapp/serializers/specification.py b/SAS/TMSS/src/tmss/tmssapp/serializers/specification.py
index 90152f462b3..3a3a3fa3f8b 100644
--- a/SAS/TMSS/src/tmss/tmssapp/serializers/specification.py
+++ b/SAS/TMSS/src/tmss/tmssapp/serializers/specification.py
@@ -212,6 +212,18 @@ class SettingSerializer(serializers.ModelSerializer):
         fields = '__all__'
 
 
+class ProjectCategorySerializer(serializers.ModelSerializer):
+    class Meta:
+        model = models.ProjectCategory
+        fields = '__all__'
+
+
+class PeriodCategorySerializer(serializers.ModelSerializer):
+    class Meta:
+        model = models.PeriodCategory
+        fields = '__all__'
+
+
 class SchedulingSetSerializer(RelationalHyperlinkedModelSerializer):
 
     # Create a JSON editor form to replace the simple text field based on the schema in the template that this
diff --git a/SAS/TMSS/src/tmss/tmssapp/viewsets/specification.py b/SAS/TMSS/src/tmss/tmssapp/viewsets/specification.py
index 93ab6971734..5b9443860cf 100644
--- a/SAS/TMSS/src/tmss/tmssapp/viewsets/specification.py
+++ b/SAS/TMSS/src/tmss/tmssapp/viewsets/specification.py
@@ -208,6 +208,16 @@ class SettingViewSet(LOFARViewSet):
     serializer_class = serializers.SettingSerializer
 
 
+class PeriodCategoryViewSet(LOFARViewSet):
+    queryset = models.PeriodCategory.objects.all()
+    serializer_class = serializers.PeriodCategorySerializer
+
+
+class ProjectCategoryViewSet(LOFARViewSet):
+    queryset = models.ProjectCategory.objects.all()
+    serializer_class = serializers.ProjectCategorySerializer
+
+
 class SchedulingUnitDraftViewSet(LOFARViewSet):
     queryset = models.SchedulingUnitDraft.objects.all()
     serializer_class = serializers.SchedulingUnitDraftSerializer
diff --git a/SAS/TMSS/src/tmss/urls.py b/SAS/TMSS/src/tmss/urls.py
index 6dda70922b4..ba637bdea81 100644
--- a/SAS/TMSS/src/tmss/urls.py
+++ b/SAS/TMSS/src/tmss/urls.py
@@ -87,6 +87,8 @@ router.register(r'datatype', viewsets.DatatypeViewSet)
 router.register(r'dataformat', viewsets.DataformatViewSet)
 router.register(r'copy_reason', viewsets.CopyReasonViewSet)
 router.register(r'flag', viewsets.FlagViewSet)
+router.register(r'period_category', viewsets.PeriodCategoryViewSet)
+router.register(r'project_category', viewsets.ProjectCategoryViewSet)
 
 # templates
 router.register(r'generator_template', viewsets.GeneratorTemplateViewSet)
-- 
GitLab