From 32c47bdaa6f0e07ff37366b68e8cf4a4634911f3 Mon Sep 17 00:00:00 2001
From: Jorrit Schaap <schaap@astron.nl>
Date: Fri, 21 Jul 2023 13:22:46 +0200
Subject: [PATCH] non-triggered dynamically scheduled units are not allowed to
 unschedule fixed_time scheduled units

---
 .../services/scheduling/lib/dynamic_scheduling.py  |  4 ++++
 .../src/tmss/tmssapp/models/specification.py       | 14 ++++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/SAS/TMSS/backend/services/scheduling/lib/dynamic_scheduling.py b/SAS/TMSS/backend/services/scheduling/lib/dynamic_scheduling.py
index 308e66981d7..0a44999f5a3 100644
--- a/SAS/TMSS/backend/services/scheduling/lib/dynamic_scheduling.py
+++ b/SAS/TMSS/backend/services/scheduling/lib/dynamic_scheduling.py
@@ -1163,6 +1163,10 @@ def unschededule_blocking_scheduled_units_if_needed_and_possible(candidate_sched
     if candidate_scheduling_unit.priority_queue.value == models.PriorityQueueType.Choices.B.value:
         overlapping_scheduled_scheduling_units = overlapping_scheduled_scheduling_units.exclude(priority_queue__value=models.PriorityQueueType.Choices.A.value)
 
+    # non-triggered dynamically scheduled units are not allowed to unschedule fixed_time scheduled units
+    if candidate_scheduling_unit.is_dynamically_scheduled and not candidate_scheduling_unit.interrupts_telescope:
+        overlapping_scheduled_scheduling_units = overlapping_scheduled_scheduling_units.exclude(scheduling_constraints_doc__scheduler='fixed_time')
+
     if not overlapping_scheduled_scheduling_units.exists():
         logger.debug('no scheduled scheduling_units are blocking candidate scheduling_unit id=%s name=%s, nothing needs to be unscheduled', candidate_scheduling_unit.id, candidate_scheduling_unit.name)
         return
diff --git a/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py b/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py
index 1b0ba4d358e..35e6bc0ce1d 100644
--- a/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py
+++ b/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py
@@ -1368,6 +1368,20 @@ class SchedulingUnitBlueprint(ProjectPropertyMixin, TemplateSchemaMixin, Schedul
         '''
         return self.draft.scheduling_set.project
 
+    @property
+    def is_dynamically_scheduled(self) -> bool:
+        '''is this unit scheduled dynamically?'''
+        if self.scheduling_constraints_doc is not None:
+            return self.scheduling_constraints_doc.get('scheduler', '') == 'dynamic'
+        return False
+
+    @property
+    def is_fixed_time_scheduled(self) -> bool:
+        '''is this unit scheduled at fixed time?'''
+        if self.scheduling_constraints_doc is not None:
+            return self.scheduling_constraints_doc.get('scheduler', '') == 'fixed_time'
+        return False
+
     @property
     def main_observation_used_stations(self) -> list:
         '''return the list of used stations of the main observation task'''
-- 
GitLab