From 7c8db60162c27dc26d961f1b9b467c4adff6dd5e Mon Sep 17 00:00:00 2001
From: Jorrit Schaap <schaap@astron.nl>
Date: Tue, 28 Nov 2023 17:34:15 +0100
Subject: [PATCH] TMSS-2836: prevent duplicate copies

---
 SAS/TMSS/backend/src/tmss/tmssapp/tasks.py | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/SAS/TMSS/backend/src/tmss/tmssapp/tasks.py b/SAS/TMSS/backend/src/tmss/tmssapp/tasks.py
index 8fa026de527..192ec53f2fa 100644
--- a/SAS/TMSS/backend/src/tmss/tmssapp/tasks.py
+++ b/SAS/TMSS/backend/src/tmss/tmssapp/tasks.py
@@ -184,8 +184,8 @@ def create_scheduling_unit_draft_from_scheduling_unit_blueprint(scheduling_unit_
     logger.debug("create_scheduling_unit_draft_from_scheduling_unit_blueprint(scheduling_unit_blueprint.id=%s)", scheduling_unit_blueprint.pk)
 
     with transaction.atomic():
-        scheduling_unit_draft_copy = models.SchedulingUnitDraft.objects.create(name="%s (Copy from blueprint)" % (scheduling_unit_blueprint.name,),
-                                                                               description="%s (Copy from blueprint '%s' id=%s)" % (scheduling_unit_blueprint.description or "<no description>", scheduling_unit_blueprint.name,scheduling_unit_blueprint.id),
+        scheduling_unit_draft_copy = models.SchedulingUnitDraft.objects.create(name="%s (Copy)" % (scheduling_unit_blueprint.name,)[:128],
+                                                                               description="%s (Copy from blueprint id=%s)" % (scheduling_unit_blueprint.description or "<no description>", scheduling_unit_blueprint.id)[:256],
                                                                                scheduling_set=scheduling_unit_blueprint.draft.scheduling_set,
                                                                                observation_strategy_template=scheduling_unit_blueprint.draft.observation_strategy_template,
                                                                                specifications_template=scheduling_unit_blueprint.specifications_template,
@@ -574,8 +574,19 @@ def create_scheduling_unit_blueprint_and_tasks_and_subtasks_from_scheduling_unit
 def create_lofar2_sibling_scheduling_unit_draft_and_blueprint(scheduling_unit_blueprint: models.SchedulingUnitBlueprint) -> models.SchedulingUnitBlueprint:
     '''Convenience method: Create a new sibling scheduling_unit_blueprint (and draft), which is a copy of the given scheduling_unit_blueprint but with lofar2 stations, and scheduled in parallel with the original'''
     with transaction.atomic():
+        # get the current lofar2 stations
+        from lofar.sas.tmss.tmss.tmssapp.conversions import get_lofar2_stations
+        lofar2_stations = set(get_lofar2_stations())
+
+        # do not create a sibling for a unit that is already using lofar2 stations
+        for obs_task in scheduling_unit_blueprint.observation_tasks.all():
+            specified_station_groups = obs_task.specifications_doc.get('station_configuration', {}).get('station_groups', [])
+            specified_stations = set(sum([group['stations'] for group in specified_station_groups], []))
+            if lofar2_stations == specified_stations:
+                raise BlueprintCreationException("Cannot create a Lofar2 sibling for scheduling unit id=%s because it is already using Lofar2 stations" % (scheduling_unit_blueprint.id,))
+
         scheduling_unit_draft = create_scheduling_unit_draft_from_scheduling_unit_blueprint(scheduling_unit_blueprint)
-        scheduling_unit_draft.name = scheduling_unit_draft.name.replace("(Copy from blueprint)", "(Lofar2 sibling)")
+        scheduling_unit_draft.name = scheduling_unit_draft.name.replace("(Copy)", "(Lofar2 sibling)")
 
         # let the commissioners decide if data is to be ingested
         scheduling_unit_draft.ingest_permission_required = True
@@ -597,11 +608,8 @@ def create_lofar2_sibling_scheduling_unit_draft_and_blueprint(scheduling_unit_bl
         scheduling_unit_draft.scheduling_constraints_doc['time_offset'] = 0
         scheduling_unit_draft.save()
 
-        # get the current lofar2 stations
-        from lofar.sas.tmss.tmss.tmssapp.conversions import get_lofar2_stations
-        lofar2_stations = sorted(list(get_lofar2_stations()))
-
         # overwrite any list of original station_groups with the lofar2 station group
+        lofar2_stations = sorted(list(lofar2_stations))
         for obs_task in scheduling_unit_draft.observation_tasks.all():
             obs_task.specifications_doc['station_configuration']['station_groups'] = [{'stations': lofar2_stations,
                                                                                        'max_nr_missing': max(0, len(lofar2_stations)-1)
-- 
GitLab