From 3dc519eac0777ee49106b5e6361e4d88268318a6 Mon Sep 17 00:00:00 2001
From: Felix Blanke <info@fblanke.de>
Date: Tue, 21 Jan 2025 19:32:16 +0100
Subject: [PATCH 1/4] Encapsulate discretization in Event::discretize_timeslots

---
 AKModel/models.py   | 9 ++++++++-
 AKModel/views/ak.py | 6 +-----
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/AKModel/models.py b/AKModel/models.py
index d30912d3..f948da9b 100644
--- a/AKModel/models.py
+++ b/AKModel/models.py
@@ -382,6 +382,13 @@ class Event(models.Model):
                 constraints=category_constraints,
             )
 
+    def discretize_timeslots(self, *, slots_in_an_hour: float = 1.0) -> Iterable[TimeslotBlock]:
+        if DefaultSlot.objects.filter(event=self.event).exists():
+            # discretize default slots if they exists
+            yield from merge_blocks(self.default_time_slots(slots_in_an_hour=SLOTS_IN_AN_HOUR))
+        else:
+            yield from self.uniform_time_slots(slots_in_an_hour=SLOTS_IN_AN_HOUR)
+
     def schedule_from_json(self, schedule: str) -> None:
         """Load AK schedule from a json string.
 
@@ -396,7 +403,7 @@ class Event(models.Model):
 
         timeslot_dict = {
             timeslot.idx: timeslot
-            for block in merge_blocks(self.default_time_slots(slots_in_an_hour=slots_in_an_hour))
+            for block in self.discretize_timeslots(slots_in_an_hour=slots_in_an_hour)
             for timeslot in block
         }
 
diff --git a/AKModel/views/ak.py b/AKModel/views/ak.py
index 90599a1b..859120ba 100644
--- a/AKModel/views/ak.py
+++ b/AKModel/views/ak.py
@@ -114,11 +114,7 @@ class AKJSONExportView(AdminViewMixin, FilterByEventSlugMixin, ListView):
             if (values := AKSlot.objects.select_related().filter(ak__pk=ak_id, fixed=True)).exists()
         }
 
-        if DefaultSlot.objects.filter(event=self.event).exists():
-            # discretize default slots if they exists
-            blocks = merge_blocks(self.event.default_time_slots(slots_in_an_hour=SLOTS_IN_AN_HOUR))
-        else:
-            blocks = self.event.uniform_time_slots(slots_in_an_hour=SLOTS_IN_AN_HOUR)
+        blocks = self.event.discretize_timeslots(slots_in_an_hour=SLOTS_IN_AN_HOUR)
 
         for block in blocks:
             current_block = []
-- 
GitLab


From 744b0e76af0e27dc08199d05c83829268f601292 Mon Sep 17 00:00:00 2001
From: Felix Blanke <info@fblanke.de>
Date: Tue, 21 Jan 2025 19:37:00 +0100
Subject: [PATCH 2/4] Fix

---
 AKModel/models.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/AKModel/models.py b/AKModel/models.py
index f948da9b..208974b8 100644
--- a/AKModel/models.py
+++ b/AKModel/models.py
@@ -383,7 +383,7 @@ class Event(models.Model):
             )
 
     def discretize_timeslots(self, *, slots_in_an_hour: float = 1.0) -> Iterable[TimeslotBlock]:
-        if DefaultSlot.objects.filter(event=self.event).exists():
+        if DefaultSlot.objects.filter(event=self).exists():
             # discretize default slots if they exists
             yield from merge_blocks(self.default_time_slots(slots_in_an_hour=SLOTS_IN_AN_HOUR))
         else:
-- 
GitLab


From bf157dc3d360dbaeb477e78ab53b9a240d44aa56 Mon Sep 17 00:00:00 2001
From: Felix Blanke <info@fblanke.de>
Date: Tue, 21 Jan 2025 19:41:26 +0100
Subject: [PATCH 3/4] Fix kwarg usage

---
 AKModel/models.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/AKModel/models.py b/AKModel/models.py
index 208974b8..173c2745 100644
--- a/AKModel/models.py
+++ b/AKModel/models.py
@@ -385,9 +385,9 @@ class Event(models.Model):
     def discretize_timeslots(self, *, slots_in_an_hour: float = 1.0) -> Iterable[TimeslotBlock]:
         if DefaultSlot.objects.filter(event=self).exists():
             # discretize default slots if they exists
-            yield from merge_blocks(self.default_time_slots(slots_in_an_hour=SLOTS_IN_AN_HOUR))
+            yield from merge_blocks(self.default_time_slots(slots_in_an_hour=slots_in_an_hour))
         else:
-            yield from self.uniform_time_slots(slots_in_an_hour=SLOTS_IN_AN_HOUR)
+            yield from self.uniform_time_slots(slots_in_an_hour=slots_in_an_hour)
 
     def schedule_from_json(self, schedule: str) -> None:
         """Load AK schedule from a json string.
-- 
GitLab


From 71c4ac46eb928848bff3598f5a039abc4ee0dbfd Mon Sep 17 00:00:00 2001
From: Felix Blanke <info@fblanke.de>
Date: Wed, 22 Jan 2025 12:14:05 +0100
Subject: [PATCH 4/4] Address findings

---
 AKModel/models.py   | 10 ++++++++++
 AKModel/views/ak.py |  2 +-
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/AKModel/models.py b/AKModel/models.py
index 173c2745..2a80cef7 100644
--- a/AKModel/models.py
+++ b/AKModel/models.py
@@ -383,6 +383,16 @@ class Event(models.Model):
             )
 
     def discretize_timeslots(self, *, slots_in_an_hour: float = 1.0) -> Iterable[TimeslotBlock]:
+        """"Choose discretization scheme.
+
+        Uses default_time_slots if the event has any DefaultSlot, otherwise uniform_time_slots.
+
+        :param slots_in_an_hour: The percentage of an hour covered by a single slot.
+            Determines the discretization granularity.
+        :yield: Block of optimizer timeslots as the discretization result.
+        :ytype: list of TimeslotBlock
+        """
+
         if DefaultSlot.objects.filter(event=self).exists():
             # discretize default slots if they exists
             yield from merge_blocks(self.default_time_slots(slots_in_an_hour=slots_in_an_hour))
diff --git a/AKModel/views/ak.py b/AKModel/views/ak.py
index 859120ba..1d60717b 100644
--- a/AKModel/views/ak.py
+++ b/AKModel/views/ak.py
@@ -9,7 +9,7 @@ from django.views.generic import ListView, DetailView
 from AKModel.availability.models import Availability
 from AKModel.metaviews.admin import AdminViewMixin, FilterByEventSlugMixin, EventSlugMixin, IntermediateAdminView, \
     IntermediateAdminActionView
-from AKModel.models import AKRequirement, AKSlot, DefaultSlot, Event, AKOrgaMessage, AK, Room, AKOwner, merge_blocks
+from AKModel.models import AKRequirement, AKSlot, Event, AKOrgaMessage, AK, Room, AKOwner
 
 
 class AKRequirementOverview(AdminViewMixin, FilterByEventSlugMixin, ListView):
-- 
GitLab