diff --git a/aleksis/apps/chronos/managers.py b/aleksis/apps/chronos/managers.py index 0e956705f885244082aef5b68f6da51e20e26d1e..361f8da29cdeecf23ff80fc14ea722f9623d52c1 100644 --- a/aleksis/apps/chronos/managers.py +++ b/aleksis/apps/chronos/managers.py @@ -64,12 +64,13 @@ class LessonSubstitutionManager(CurrentSiteManager): class WeekQuerySetMixin: def annotate_week(self, week: Union[CalendarWeek, int]): """Annotate all lessons in the QuerySet with the number of the provided calendar week.""" - if isinstance(week, CalendarWeek): - week_num = week.week - else: - week_num = week + if isinstance(week, int): + week = CalendarWeek(week=week) - return self.annotate(_week=models.Value(week_num, models.IntegerField())) + return self.annotate( + _week=models.Value(week.week, models.IntegerField()), + _year=models.Value(week.year, models.IntegerField()) + ) class GroupByPeriodsMixin: @@ -236,6 +237,9 @@ class LessonDataQuerySet(models.QuerySet, WeekQuerySetMixin): if next_index > self.count() - 1: next_index %= self.count() week = reference._week + 1 + elif next_index < 0: + next_index = self.count() + next_index + week = reference._week - 1 else: week = reference._week diff --git a/aleksis/apps/chronos/models.py b/aleksis/apps/chronos/models.py index a7a54e9a741fd244173f99e3fe75923b6a80048e..572975b7d8d37343442c719c380c8c09e7d57423 100644 --- a/aleksis/apps/chronos/models.py +++ b/aleksis/apps/chronos/models.py @@ -321,6 +321,35 @@ class LessonPeriod(ExtensibleModel): def __str__(self) -> str: return f"{self.period}, {self.lesson}" + @property + def next(self) -> "LessonPeriod": + """Get next lesson period of this lesson. + + .. warning:: + To use this property, the provided lesson period must be annotated with a week. + """ + return LessonPeriod.objects.filter(lesson=self.lesson).next_lesson(self) + + @property + def prev(self) -> "LessonPeriod": + """Get previous lesson period of this lesson. + + .. warning:: + To use this property, the provided lesson period must be annotated with a week. + """ + return LessonPeriod.objects.filter(lesson=self.lesson).next_lesson(self, -1) + + @property + def week(self) -> Union[CalendarWeek, None]: + """Get annotated week as `CalendarWeek`. + + Defaults to `None` if no week is annotated. + """ + if hasattr(self, "_week"): + return CalendarWeek(week=self._week, year=self._year) + else: + return None + class Meta: ordering = [ "lesson__date_start",