From 3c3f3f1deb2831cab78c82cdf0860330a390bb0b Mon Sep 17 00:00:00 2001 From: Dominik George <nik@naturalnet.de> Date: Sun, 10 Nov 2019 21:01:02 +0100 Subject: [PATCH] Carry absences to all following lessons on save. Closes #17. --- biscuit/apps/alsijil/model_extensions.py | 25 ++++++++++++++++++++++++ biscuit/apps/alsijil/views.py | 14 +++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/biscuit/apps/alsijil/model_extensions.py b/biscuit/apps/alsijil/model_extensions.py index 9d1bd9413..e34dcc9bb 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 c9ab0a579..f2d4a8b96 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 -- GitLab