From 959abd5849a4259fd49a85ceea4a3a31aeb17d1a Mon Sep 17 00:00:00 2001
From: Jorrit Schaap <schaap@astron.nl>
Date: Thu, 27 Aug 2020 16:52:15 +0200
Subject: [PATCH] TMSS-272: check for None

---
 .../src/tmss/tmssapp/models/scheduling.py     |  8 +--
 .../src/tmss/tmssapp/models/specification.py  | 63 +++----------------
 2 files changed, 11 insertions(+), 60 deletions(-)

diff --git a/SAS/TMSS/src/tmss/tmssapp/models/scheduling.py b/SAS/TMSS/src/tmss/tmssapp/models/scheduling.py
index e869c80cc92..6810c743be9 100644
--- a/SAS/TMSS/src/tmss/tmssapp/models/scheduling.py
+++ b/SAS/TMSS/src/tmss/tmssapp/models/scheduling.py
@@ -198,7 +198,7 @@ class Subtask(BasicCommon):
     def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
         creating = self._state.adding  # True on create, False on update
 
-        if self.specifications_doc and self.specifications_template_id and self.specifications_template.schema:
+        if self.specifications_doc is not None and self.specifications_template_id and self.specifications_template.schema:
             validate_json_against_schema(self.specifications_doc, self.specifications_template.schema)
 
         if self.state.value == SubtaskState.Choices.SCHEDULED.value and self.__original_state.value == SubtaskState.Choices.SCHEDULING.value:
@@ -257,7 +257,7 @@ class SubtaskInput(BasicCommon):
     selection_template = ForeignKey('TaskRelationSelectionTemplate', on_delete=PROTECT, help_text='Schema used for selection_doc.')
 
     def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
-        if self.selection_doc and self.selection_template_id and self.selection_template.schema:
+        if self.selection_doc is not None and self.selection_template_id and self.selection_template.schema is not None :
             validate_json_against_schema(self.selection_doc, self.selection_template.schema)
 
         super().save(force_insert, force_update, using, update_fields)
@@ -289,10 +289,10 @@ class Dataproduct(BasicCommon):
     feedback_template = ForeignKey('DataproductFeedbackTemplate', on_delete=PROTECT, help_text='Schema used for feedback_doc.')
 
     def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
-        if self.specifications_doc and self.specifications_template_id and self.specifications_template.schema:
+        if self.specifications_doc is not None and self.specifications_template_id and self.specifications_template.schema is not None:
             validate_json_against_schema(self.specifications_doc, self.specifications_template.schema)
 
-        if self.feedback_doc and self.feedback_template_id and self.feedback_template.schema:
+        if self.feedback_doc is not None and self.feedback_template_id and self.feedback_template.schema is not None:
             validate_json_against_schema(self.feedback_doc, self.feedback_template.schema)
 
         super().save(force_insert, force_update, using, update_fields)
diff --git a/SAS/TMSS/src/tmss/tmssapp/models/specification.py b/SAS/TMSS/src/tmss/tmssapp/models/specification.py
index 91713a775ae..1af4be38df2 100644
--- a/SAS/TMSS/src/tmss/tmssapp/models/specification.py
+++ b/SAS/TMSS/src/tmss/tmssapp/models/specification.py
@@ -220,55 +220,6 @@ class Template(NamedCommon):
         # TODO: remove all <class>_unique_name_version UniqueConstraint's from the subclasses and replace by this line below when we start using django 3.0
         # constraints = [UniqueConstraint(fields=['name', 'version'], name='%(class)s_unique_name_version')]
 
-    @staticmethod
-    def update_tmss_common_json_schema_refs(schema, base_url: str=None):
-        '''return the given schema with all $ref fields updated so they point to the given base_url'''
-        if base_url is None:
-            # assume tmms is running locally
-            base_url = 'http://localhost:8000'
-
-        if isinstance(schema, dict):
-            updated_schema = {}
-            for key, value in schema.items():
-                if key == "$ref":
-                    if value.startswith('#'):
-                        # reference to local document, no need for http injection
-                        updated_schema[key] = value
-                    else:
-                        try:
-                            # deduct referred schema name and version from ref-value
-                            head, hash, tail = value.partition('#')
-                            head_parts = head.rstrip('/').split('/')
-                            schema_name = head_parts[-2]
-                            schema_version = head_parts[-1]
-                            tail = hash+tail
-
-                            # construct the common json schema path for this ref
-                            schema_path = reverse_path('common_json_schema', kwargs={'name': schema_name, 'version': schema_version})
-
-                            # and construct the proper ref url
-                            updated_schema[key] = base_url + schema_path + tail
-                        except:
-                            # aparently the reference is not conform the expected lofar common json schema path...
-                            # so, just accept the original value and assume that the user uploaded a proper schema
-                            updated_schema[key] = value
-                elif isinstance(value, dict):
-                    updated_schema[key] = Template.update_tmss_common_json_schema_refs(value, base_url)
-                elif isinstance(value, list):
-                    updated_schema[key] = [Template.update_tmss_common_json_schema_refs(item, base_url) for item in value]
-                else:
-                    updated_schema[key] = value
-            return updated_schema
-
-        if isinstance(schema, list):
-            return [Template.update_tmss_common_json_schema_refs(item, base_url) for item in schema]
-
-        return schema
-
-    def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
-        self.schema = Template.update_tmss_common_json_schema_refs(self.schema)
-        super().save(force_insert, force_update, using, update_fields)
-
 
 # concrete models
 
