From 76024cc60148ff3d5dadc776354e225d8a0bd436 Mon Sep 17 00:00:00 2001 From: Julian Leucker <leuckerj@gmail.com> Date: Wed, 17 Aug 2022 13:42:26 +0200 Subject: [PATCH] Display courses in coursebook for substitution teachers as well --- aleksis/apps/alsijil/views.py | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/aleksis/apps/alsijil/views.py b/aleksis/apps/alsijil/views.py index 0f44426d1..589c6bb59 100644 --- a/aleksis/apps/alsijil/views.py +++ b/aleksis/apps/alsijil/views.py @@ -37,7 +37,7 @@ from aleksis.apps.chronos.models import ( TimePeriod, ) from aleksis.apps.chronos.util.build import build_weekdays -from aleksis.apps.chronos.util.date import get_weeks_for_year, week_weekday_to_date +from aleksis.apps.chronos.util.date import get_current_year, get_weeks_for_year, week_weekday_to_date from aleksis.core.mixins import ( AdvancedCreateView, AdvancedDeleteView, @@ -1374,5 +1374,36 @@ class SelectCoursebookView(TemplateView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - context["lessons"] = self.request.user.person.lessons_as_teacher.all() + person = self.request.user.person + + current_week = CalendarWeek.current_week() + current_year = get_current_year() + # Show all future and the ones of last week + + last_week, last_week_year = ( + current_week - 1, + current_year + ) if current_week >= 2 else ( + CalendarWeek.get_last_week_of_year(current_year - 1), + current_year - 1 + ) + + last_week_query = Q( + lesson_periods__substitutions__week=last_week, + lesson_periods__substitutions__year=last_week_year + ) + this_week_query = Q( + lesson_periods__substitutions__week__gte=current_week, + lesson_periods__substitutions__year=current_year + ) + next_year_query = Q( + lesson_periods__substitutions__year__gt=current_year + ) + context["lessons"] = Lesson.objects.filter( + Q(teachers=person) | ( + Q(lesson_periods__substitutions__teachers=person) & ( + last_week_query | this_week_query | next_year_query + ) + ) + ).for_current_or_all() return context -- GitLab