From a5d0e2c82c714c216bb78203d7246c66182506c9 Mon Sep 17 00:00:00 2001
From: Dominik George <dominik.george@teckids.org>
Date: Sun, 27 Oct 2019 16:26:06 +0100
Subject: [PATCH] Respect week overjet in next(). Advances #34.

---
 biscuit/apps/chronos/models.py | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/biscuit/apps/chronos/models.py b/biscuit/apps/chronos/models.py
index 49eece26..dd1300cd 100644
--- a/biscuit/apps/chronos/models.py
+++ b/biscuit/apps/chronos/models.py
@@ -83,8 +83,18 @@ class LessonPeriodQuerySet(models.QuerySet):
                 Q(substitutions__room=room, substitutions__week=models.F('_week')) | Q(room=room))
 
     def next(self, reference: LessonPeriod, offset: Optional[int] = 1) -> LessonPeriod:
-        id_ = list(self.values_list('id', flat=True)).index(reference.id)
-        return self.all()[id_+offset]
+        index = list(self.values_list('id', flat=True)).index(reference.id)
+
+        next_index = index + offset
+        if next_index > self.count() - 1:
+            next_index %= self.count()
+            week = reference._week + 1
+        else:
+            week = reference._week
+
+        return self.extra(
+            select={'_week': week}
+        ).all()[next_index]
 
     def filter_from_query(self, query_data: QueryDict):
         if query_data.get('group', None):
-- 
GitLab