From 29ba4088651951358d6b03999b7e88e551869558 Mon Sep 17 00:00:00 2001 From: Dominik George <nik@naturalnet.de> Date: Sun, 10 Nov 2019 20:35:07 +0100 Subject: [PATCH] Move some APIs to model extensions on chronos models. --- biscuit/apps/alsijil/model_extensions.py | 47 ++++++++++++++++++++++++ biscuit/apps/alsijil/views.py | 21 +---------- 2 files changed, 48 insertions(+), 20 deletions(-) create mode 100644 biscuit/apps/alsijil/model_extensions.py diff --git a/biscuit/apps/alsijil/model_extensions.py b/biscuit/apps/alsijil/model_extensions.py new file mode 100644 index 000000000..9d1bd9413 --- /dev/null +++ b/biscuit/apps/alsijil/model_extensions.py @@ -0,0 +1,47 @@ +from django.db.models import Exists, OuterRef + +from biscuit.apps.chronos.models import Lesson, LessonPeriod +from biscuit.apps.chronos.util import CalendarWeek +from biscuit.core.models import Group, Person + +from .models import PersonalNote + + +@Person.property +def lessons_as_participant(self): + return Lesson.objects.filter(groups__members=self) + + +@Person.property +def lesson_periods_as_participant(self): + return LessonPeriod.objects.filter(lesson__groups__members=self) + + +@Person.property +def lesson_periods_as_teacher(self): + return LessonPeriod.objects.filter(lesson__teachers=self) + + +@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 + 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) diff --git a/biscuit/apps/alsijil/views.py b/biscuit/apps/alsijil/views.py index ce4694ff6..c9ab0a579 100644 --- a/biscuit/apps/alsijil/views.py +++ b/biscuit/apps/alsijil/views.py @@ -48,28 +48,9 @@ def lesson(request: HttpRequest, year: Optional[int] = None, week: Optional[int] lesson_documentation_form = LessonDocumentationForm( 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 - persons_qs = PersonalNote.objects.select_related('person').filter( - lesson_period=lesson_period, week=wanted_week.week) # FIXME Respect year as well + persons_qs = lesson_period.personal_notes(wanted_week) personal_note_formset = PersonalNoteFormSet( request.POST or None, queryset=persons_qs, prefix='personal_notes') -- GitLab