@@ -408,7 +359,7 @@ class SchedulingSet(NamedCommon):
     project = ForeignKey('Project', related_name="scheduling_sets", on_delete=PROTECT, help_text='Project to which this scheduling set belongs.')  # protected to avoid accidents
 
     def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
-        if self.generator_doc and self.generator_template_id and self.generator_template.schema:
+        if self.generator_doc is not None and self.generator_template_id and self.generator_template.schema is not None:
             validate_json_against_schema(self.generator_doc, self.generator_template.schema)
 
         super().save(force_insert, force_update, using, update_fields)
@@ -423,7 +374,7 @@ class SchedulingUnitDraft(NamedCommon):
     requirements_template = ForeignKey('SchedulingUnitTemplate', on_delete=CASCADE, help_text='Schema used for requirements_doc.') # todo: 'schema'?
 
     def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
-        if self.requirements_doc and self.requirements_template_id and self.requirements_template.schema:
+        if self.requirements_doc is not None and self.requirements_template_id and self.requirements_template.schema is not None:
             validate_json_against_schema(self.requirements_doc, self.requirements_template.schema)
 
         super().save(force_insert, force_update, using, update_fields)
@@ -462,7 +413,7 @@ class SchedulingUnitBlueprint(NamedCommon):
     draft = ForeignKey('SchedulingUnitDraft', related_name='scheduling_unit_blueprints', on_delete=CASCADE, help_text='Scheduling Unit Draft which this run instantiates.')
 
     def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
-        if self.requirements_doc and self.requirements_template_id and self.requirements_template.schema:
+        if self.requirements_doc is not None and self.requirements_template_id and self.requirements_template.schema is not None:
             validate_json_against_schema(self.requirements_doc, self.requirements_template.schema)
 
         super().save(force_insert, force_update, using, update_fields)
@@ -525,7 +476,7 @@ class TaskDraft(NamedCommon):
     specifications_template = ForeignKey('TaskTemplate', on_delete=CASCADE, help_text='Schema used for requirements_doc.') # todo: 'schema'?
 
     def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
-        if self.specifications_doc and self.specifications_template_id and self.specifications_template.schema:
+        if self.specifications_doc is not None and self.specifications_template_id and self.specifications_template.schema is not None:
             validate_json_against_schema(self.specifications_doc, self.specifications_template.schema)
 
         super().save(force_insert, force_update, using, update_fields)
@@ -641,7 +592,7 @@ class TaskBlueprint(NamedCommon):
     scheduling_unit_blueprint = ForeignKey('SchedulingUnitBlueprint', related_name='task_blueprints', on_delete=CASCADE, help_text='Scheduling Unit Blueprint to which this task belongs.')
 
     def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
-        if self.specifications_doc and self.specifications_template_id and self.specifications_template.schema:
+        if self.specifications_doc is not None and self.specifications_template_id and self.specifications_template.schema is not None:
             validate_json_against_schema(self.specifications_doc, self.specifications_template.schema)
 
         super().save(force_insert, force_update, using, update_fields)
@@ -743,7 +694,7 @@ class TaskRelationDraft(BasicCommon):
     output_role = ForeignKey('TaskConnectorType', related_name='taskrelationdraft_output_roles', on_delete=CASCADE, help_text='Output connector type (what kind of data can be created as output).')
 
     def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
-        if self.selection_doc and self.selection_template_id and self.selection_template.schema:
+        if self.selection_doc is not None and self.selection_template_id and self.selection_template.schema is not None:
             validate_json_against_schema(self.selection_doc, self.selection_template.schema)
 
         super().save(force_insert, force_update, using, update_fields)
@@ -764,7 +715,7 @@ class TaskRelationBlueprint(BasicCommon):
     selection_template = ForeignKey('TaskRelationSelectionTemplate', on_delete=CASCADE, help_text='Schema used for selection_doc.')  # todo: 'schema'?
 
     def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
-        if self.selection_doc and self.selection_template_id and self.selection_template.schema:
+        if self.selection_doc is not None and self.selection_template_id and self.selection_template.schema is not None:
             validate_json_against_schema(self.selection_doc, self.selection_template.schema)
 
         super().save(force_insert, force_update, using, update_fields)
-- 
GitLab