From 3868c7d3994eb0e716345119fe3398c94c1310ca Mon Sep 17 00:00:00 2001
From: goei <JsXLRu>
Date: Mon, 2 Nov 2020 13:52:54 +0100
Subject: [PATCH] TMSS-337 Remove 'default' value in SchedulingUnitBlueprint
 and add propertyobserved_end_time in TaskBlueprint

---
 .../src/tmss/tmssapp/models/specification.py     | 16 +++++++++++++++-
 .../tmss/tmssapp/serializers/specification.py    |  2 +-
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/SAS/TMSS/src/tmss/tmssapp/models/specification.py b/SAS/TMSS/src/tmss/tmssapp/models/specification.py
index 353f7a16ea0..d76320d6142 100644
--- a/SAS/TMSS/src/tmss/tmssapp/models/specification.py
+++ b/SAS/TMSS/src/tmss/tmssapp/models/specification.py
@@ -660,6 +660,20 @@ class SchedulingUnitBlueprint(NamedCommon):
         else:
             return None
 
+    @property
+    def observed_end_time(self) -> datetime or None:
+        """
+        return the latest stop time of all (observation) tasks of this scheduling unit with the status observed
+        """
+        observed_tasks = []
+        for task in self.task_blueprints.all():
+            if task.status == TaskBlueprint.Status.OBSERVED.value and task.stop_time is not None:
+                observed_tasks.append(task)
+        if observed_tasks:
+            return max(observed_tasks, key=lambda x: x.stop_time).stop_time
+        else:
+            return None
+
     @property
     def status(self):
         """
@@ -930,7 +944,7 @@ class TaskBlueprint(NamedCommon):
                     # todo: max of several relations
                     if previous_related_task_blueprint.relative_stop_time:
                         return previous_related_task_blueprint.relative_stop_time + datetime.timedelta(seconds=time_offset)
-        return datetime.timedelta(seconds=666660)
+        return datetime.timedelta(seconds=0)
 
     @cached_property
     def relative_stop_time(self) -> datetime.timedelta:
diff --git a/SAS/TMSS/src/tmss/tmssapp/serializers/specification.py b/SAS/TMSS/src/tmss/tmssapp/serializers/specification.py
index bf250c5a51a..279d0ae7621 100644
--- a/SAS/TMSS/src/tmss/tmssapp/serializers/specification.py
+++ b/SAS/TMSS/src/tmss/tmssapp/serializers/specification.py
@@ -314,7 +314,7 @@ class SchedulingUnitBlueprintSerializer(RelationalHyperlinkedModelSerializer):
     class Meta:
         model = models.SchedulingUnitBlueprint
         fields = '__all__'
-        extra_fields = ['task_blueprints', 'duration', 'start_time', 'stop_time', 'status']
+        extra_fields = ['task_blueprints', 'duration', 'start_time', 'stop_time', 'status', 'observed_end_time']
 
 class SchedulingUnitBlueprintCopyToSchedulingUnitDraftSerializer(SchedulingUnitBlueprintSerializer):
     class Meta(SchedulingUnitDraftSerializer.Meta):
-- 
GitLab