From b23d14b0afb871dc99afa557cd76b73ee7f83428 Mon Sep 17 00:00:00 2001
From: Jorrit Schaap <schaap@astron.nl>
Date: Mon, 17 Aug 2020 17:45:09 +0200
Subject: [PATCH] TMSS-215: added
 _populate_scheduling_unit_observation_strategry_schema, and adapted populate
 code for scheduling unit for new schema

---
 SAS/TMSS/src/tmss/tmssapp/populate.py | 77 ++++++++++++++++-----------
 1 file changed, 45 insertions(+), 32 deletions(-)

diff --git a/SAS/TMSS/src/tmss/tmssapp/populate.py b/SAS/TMSS/src/tmss/tmssapp/populate.py
index c2ebe1653ce..7cf30fe4da7 100644
--- a/SAS/TMSS/src/tmss/tmssapp/populate.py
+++ b/SAS/TMSS/src/tmss/tmssapp/populate.py
@@ -47,6 +47,8 @@ def populate_settings(apps, schema_editor):
 def populate_lofar_json_schemas(apps, schema_editor):
 
     _populate_scheduling_unit_schema()
+    _populate_scheduling_unit_observation_strategry_schema()
+
     # populate task schema's
     _populate_preprocessing_schema()
     _populate_observation_with_stations_schema()
@@ -78,59 +80,59 @@ def populate_test_data():
 
             # create a Test Scheduling Set UC1 under project TMSS-Commissioning
             tmss_project = models.Project.objects.get(name="TMSS-Commissioning")
-            for set_nr in range(3):
+            for set_nr in range(1):
                 scheduling_set_data = SchedulingSet_test_data(name="Test Scheduling Set UC1 example %s" % (set_nr,), project=tmss_project)
                 scheduling_set = models.SchedulingSet.objects.create(**scheduling_set_data)
                 scheduling_set.tags = ["TEST"]
                 scheduling_set.save()
 
-                for unit_nr in range(3):
+                for unit_nr in range(1):
                     # construct a scheduling_unit_doc, i.e.: a specification of interrelated tasks which conforms the scheduling unit schema
                     # by default, this scheduling_unit_doc holds no tasks, so lets setup the UC1 sequence of tasks here, and add it to the scheduling_unit_doc
                     scheduling_unit_template = models.SchedulingUnitTemplate.objects.get(name="scheduling unit schema")
                     scheduling_unit_doc = get_default_json_object_for_schema(scheduling_unit_template.schema)
 
                     # create and add a calibrator task spec
-                    scheduling_unit_doc['tasks'].append({"name": "Calibrator Observation 1",
-                                                         "description": "Calibrator Observation for UC1 HBA scheduling unit",
-                                                         "specifications_doc": get_default_json_object_for_schema(models.TaskTemplate.objects.get(name="calibrator schema").schema),
-                                                         "specifications_template": "calibrator schema"})
+                    scheduling_unit_doc['tasks']["Calibrator Observation 1"] = {
+                        "description": "Calibrator Observation for UC1 HBA scheduling unit",
+                        "specifications_doc": get_default_json_object_for_schema(models.TaskTemplate.objects.get(name="calibrator schema").schema),
+                        "specifications_template": "calibrator schema"}
 
                     # create and add a calibrator preprocessing spec
-                    scheduling_unit_doc['tasks'].append({"name": "Pipeline Calibrator1",
-                                                         "description": "Preprocessing Pipeline for Calibrator Observation 1",
-                                                         "specifications_doc": get_default_json_object_for_schema(models.TaskTemplate.objects.get(name="preprocessing schema").schema),
-                                                         "specifications_template": "preprocessing schema"})
+                    scheduling_unit_doc['tasks']["Pipeline Calibrator1"] = {
+                        "description": "Preprocessing Pipeline for Calibrator Observation 1",
+                        "specifications_doc": get_default_json_object_for_schema(models.TaskTemplate.objects.get(name="preprocessing schema").schema),
+                        "specifications_template": "preprocessing schema"}
 
                     # create and add a target obs spec
-                    scheduling_unit_doc['tasks'].append({"name": "Target Observation",
-                                                         "description": "Target Observation for UC1 HBA scheduling unit",
-                                                         "specifications_doc": get_default_json_object_for_schema(models.TaskTemplate.objects.get(name="observation schema").schema),
-                                                         "specifications_template": "observation schema"})
+                    scheduling_unit_doc['tasks']["Target Observation"] = {
+                        "description": "Target Observation for UC1 HBA scheduling unit",
+                        "specifications_doc": get_default_json_object_for_schema(models.TaskTemplate.objects.get(name="observation schema").schema),
+                        "specifications_template": "observation schema"}
 
                     # create and add a target pipeline spec for sap0
