diff --git a/biscuit/apps/chronos/models.py b/biscuit/apps/chronos/models.py index 93eb2fd0d8909bd4a3c191bc07517609187bcb3d..62ff247499c860ea04ada9422e8b0c375daac397 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))),