diff --git a/biscuit/apps/alsijil/model_extensions.py b/biscuit/apps/alsijil/model_extensions.py index 9d1bd94136bdfc1c624e7dd86d85c210cece5f03..e34dcc9bb8383976f8a7981c4109a9fbcbf17fcf 100644 --- a/biscuit/apps/alsijil/model_extensions.py +++ b/biscuit/apps/alsijil/model_extensions.py @@ -1,3 +1,6 @@ +from datetime import date +from typing import Optional + from django.db.models import Exists, OuterRef from biscuit.apps.chronos.models import Lesson, LessonPeriod @@ -22,6 +25,28 @@ def lesson_periods_as_teacher(self): return LessonPeriod.objects.filter(lesson__teachers=self) +@Person.method +def mark_absent(self, day: date, starting_period: Optional[int] = 0, absent=True, excused=False): + wanted_week = CalendarWeek.from_date(day) + + # Get all lessons of this person on the specified day + lesson_periods = self.lesson_periods_as_participant.on_day( + day + ).filter( + period__period__gte=starting_period + ) + + # Create and update all personal notes for the discovered lesson periods + for lesson_period in lesson_periods: + PersonalNote.objects.update_or_create( + person=self, + lesson_period=lesson_period, + week=wanted_week.week, + absent=absent, + excused=excused + ) + + @LessonPeriod.method def personal_notes(self, wanted_week: CalendarWeek): # Find all persons in the associated groups that do not yet have a personal note for this lesson diff --git a/biscuit/apps/alsijil/views.py b/biscuit/apps/alsijil/views.py index c9ab0a579727d8a82e0078b38b83a7e7a26881d8..f2d4a8b96670bb7c001663dcd34dc08f7d79611e 100644 --- a/biscuit/apps/alsijil/views.py +++ b/biscuit/apps/alsijil/views.py @@ -14,7 +14,7 @@ from biscuit.apps.chronos.util import CalendarWeek from biscuit.core.models import Group, Person from .forms import LessonDocumentationForm, PersonalNoteFormSet, SelectForm -from .models import LessonDocumentation, PersonalNote +from .models import LessonDocumentation @login_required @@ -57,8 +57,18 @@ def lesson(request: HttpRequest, year: Optional[int] = None, week: Optional[int] if request.method == 'POST': if lesson_documentation_form.is_valid(): lesson_documentation_form.save() + if personal_note_formset.is_valid(): - personal_note_formset.save() + instances = personal_note_formset.save() + + # Iterate over personal notes and carry changed absences to following lessons + for instance in instances: + instance.person.mark_absent( + wanted_week[lesson_period.period.weekday], + lesson_period.period.period+1, + instance.absent, + instance.excused + ) context['lesson_documentation'] = lesson_documentation context['lesson_documentation_form'] = lesson_documentation_form