-                    scheduling_unit_doc['tasks'].append({"name": "Preprocessing Pipeline SAP0",
-                                                         "description": "Preprocessing Pipeline for Target Observation SAP0",
-                                                         "specifications_doc": get_default_json_object_for_schema(models.TaskTemplate.objects.get(name="preprocessing schema").schema),
-                                                         "specifications_template": "preprocessing schema"})
+                    scheduling_unit_doc['tasks']["Preprocessing Pipeline SAP0"] = {
+                        "description": "Preprocessing Pipeline for Target Observation SAP0",
+                        "specifications_doc": get_default_json_object_for_schema(models.TaskTemplate.objects.get(name="preprocessing schema").schema),
+                        "specifications_template": "preprocessing schema"}
 
                     # create and add a target pipeline spec for sap1
-                    scheduling_unit_doc['tasks'].append({"name": "Preprocessing Pipeline SAP1",
-                                                         "description": "Preprocessing Pipeline for Target Observation SAP1",
-                                                         "specifications_doc": get_default_json_object_for_schema(models.TaskTemplate.objects.get(name="preprocessing schema").schema),
-                                                         "specifications_template": "preprocessing schema"})
+                    scheduling_unit_doc['tasks']["Preprocessing Pipeline SAP1"] = {
+                        "description": "Preprocessing Pipeline for Target Observation SAP1",
+                        "specifications_doc": get_default_json_object_for_schema(models.TaskTemplate.objects.get(name="preprocessing schema").schema),
+                        "specifications_template": "preprocessing schema"}
 
                     # create and add a calibrator task spec
-                    scheduling_unit_doc['tasks'].append({"name": "Calibrator Observation 2",
-                                                         "description": "Calibrator Observation for UC1 HBA scheduling unit",
-                                                         "specifications_doc": get_default_json_object_for_schema(models.TaskTemplate.objects.get(name="calibrator schema").schema),
-                                                         "specifications_template": "calibrator schema"})
+                    scheduling_unit_doc['tasks']["Calibrator Observation 2"] = {
+                        "description": "Calibrator Observation for UC1 HBA scheduling unit",
+                        "specifications_doc": get_default_json_object_for_schema(models.TaskTemplate.objects.get(name="calibrator schema").schema),
+                        "specifications_template": "calibrator schema"}
 
                     # create and add a calibrator preprocessing spec
-                    scheduling_unit_doc['tasks'].append({"name": "Pipeline Calibrator2",
-                                                         "description": "Preprocessing Pipeline for Calibrator Observation 2",
-                                                         "specifications_doc": get_default_json_object_for_schema(models.TaskTemplate.objects.get(name="preprocessing schema").schema),
-                                                         "specifications_template": "preprocessing schema"})
+                    scheduling_unit_doc['tasks']["Pipeline Calibrator2"] = {
+                        "description": "Preprocessing Pipeline for Calibrator Observation 2",
+                        "specifications_doc": get_default_json_object_for_schema(models.TaskTemplate.objects.get(name="preprocessing schema").schema),
+                        "specifications_template": "preprocessing schema"}
 
                     # ----- end of tasks
 
@@ -194,7 +196,7 @@ def populate_test_data():
                         else:
                             create_task_blueprints_and_subtasks_from_scheduling_unit_draft(scheduling_unit_draft)
                     except TMSSException as e:
-                        logger.error(e)
+                        logger.exception(e)
 
     except ImportError:
         pass
@@ -263,11 +265,22 @@ def _populate_scheduling_unit_schema():
         scheduling_unit_template_data = {"name": "scheduling unit schema",
                                          "description": 'Schema for scheduling unit',
                                          "version": '0.1',
-                                         "tags": ["UC1"],
+                                         "tags": [],
                                          "schema": json_data}
     SchedulingUnitTemplate.objects.create(**scheduling_unit_template_data)
 
 
+def _populate_scheduling_unit_observation_strategry_schema():
+    with open(os.path.join(working_dir, "schemas/UC1-scheduling-unit-observation-strategy.json")) as json_file:
+        json_data = json.loads(json_file.read())
+        template_data = {"name": "UC1 observation strategy template",
+                         "description": 'UC1 observation strategy template',
+                         "version": '0.1',
+                         "tags": ["UC1"],
+                         "schema": json_data}
+    SchedulingUnitObservingStrategyTemplate.objects.create(**template_data)
+
+
 def _populate_observation_with_stations_schema():
     with open(os.path.join(working_dir, "schemas/task-observation-with-stations.json")) as json_file:
         json_data = json.loads(json_file.read())
-- 
GitLab