From b963f1a121da100ab3655d33c6e4468ca8da7093 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Benjamin=20H=C3=A4ttasch?=
 <benjamin.haettasch@fachschaft.informatik.tu-darmstadt.de>
Date: Fri, 29 Oct 2021 11:39:04 +0200
Subject: [PATCH] Make sure corresponding CVs are deleted if reso_deadline is
 nulled

---
 AKScheduling/models.py | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/AKScheduling/models.py b/AKScheduling/models.py
index deb7f795..bfdb1e16 100644
--- a/AKScheduling/models.py
+++ b/AKScheduling/models.py
@@ -44,6 +44,8 @@ def update_cv_reso_deadline_for_slot(slot):
     """
     event = slot.event
     if slot.ak.reso and slot.event.reso_deadline and slot.start:
+        # Update only if reso_deadline exists
+        # if event was changed and reso_deadline is removed, CVs will be deleted by event changed handler
         violation_type = ConstraintViolation.ViolationType.AK_AFTER_RESODEADLINE
         new_violations = []
         if slot.end > event.reso_deadline:
@@ -612,7 +614,7 @@ def availability_changed_handler(sender, instance: Availability, **kwargs):
                 c.ak_slots_tmp.add(slot)
                 new_violations.append(c)
 
-        print(f"{instance.ak} has the following slots putside availabilities: {new_violations}")
+        print(f"{instance.ak} has the following slots outside availabilities: {new_violations}")
 
         # ... and compare to/update list of existing violations of this type
         # belonging to the AK that was recently changed (important!)
@@ -622,8 +624,13 @@ def availability_changed_handler(sender, instance: Availability, **kwargs):
 
 
 @receiver(post_save, sender=Event)
-def event_changed_handler(sender, instance, **kwargs):
+def event_changed_handler(sender, instance: Event, **kwargs):
     # == Check for reso ak after reso deadline (which might have changed) ==
     if instance.reso_deadline:
         for slot in instance.akslot_set.filter(start__isnull=False, ak__reso=True):
             update_cv_reso_deadline_for_slot(slot)
+    else:
+        # No reso deadline, delete all violations
+        violation_type = ConstraintViolation.ViolationType.AK_AFTER_RESODEADLINE
+        existing_violations_to_check = list(instance.constraintviolation_set.filter(type=violation_type))
+        update_constraint_violations([], existing_violations_to_check)
-- 
GitLab