From e34b6280c2823e3182b828ee3cc222bb50e7191d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Benjamin=20H=C3=A4ttasch?=
 <benjamin.haettasch@fachschaft.informatik.tu-darmstadt.de>
Date: Wed, 12 May 2021 01:41:42 +0200
Subject: [PATCH] Implement reso deadline violation check (both for slot and
 event changes)

Additionally, make sure that ak fields are always filled (was not the case for multiple aks in one room check)
---
 AKScheduling/models.py | 37 +++++++++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 4 deletions(-)

diff --git a/AKScheduling/models.py b/AKScheduling/models.py
index 4aa0fb22..29624910 100644
--- a/AKScheduling/models.py
+++ b/AKScheduling/models.py
@@ -34,6 +34,28 @@ def update_constraint_violations(new_violations, existing_violations_to_check):
         outdated_violation.delete()
 
 
+def update_cv_reso_deadline_for_slot(slot):
+    """
+    Update constraint violation AK_AFTER_RESODEADLINE for given slot
+
+    :param slot: slot to check/update
+    :type slot: AKSlot
+    """
+    event = slot.event
+    if slot.ak.reso and slot.event.reso_deadline:
+        violation_type = ConstraintViolation.ViolationType.AK_AFTER_RESODEADLINE
+        new_violations = []
+        if slot.end > event.reso_deadline:
+            c = ConstraintViolation(
+                type=violation_type,
+                level=ConstraintViolation.ViolationLevel.VIOLATION,
+                event=event,
+            )
+            c.aks_tmp.add(slot.ak)
+            c.ak_slots_tmp.add(slot)
+            new_violations.append(c)
+        update_constraint_violations(new_violations, list(slot.constraintviolation_set.filter(type=violation_type)))
+
 @receiver(post_save, sender=AK)
 def ak_changed_handler(sender, instance: AK, **kwargs):
     # Changes might affect: Owner(s), Requirements, Conflicts, Prerequisites, Category, Interest
@@ -149,6 +171,8 @@ def akslot_changed_handler(sender, instance: AKSlot, **kwargs):
                     event=event,
                     room=instance.room
                 )
+                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)
@@ -161,6 +185,10 @@ def akslot_changed_handler(sender, instance: AKSlot, **kwargs):
     print(existing_violations_to_check)
     update_constraint_violations(new_violations, existing_violations_to_check)
 
+    # == Check for reso ak after reso deadline ==
+
+    update_cv_reso_deadline_for_slot(instance)
+
 
 @receiver(post_save, sender=Room)
 def room_changed_handler(sender, **kwargs):
@@ -177,7 +205,8 @@ def availability_changed_handler(sender, **kwargs):
 
 
 @receiver(post_save, sender=Event)
-def room_changed_handler(sender, **kwargs):
-    # Changes might affect: Reso-Deadline
-    print(f"{sender} changed")
-    # TODO Replace with real handling
+def room_changed_handler(sender, instance, **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)
-- 
GitLab