Skip to content
Snippets Groups Projects
Verified Commit 3a07d8c0 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Add helper methods for getting next/previous lesson of a person on a day

parent d0d7bb41
No related branches found
No related tags found
1 merge request!95Add helper methods for getting next/previous lesson of a person on a day
Pipeline #4160 failed
from datetime import date
from typing import Optional, Union
from django.utils.translation import gettext_lazy as _
......@@ -79,6 +80,38 @@ def lesson_periods_as_teacher(self):
return LessonPeriod.objects.filter(lesson__teachers=self)
@Person.method
def daily_lessons(self, day: date):
"""Get all lessons of this person on the given day."""
return LessonPeriod.objects.on_day(day).filter_from_person(self)
@Person.method
def next_lesson(self, lesson_period: "LessonPeriod", day: date) -> Union["LessonPeriod", None]:
"""Get next lesson of the person on the same day."""
daily_lessons = self.daily_lessons(day)
ids = list(daily_lessons.values_list("id", flat=True))
index = ids.index(lesson_period.pk)
if index + 1 < len(ids):
return daily_lessons[index + 1]
else:
return None
@Person.method
def previous_lesson(self, lesson_period: "LessonPeriod", day: date) -> Union["LessonPeriod", None]:
"""Get previous lesson of the person on the same day."""
daily_lessons = self.daily_lessons(day)
ids = list(daily_lessons.values_list("id", flat=True))
index = ids.index(lesson_period.pk)
if index > 0:
return daily_lessons[index - 1]
else:
return None
def for_timetables(cls):
"""Return all announcements that should be shown in timetable views."""
return cls.objects.filter(show_in_timetables=True)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment