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

Let next_lesson/prev_lesson base on common method _adjacent_lesson

Also improve documentation and naming
parent 3a07d8c0
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 #4193 failed
......@@ -81,35 +81,44 @@ def lesson_periods_as_teacher(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)
def lessons_on_day(self, day: date):
"""Get all lessons of this person (either as participant or teacher) on the given day."""
return (
LessonPeriod.objects.on_day(day)
.filter_from_person(self)
.order_by("period__period")
)
@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)
def _adjacent_lesson(
self, lesson_period: "LessonPeriod", day: date, offset: int = 1
) -> Union["LessonPeriod", None]:
"""Get next/previous lesson of the person (either as participant or teacher) on the same day."""
daily_lessons = self.lessons_on_day(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]
if (offset > 0 and index + offset < len(ids)) or (offset < 0 and index >= -offset):
return daily_lessons[index + offset]
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)
def next_lesson(
self, lesson_period: "LessonPeriod", day: date
) -> Union["LessonPeriod", None]:
"""Get next lesson of the person (either as participant or teacher) on the same day."""
return self._adjacent_lesson(lesson_period, day)
if index > 0:
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 (either as participant or teacher) on the same day."""
return self._adjacent_lesson(lesson_period, day, offset=-1)
def for_timetables(cls):
......
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