From 440709c1891512885031e45c073817e3a8f1b37c Mon Sep 17 00:00:00 2001
From: Felix Blanke <info@fblanke.de>
Date: Mon, 27 May 2024 07:46:06 +0200
Subject: [PATCH] Introduce availabilits class method is_event_covered

---
 AKModel/availability/models.py |  8 ++++++++
 AKModel/models.py              | 17 +++--------------
 2 files changed, 11 insertions(+), 14 deletions(-)

diff --git a/AKModel/availability/models.py b/AKModel/availability/models.py
index 7ce794dc..de51e438 100644
--- a/AKModel/availability/models.py
+++ b/AKModel/availability/models.py
@@ -267,6 +267,14 @@ class Availability(models.Model):
         return Availability(start=timeframe_start, end=timeframe_end, event=event, person=person,
                                     room=room, ak=ak, ak_category=ak_category)
 
+    @classmethod
+    def is_event_covered(cls, event, availabilities: List['Availability']) -> bool:
+        # NOTE: Cannot use `Availability.with_event_length` as its end is the
+        #       event end + 1 day
+        full_event = Availability(event=event, start=event.start, end=event.end)
+        avail_union = Availability.union(availabilities)
+        return not avail_union or avail_union[0].contains(full_event)
+
     class Meta:
         verbose_name = _('Availability')
         verbose_name_plural = _('Availabilities')
diff --git a/AKModel/models.py b/AKModel/models.py
index c0078013..2052a5cd 100644
--- a/AKModel/models.py
+++ b/AKModel/models.py
@@ -579,12 +579,7 @@ class Room(models.Model):
 
         # check if room is available for the whole event
         # -> no time constraint needs to be introduced
-
-        # NOTE: Cannot use `Availability.with_event_length` as its end is the
-        #       event end + 1 day
-        full_event = Availability(event=self.event, start=self.event.start, end=self.event.end)
-        avail_union = Availability.union(self.availabilities.all())
-        if not avail_union or avail_union[0].contains(full_event):
+        if Availability.is_event_covered(self.event, self.availabilities.all()):
             time_constraints = []
         else:
             time_constraints = [f"availability-room-{self.pk}"]
@@ -703,19 +698,13 @@ class AKSlot(models.Model):
         # check if ak resp. owner is available for the whole event
         # -> no time constraint needs to be introduced
 
-        # NOTE: Cannot use `Availability.with_event_length` as its end is the
-        #       event end + 1 day
-        full_event = Availability(event=self.event, start=self.event.start, end=self.event.end)
-
-        ak_avail_union = Availability.union(self.ak.availabilities.all())
-        if not ak_avail_union or ak_avail_union[0].contains(full_event):
+        if Availability.is_event_covered(self.event, self.ak.availabilities.all()):
             ak_time_constraints = []
         else:
             ak_time_constraints = [f"availability-ak-{self.ak.pk}"]
 
         def _owner_time_constraints(owner: AKOwner):
-            owner_avail_union = Availability.union(owner.availabilities.all())
-            if not owner_avail_union or owner_avail_union[0].contains(full_event):
+            if Availability.is_event_covered(self.event, owner.availabilities.all()):
                 return []
             else:
                 return [f"availability-person-{owner.pk}"]
-- 
GitLab