From 554b3871190806f122a48e56ffbb8ae573b42182 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Benjamin=20H=C3=A4ttasch?=
 <benjamin.haettasch@fachschaft.informatik.tu-darmstadt.de>
Date: Thu, 13 May 2021 02:34:38 +0200
Subject: [PATCH] Fix constraint check for not-yet scheduled ak slots

---
 AKScheduling/models.py | 74 ++++++++++++++++++++++--------------------
 1 file changed, 38 insertions(+), 36 deletions(-)

diff --git a/AKScheduling/models.py b/AKScheduling/models.py
index f148da47..769b2ece 100644
--- a/AKScheduling/models.py
+++ b/AKScheduling/models.py
@@ -268,29 +268,30 @@ def akslot_changed_handler(sender, instance: AKSlot, **kwargs):
     violation_type = ConstraintViolation.ViolationType.OWNER_TWO_SLOTS
     new_violations = []
 
-    # For all owners (after recent change)...
-    for owner in instance.ak.owners.all():
-        # ...find other slots that might be overlapping...
+    if instance.start:
+        # For all owners (after recent change)...
+        for owner in instance.ak.owners.all():
+            # ...find other slots that might be overlapping...
 
-        for ak in owner.ak_set.all():
-            # ...find overlapping slots...
-            if ak != instance.ak:
-                for other_slot in ak.akslot_set.filter(start__isnull=False):
-                    if instance.overlaps(other_slot):
-                        # ...and create a temporary violation if necessary...
-                        c = ConstraintViolation(
-                            type=violation_type,
-                            level=ConstraintViolation.ViolationLevel.VIOLATION,
-                            event=event,
-                            ak_owner=owner
-                        )
-                        c.aks_tmp.add(instance.ak)
-                        c.aks_tmp.add(other_slot.ak)
-                        c.ak_slots_tmp.add(instance)
-                        c.ak_slots_tmp.add(other_slot)
-                        new_violations.append(c)
+            for ak in owner.ak_set.all():
+                # ...find overlapping slots...
+                if ak != instance.ak:
+                    for other_slot in ak.akslot_set.filter(start__isnull=False):
+                        if instance.overlaps(other_slot):
+                            # ...and create a temporary violation if necessary...
+                            c = ConstraintViolation(
+                                type=violation_type,
+                                level=ConstraintViolation.ViolationLevel.VIOLATION,
+                                event=event,
+                                ak_owner=owner
+                            )
+                            c.aks_tmp.add(instance.ak)
+                            c.aks_tmp.add(other_slot.ak)
+                            c.ak_slots_tmp.add(instance)
+                            c.ak_slots_tmp.add(other_slot)
+                            new_violations.append(c)
 
-        print(f"{owner} has the following conflicts: {new_violations}")
+            print(f"{owner} has the following conflicts: {new_violations}")
 
     # ... and compare to/update list of existing violations of this type
     # belonging to the AK that was recently changed (important!)
@@ -339,21 +340,22 @@ def akslot_changed_handler(sender, instance: AKSlot, **kwargs):
     violation_type = ConstraintViolation.ViolationType.AK_SLOT_COLLISION
     new_violations = []
 
-    # For all other slots of this ak...
-    for other_slot in instance.ak.akslot_set.filter(start__isnull=False):
-        if other_slot != instance:
-            # ... find overlapping slots...
-            if instance.overlaps(other_slot):
-                # ...and create a temporary violation if necessary...
-                c = ConstraintViolation(
-                    type=violation_type,
-                    level=ConstraintViolation.ViolationLevel.WARNING,
-                    event=event,
-                )
-                c.aks_tmp.add(instance.ak)
-                c.ak_slots_tmp.add(instance)
-                c.ak_slots_tmp.add(other_slot)
-                new_violations.append(c)
+    if instance.start:
+        # For all other slots of this ak...
+        for other_slot in instance.ak.akslot_set.filter(start__isnull=False):
+            if other_slot != instance:
+                # ... find overlapping slots...
+                if instance.overlaps(other_slot):
+                    # ...and create a temporary violation if necessary...
+                    c = ConstraintViolation(
+                        type=violation_type,
+                        level=ConstraintViolation.ViolationLevel.WARNING,
+                        event=event,
+                    )
+                    c.aks_tmp.add(instance.ak)
+                    c.ak_slots_tmp.add(instance)
+                    c.ak_slots_tmp.add(other_slot)
+                    new_violations.append(c)
 
     # ... and compare to/update list of existing violations of this type
     # belonging to the slot that was recently changed (important!)
-- 
GitLab