From 6341d76dafa674e3de9cb850dadacf33044b9b61 Mon Sep 17 00:00:00 2001
From: mirabilos <t.glaser@tarent.de>
Date: Sat, 21 Sep 2019 16:45:07 +0200
Subject: [PATCH] Add helper to retrieve next LessonPeriod for same group(s).
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Here, “same group(s)” means: the next time the same constellation
of groups has a lesson period together.

Advances: #34
---
 biscuit/apps/chronos/models.py | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/biscuit/apps/chronos/models.py b/biscuit/apps/chronos/models.py
index 93eb2fd0..62ff2474 100644
--- a/biscuit/apps/chronos/models.py
+++ b/biscuit/apps/chronos/models.py
@@ -187,6 +187,19 @@ class LessonPeriod(SchoolRelated):
     def get_groups(self) -> models.query.QuerySet:
         return self.lesson.groups
 
+    def next_of_same_groups(self) -> Optional[LessonPeriod]:
+        res = LessonPeriod.objects
+        # retrieve all LessonPeriods of this group constellation
+        for group in self.lesson.groups.all():
+            res = res.filter(lesson__groups__pk=group.pk)
+        # sorted by time
+        res = res.order_by('period__weekday', 'period__period').distinct()
+        # simple list of their PKs
+        all = list(res.values_list('pk', flat=True))
+        # next one after ourselves, wrapping around
+        next = all[(all.index(self.pk) + 1) % len(all)]
+        return LessonPeriod.objects.get(pk=next)
+
     def __str__(self) -> str:
         return '%s, %d., %s, %s' % (self.period.get_weekday_display(), self.period.period,
             ', '.join(list(self.lesson.groups.values_list('short_name', flat=True))),
-- 
GitLab