From f58af4236ad470620225211378e78744c433009b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Benjamin=20H=C3=A4ttasch?=
 <benjamin.haettasch@fachschaft.informatik.tu-darmstadt.de>
Date: Thu, 28 Oct 2021 23:29:22 +0200
Subject: [PATCH] Implement room capacity constraint checks for changed AKs and
 rooms

---
 AKScheduling/models.py | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)

diff --git a/AKScheduling/models.py b/AKScheduling/models.py
index 0fb76831..deb7f795 100644
--- a/AKScheduling/models.py
+++ b/AKScheduling/models.py
@@ -100,7 +100,17 @@ def check_capacity_for_slot(slot: AKSlot):
 def ak_changed_handler(sender, instance: AK, **kwargs):
     # Changes might affect: Reso intention, Category, Interest
     # TODO Reso intention changes
-    pass
+
+    # Check room capacities
+    violation_type = ConstraintViolation.ViolationType.ROOM_CAPACITY_EXCEEDED
+    new_violations = []
+    for slot in instance.akslot_set.all():
+        cv = check_capacity_for_slot(slot)
+        if cv is not None:
+            new_violations.append(cv)
+
+    existing_violations_to_check = list(instance.constraintviolation_set.filter(type=violation_type))
+    update_constraint_violations(new_violations, existing_violations_to_check)
 
 
 @receiver(m2m_changed, sender=AK.owners.through)
@@ -540,9 +550,19 @@ def akslot_changed_handler(sender, instance: AKSlot, **kwargs):
 
 
 @receiver(post_save, sender=Room)
-def room_changed_handler(sender, **kwargs):
+def room_changed_handler(sender, instance: Room, **kwargs):
     # Changes might affect: Room size
-    print(f"{sender} changed")
+
+    # Check room capacities
+    violation_type = ConstraintViolation.ViolationType.ROOM_CAPACITY_EXCEEDED
+    new_violations = []
+    for slot in instance.akslot_set.all():
+        cv = check_capacity_for_slot(slot)
+        if cv is not None:
+            new_violations.append(cv)
+
+    existing_violations_to_check = list(instance.constraintviolation_set.filter(type=violation_type))
+    update_constraint_violations(new_violations, existing_violations_to_check)
 
 
 @receiver(m2m_changed, sender=Room.properties.through)
-- 
GitLab