diff --git a/aleksis/apps/alsijil/models.py b/aleksis/apps/alsijil/models.py index feaba143cc7107c87556f9808ed3db1e7790b67f..774c1be3e6d390cfe83c494a866ec27d82b357e1 100644 --- a/aleksis/apps/alsijil/models.py +++ b/aleksis/apps/alsijil/models.py @@ -695,30 +695,41 @@ class Documentation(CalendarEvent): ) def touch(self): + """Ensure that participation statuses are created for this documentation.""" # TODO: Check for preexisting absences in kolego - # TODO: maybe only create if the lesson start is in the past - if not self.amends: - # There is no source to update from + if not self.amends or self.datetime_start <= localtime(): + # There is no source to update from or it's to early return lesson_event: LessonEvent = self.amends all_members = lesson_event.all_members - existing_participations = ParticipationStatus.objects.filter( - person__in=all_members, related_documentation=self + existing_participations = ParticipationStatus.objects.filter(related_documentation=self) + if existing_participations.exists(): + return + + new_persons = Person.objects.filter(Q(pk__in=[p.pk for p in all_members])).prefetch_related( + "member_of" ) - new_persons = Person.objects.filter( - Q(pk__in=[p.pk for p in all_members]) - & ~Q(pk__in=existing_participations.values_list("person", flat=True)) - ).prefetch_related("member_of") new_participations = [] + new_groups_of_person = [] for person in new_persons: - participation_status = self.build_participation_status(person) - participation_status.save() - participation_status.groups_of_person.set(person.member_of.all()) + participation_status = ParticipationStatus.objects.create( + person=person, + related_documentation=self, + datetime_start=self.datetime_start, + datetime_end=self.datetime_end, + timezone=self.timezone, + ) + new_groups_of_person += [ + ParticipationStatus.groups_of_person.through( + group=group, participationstatus=participation_status + ) + for group in person.member_of.all() + ] new_participations.append(participation_status) - + ParticipationStatus.groups_of_person.through.objects.bulk_create(new_groups_of_person) return new_participations