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] 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