From 52d9276c26aa0c28d5525eca93c0ba30184ebbbb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=B6rn=20K=C3=BCnsem=C3=B6ller?=
 <jkuensem@physik.uni-bielefeld.de>
Date: Tue, 27 Apr 2021 20:45:42 +0200
Subject: [PATCH] TMSS-697: debug

---
 .../src/tmss/tmssapp/models/specification.py  | 25 ++++++++++++++++---
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py b/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py
index cbf04a81f2d..345ac8b1059 100644
--- a/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py
+++ b/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py
@@ -17,7 +17,7 @@ from django.core.exceptions import ValidationError
 import datetime
 from collections import Counter
 from django.utils.functional import cached_property
-
+from lofar.sas.tmss.tmss.exceptions import TMSSException
 
 #
 # Mixins
@@ -497,6 +497,14 @@ class SchedulingUnitBlueprint(RefreshFromDbInvalidatesCachedPropertiesMixin, Tem
     scheduling_constraints_doc = JSONField(help_text='Scheduling Constraints for this run.', null=True)
     scheduling_constraints_template = ForeignKey('SchedulingConstraintsTemplate', on_delete=CASCADE, null=True, help_text='Schema used for scheduling_constraints_doc.')
 
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+
+        # keep original scheduling constraints to detect changes on save
+        # todo: make this work for constraints_doc as well:
+        # self.__original_scheduling_constraints_doc = self.scheduling_constraints_doc
+        self.__original_scheduling_constraints_template_id = self.scheduling_constraints_template_id
+
     def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
         self.annotate_validate_add_defaults_to_doc_using_template('requirements_doc', 'requirements_template')
 
@@ -507,9 +515,18 @@ class SchedulingUnitBlueprint(RefreshFromDbInvalidatesCachedPropertiesMixin, Tem
                 if getattr(self, copy_field) is None and hasattr(self, 'draft'):
                     setattr(self, copy_field, getattr(self.draft, copy_field))
         else:
-            # On updates, prevent changing the scheduling contraints doc or template
-            if self.status != SchedulingUnitBlueprint.Status.SCHEDULABLE.value and (self.scheduling_constraints_doc or self.scheduling_constraints_doc):
-                raise ProtectedError('The scheduling constraints of SchedulingUnitBlueprint pk=%s status=%s cannot be updated since is is not in schedulable state.' % (self.pk, self.status))
+            # On updates, prevent changing the scheduling contraints doc or template if we are past schedulable state
+            if self.status not in [SchedulingUnitBlueprint.Status.DEFINED.value, SchedulingUnitBlueprint.Status.SCHEDULABLE.value] and \
+                    self.scheduling_constraints_template_id != self.__original_scheduling_constraints_template_id:
+                    # todo: make this work for constraints_doc as well:
+                    #(self.scheduling_constraints_doc != self.__original_scheduling_constraints_doc or
+                    # self.scheduling_constraints_template_id != self.__original_scheduling_constraints_template_id):
+                raise TMSSException('The scheduling constraints of SchedulingUnitBlueprint pk=%s status=%s cannot be updated since it is not in defined or schedulable state.' % (self.pk, self.status))
+
+            # update the original constraints value for comparison on next save
+            # todo: make this work for constraints_doc as well:
+            # self.__original_scheduling_constraints_doc = self.scheduling_constraints_doc
+            self.__original_scheduling_constraints_template_id = self.scheduling_constraints_template_id
 
         super().save(force_insert, force_update, using, update_fields)
 
-- 
GitLab