diff --git a/aleksis/apps/alsijil/model_extensions.py b/aleksis/apps/alsijil/model_extensions.py index e381407c49f10943904f11d50f6d56a7d8c31757..f65e2383713b5bf57624ef551dbf5b503b90c154 100644 --- a/aleksis/apps/alsijil/model_extensions.py +++ b/aleksis/apps/alsijil/model_extensions.py @@ -3,6 +3,7 @@ from typing import Dict, Optional, Union from django.db.models import Exists, OuterRef, QuerySet +import reversion from calendarweek import CalendarWeek from aleksis.apps.chronos.models import LessonPeriod @@ -45,21 +46,26 @@ def mark_absent( # Create and update all personal notes for the discovered lesson periods for lesson_period in lesson_periods: - personal_note, created = PersonalNote.objects.update_or_create( - person=self, - lesson_period=lesson_period, - week=wanted_week.week, - year=wanted_week.year, - defaults={"absent": absent, "excused": excused, "excuse_type": excuse_type}, - ) - personal_note.groups_of_person.set(self.member_of.all()) - - if remarks: - if personal_note.remarks: - personal_note.remarks += "; %s" % remarks - else: - personal_note.remarks = remarks - personal_note.save() + with reversion.create_revision(): + personal_note, created = PersonalNote.objects.update_or_create( + person=self, + lesson_period=lesson_period, + week=wanted_week.week, + year=wanted_week.year, + defaults={ + "absent": absent, + "excused": excused, + "excuse_type": excuse_type, + }, + ) + personal_note.groups_of_person.set(self.member_of.all()) + + if remarks: + if personal_note.remarks: + personal_note.remarks += "; %s" % remarks + else: + personal_note.remarks = remarks + personal_note.save() @LessonPeriod.method diff --git a/aleksis/apps/alsijil/views.py b/aleksis/apps/alsijil/views.py index bb47663efc608389e0b7aaed4bbbecff25d1f6a8..7887a60a255ba8fa497c238d4934ca1f14c81e47 100644 --- a/aleksis/apps/alsijil/views.py +++ b/aleksis/apps/alsijil/views.py @@ -127,7 +127,8 @@ def lesson( or not get_site_preferences()["alsijil__block_personal_notes_for_cancelled"] ): if personal_note_formset.is_valid(): - instances = personal_note_formset.save() + with reversion.create_revision(): + instances = personal_note_formset.save() # Iterate over personal notes and carry changed absences to following lessons for instance in instances: @@ -510,7 +511,12 @@ def overview_person(request: HttpRequest, id_: Optional[int] = None) -> HttpResp absent=True, excused=False, ) - notes.update(excused=True, excuse_type=excuse_type) + for note in notes: + note.excused = True + note.excuse_type = excuse_type + with reversion.create_revision(): + note.save() + messages.success( request, _("The absences have been marked as excused.") ) @@ -525,7 +531,8 @@ def overview_person(request: HttpRequest, id_: Optional[int] = None) -> HttpResp if note.absent: note.excused = True note.excuse_type = excuse_type - note.save() + with reversion.create_revision(): + note.save() messages.success( request, _("The absence has been marked as excused.") ) @@ -646,8 +653,6 @@ class DeletePersonalNoteView(DetailView): def post(self, request, *args, **kwargs): note = self.get_object() - with reversion.create_revision(): - self.save() with reversion.create_revision(): note.reset_values() messages.success(request, _("The personal note has been deleted."))