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

Merge branch 'master' of edugit.org:Teckids/BiscuIT/BiscuIT-App-Alsijil

parents 1421e2b6 02ab2418
No related branches found
No related tags found
No related merge requests found
...@@ -36,7 +36,7 @@ class SelectForm(forms.Form): ...@@ -36,7 +36,7 @@ class SelectForm(forms.Form):
label=_('Group'), required=False) label=_('Group'), required=False)
teacher = forms.ModelChoiceField( teacher = forms.ModelChoiceField(
queryset=Person.objects.annotate(lessons_count=Count( queryset=Person.objects.annotate(lessons_count=Count(
'lessons')).filter(lessons_count__gt=0), 'lessons_as_teacher')).filter(lessons_count__gt=0),
label=_('Teacher'), required=False) label=_('Teacher'), required=False)
room = forms.ModelChoiceField( room = forms.ModelChoiceField(
queryset=Room.objects.annotate(lessons_count=Count( queryset=Room.objects.annotate(lessons_count=Count(
......
from datetime import date
from typing import Optional
from django.db.models import Exists, OuterRef
from biscuit.apps.chronos.models import LessonPeriod
from biscuit.apps.chronos.util import CalendarWeek
from biscuit.core.models import Group, Person
from .models import PersonalNote
@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,
defaults={
'absent': absent,
'excused': excused
}
)
@LessonPeriod.method
def get_personal_notes(self, wanted_week: CalendarWeek):
# Find all persons in the associated groups that do not yet have a personal note for this lesson
missing_persons = Person.objects.annotate(
no_personal_notes=~Exists(PersonalNote.objects.filter(
week=wanted_week.week,
lesson_period=self,
person__pk=OuterRef('pk')
))
).filter(
member_of__in=Group.objects.filter(pk__in=self.lesson.groups.all()),
is_active=True,
no_personal_notes=True
)
# Create all missing personal notes
PersonalNote.objects.bulk_create([
PersonalNote(person=person, lesson_period=self,
week=wanted_week.week) for person in missing_persons
])
return PersonalNote.objects.select_related('person').filter(
lesson_period=self, week=wanted_week.week)
...@@ -14,7 +14,7 @@ from biscuit.apps.chronos.util import CalendarWeek ...@@ -14,7 +14,7 @@ from biscuit.apps.chronos.util import CalendarWeek
from biscuit.core.models import Group, Person from biscuit.core.models import Group, Person
from .forms import LessonDocumentationForm, PersonalNoteFormSet, SelectForm from .forms import LessonDocumentationForm, PersonalNoteFormSet, SelectForm
from .models import LessonDocumentation, PersonalNote from .models import LessonDocumentation
@login_required @login_required
...@@ -48,36 +48,27 @@ def lesson(request: HttpRequest, year: Optional[int] = None, week: Optional[int] ...@@ -48,36 +48,27 @@ def lesson(request: HttpRequest, year: Optional[int] = None, week: Optional[int]
lesson_documentation_form = LessonDocumentationForm( lesson_documentation_form = LessonDocumentationForm(
request.POST or None, instance=lesson_documentation, prefix='leson_documentation') request.POST or None, instance=lesson_documentation, prefix='leson_documentation')
# Find all persons in the associated groups that do not yet have a personal note for this lesson
missing_persons = Person.objects.annotate(
no_personal_notes=~Exists(PersonalNote.objects.filter(
week=wanted_week.week,
lesson_period=lesson_period,
person__pk=OuterRef('pk')
))
).filter(
member_of__in=Group.objects.filter(pk__in=lesson_period.lesson.groups.all()),
is_active=True,
no_personal_notes=True
)
# Create all missing personal notes
PersonalNote.objects.bulk_create([
PersonalNote(person=person, lesson_period=lesson_period,
week=wanted_week.week) for person in missing_persons # FIXME Respect year as well
])
# Create a formset that holds all personal notes for all persons in this lesson # Create a formset that holds all personal notes for all persons in this lesson
persons_qs = PersonalNote.objects.select_related('person').filter( persons_qs = lesson_period.get_personal_notes(wanted_week)
lesson_period=lesson_period, week=wanted_week.week) # FIXME Respect year as well
personal_note_formset = PersonalNoteFormSet( personal_note_formset = PersonalNoteFormSet(
request.POST or None, queryset=persons_qs, prefix='personal_notes') request.POST or None, queryset=persons_qs, prefix='personal_notes')
if request.method == 'POST': if request.method == 'POST':
if lesson_documentation_form.is_valid(): if lesson_documentation_form.is_valid():
lesson_documentation_form.save() lesson_documentation_form.save()
if personal_note_formset.is_valid(): 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-1],
lesson_period.period.period+1,
instance.absent,
instance.excused
)
context['lesson_documentation'] = lesson_documentation context['lesson_documentation'] = lesson_documentation
context['lesson_documentation_form'] = lesson_documentation_form 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