Skip to content
Snippets Groups Projects
Verified Commit 3c3f3f1d authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Carry absences to all following lessons on save. Closes #17.

parent ba7036cf
No related branches found
No related tags found
No related merge requests found
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
......
......@@ -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
......
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