Skip to content
Snippets Groups Projects
Verified Commit c07b08bd authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Rewrite data checks for new models

parent 0265905c
No related branches found
No related tags found
1 merge request!396Migration path to new models
import logging import logging
from datetime import datetime, time
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from django.db.models import F
from django.db.models.query_utils import Q from django.db.models.query_utils import Q
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from aleksis.apps.chronos.models import LessonEvent
from aleksis.core.data_checks import DataCheck, IgnoreSolveOption, SolveOption from aleksis.core.data_checks import DataCheck, IgnoreSolveOption, SolveOption
if TYPE_CHECKING: if TYPE_CHECKING:
...@@ -32,22 +33,12 @@ class SetGroupsWithCurrentGroupsSolveOption(SolveOption): ...@@ -32,22 +33,12 @@ class SetGroupsWithCurrentGroupsSolveOption(SolveOption):
check_result.delete() check_result.delete()
class ResetPersonalNoteSolveOption(SolveOption): class NoParticipationStatusesPersonalNotesInCancelledLessonsDataCheck(DataCheck):
name = "reset_personal_note" name = "no_personal_notes_participation_statuses_in_cancelled_lessons"
verbose_name = _("Reset personal note to defaults") verbose_name = _(
"Ensure that there are no participation statuses and personal notes in cancelled lessons"
@classmethod )
def solve(cls, check_result: "DataCheckResult"): problem_name = _("The participation status or personal note is related to a cancelled lesson.")
note = check_result.related_object
note.reset_values()
note.save()
check_result.delete()
class NoPersonalNotesInCancelledLessonsDataCheck(DataCheck):
name = "no_personal_notes_in_cancelled_lessons"
verbose_name = _("Ensure that there are no personal notes in cancelled lessons")
problem_name = _("The personal note is related to a cancelled lesson.")
solve_options = { solve_options = {
DeleteRelatedObjectSolveOption.name: DeleteRelatedObjectSolveOption, DeleteRelatedObjectSolveOption.name: DeleteRelatedObjectSolveOption,
IgnoreSolveOption.name: IgnoreSolveOption, IgnoreSolveOption.name: IgnoreSolveOption,
...@@ -55,27 +46,28 @@ class NoPersonalNotesInCancelledLessonsDataCheck(DataCheck): ...@@ -55,27 +46,28 @@ class NoPersonalNotesInCancelledLessonsDataCheck(DataCheck):
@classmethod @classmethod
def check_data(cls): def check_data(cls):
from .models import PersonalNote from .models import NewPersonalNote, ParticipationStatus
personal_notes = ( participation_statuses = ParticipationStatus.objects.filter(
PersonalNote.objects.not_empty() related_documentation__amends__in=LessonEvent.objects.filter(cancelled=True)
.filter( )
lesson_period__substitutions__cancelled=True, personal_notes = NewPersonalNote.objects.filter(
lesson_period__substitutions__week=F("week"), documentation__amends__in=LessonEvent.objects.filter(cancelled=True)
lesson_period__substitutions__year=F("year"),
)
.prefetch_related("lesson_period", "lesson_period__substitutions")
) )
for status in participation_statuses:
logging.info(f"Check participation status {status}")
cls.register_result(status)
for note in personal_notes: for note in personal_notes:
logging.info(f"Check personal note {note}") logging.info(f"Check personal note {note}")
cls.register_result(note) cls.register_result(note)
class NoGroupsOfPersonsSetInPersonalNotesDataCheck(DataCheck): class NoGroupsOfPersonsSetInParticipationStatusesDataCheck(DataCheck):
name = "no_groups_of_persons_set_in_personal_notes" name = "no_groups_of_persons_set_in_participation_statuses"
verbose_name = _("Ensure that 'groups_of_person' is set for every personal note") verbose_name = _("Ensure that 'groups_of_person' is set for every participation status")
problem_name = _("The personal note has no group in 'groups_of_person'.") problem_name = _("The participation status has no group in 'groups_of_person'.")
solve_options = { solve_options = {
SetGroupsWithCurrentGroupsSolveOption.name: SetGroupsWithCurrentGroupsSolveOption, SetGroupsWithCurrentGroupsSolveOption.name: SetGroupsWithCurrentGroupsSolveOption,
DeleteRelatedObjectSolveOption.name: DeleteRelatedObjectSolveOption, DeleteRelatedObjectSolveOption.name: DeleteRelatedObjectSolveOption,
...@@ -84,24 +76,21 @@ class NoGroupsOfPersonsSetInPersonalNotesDataCheck(DataCheck): ...@@ -84,24 +76,21 @@ class NoGroupsOfPersonsSetInPersonalNotesDataCheck(DataCheck):
@classmethod @classmethod
def check_data(cls): def check_data(cls):
from .models import PersonalNote from .models import ParticipationStatus
personal_notes = PersonalNote.objects.filter(groups_of_person__isnull=True) participation_statuses = ParticipationStatus.objects.filter(groups_of_person__isnull=True)
for note in personal_notes:
logging.info(f"Check personal note {note}")
cls.register_result(note)
for status in participation_statuses:
logging.info(f"Check participation status {status}")
cls.register_result(status)
class LessonDocumentationOnHolidaysDataCheck(DataCheck):
"""Checks for lesson documentation objects on holidays.
This ignores empty lesson documentation as they are created by default. class DocumentationOnHolidaysDataCheck(DataCheck):
""" """Checks for documentation objects on holidays."""
name = "lesson_documentation_on_holidays" name = "documentation_on_holidays"
verbose_name = _("Ensure that there are no filled out lesson documentations on holidays") verbose_name = _("Ensure that there are no documentations on holidays")
problem_name = _("The lesson documentation is on holidays.") problem_name = _("The documentation is on holidays.")
solve_options = { solve_options = {
DeleteRelatedObjectSolveOption.name: DeleteRelatedObjectSolveOption, DeleteRelatedObjectSolveOption.name: DeleteRelatedObjectSolveOption,
IgnoreSolveOption.name: IgnoreSolveOption, IgnoreSolveOption.name: IgnoreSolveOption,
...@@ -111,31 +100,31 @@ class LessonDocumentationOnHolidaysDataCheck(DataCheck): ...@@ -111,31 +100,31 @@ class LessonDocumentationOnHolidaysDataCheck(DataCheck):
def check_data(cls): def check_data(cls):
from aleksis.apps.chronos.models import Holiday from aleksis.apps.chronos.models import Holiday
from .models import LessonDocumentation from .models import Documentation
holidays = Holiday.objects.all() holidays = Holiday.objects.all()
documentations = LessonDocumentation.objects.not_empty().annotate_date_range()
q = Q(pk__in=[]) q = Q(pk__in=[])
for holiday in holidays: for holiday in holidays:
q = q | Q(day_end__gte=holiday.date_start, day_start__lte=holiday.date_end) q = q | Q(
documentations = documentations.filter(q) datetime_start__gte=datetime.combine(holiday.date_start, time.min),
datetime_end__lte=datetime.combine(holiday.date_end, time.max),
)
documentations = Documentation.objects.filter(q)
for doc in documentations: for doc in documentations:
logging.info(f"Lesson documentation {doc} is on holidays") logging.info(f"Documentation {doc} is on holidays")
cls.register_result(doc) cls.register_result(doc)
class PersonalNoteOnHolidaysDataCheck(DataCheck): class ParticipationStatusPersonalNoteOnHolidaysDataCheck(DataCheck):
"""Checks for personal note objects on holidays. """Checks for participation status and personal note objects on holidays."""
This ignores empty personal notes as they are created by default. name = "participation_status_personal_note_on_holidays"
""" verbose_name = _(
"Ensure that there are no participation statuses or personal notes on holidays"
name = "personal_note_on_holidays" )
verbose_name = _("Ensure that there are no filled out personal notes on holidays") problem_name = _("The participation status or personal note is on holidays.")
problem_name = _("The personal note is on holidays.")
solve_options = { solve_options = {
DeleteRelatedObjectSolveOption.name: DeleteRelatedObjectSolveOption, DeleteRelatedObjectSolveOption.name: DeleteRelatedObjectSolveOption,
IgnoreSolveOption.name: IgnoreSolveOption, IgnoreSolveOption.name: IgnoreSolveOption,
...@@ -145,16 +134,23 @@ class PersonalNoteOnHolidaysDataCheck(DataCheck): ...@@ -145,16 +134,23 @@ class PersonalNoteOnHolidaysDataCheck(DataCheck):
def check_data(cls): def check_data(cls):
from aleksis.apps.chronos.models import Holiday from aleksis.apps.chronos.models import Holiday
from .models import PersonalNote from .models import NewPersonalNote, ParticipationStatus
holidays = Holiday.objects.all() holidays = Holiday.objects.all()
personal_notes = PersonalNote.objects.not_empty().annotate_date_range()
q = Q(pk__in=[]) q = Q(pk__in=[])
for holiday in holidays: for holiday in holidays:
q = q | Q(day_end__gte=holiday.date_start, day_start__lte=holiday.date_end) q = q | Q(
personal_notes = personal_notes.filter(q) datetime_start__gte=datetime.combine(holiday.date_start, time.min),
datetime_end__lte=datetime.combine(holiday.date_end, time.max),
)
participation_statuses = ParticipationStatus.objects.filter(q)
personal_notes = NewPersonalNote.objects.filter(q)
for status in participation_statuses:
logging.info(f"Participation status {status} is on holidays")
cls.register_result(status)
for note in personal_notes: for note in personal_notes:
logging.info(f"Personal note {note} is on holidays") logging.info(f"Personal note {note} is on holidays")
......
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