diff --git a/aleksis/apps/alsijil/data_checks.py b/aleksis/apps/alsijil/data_checks.py index 5f0272f888a0588b53c5cf37204bcb511d882f71..9f0ffb8330fceaa9fb6e5de3641b788b3ea8d430 100644 --- a/aleksis/apps/alsijil/data_checks.py +++ b/aleksis/apps/alsijil/data_checks.py @@ -2,10 +2,12 @@ import logging from django.contrib.contenttypes.models import ContentType from django.db.models import F +from django.db.models.query_utils import Q from django.utils.translation import gettext as _ from calendarweek import CalendarWeek +from aleksis.apps.chronos.util.date import week_weekday_to_date from aleksis.core.data_checks import DATA_CHECK_REGISTRY, DataCheck, IgnoreSolveOption, SolveOption @@ -88,3 +90,42 @@ class NoGroupsOfPersonsSetInPersonalNotesDataCheck(DataCheck): result = DataCheckResult.objects.get_or_create( check=cls.name, content_type=ct, object_id=note.id ) + + +@DATA_CHECK_REGISTRY.register +class LessonDocumentationOnHolidaysDataCheck(DataCheck): + """Checks for lesson documentation objects on holidays. + + This ignores empty lesson documentation as they are created by default. + """ + + name = "lesson_documentation_on_holidays" + verbose_name = _("Ensure that there are no filled out lesson documentations on holidays") + problem_name = _("The lesson documentation is on holidays.") + solve_options = { + DeleteRelatedObjectSolveOption.name: DeleteRelatedObjectSolveOption, + IgnoreSolveOption.name: IgnoreSolveOption, + } + + @classmethod + def check_data(cls): + from aleksis.apps.chronos.models import Holiday + from aleksis.core.models import DataCheckResult + + from .models import LessonDocumentation + + ct = ContentType.objects.get_for_model(LessonDocumentation) + holidays = list(Holiday.objects.all()) + + documentations = LessonDocumentation.objects.filter( + ~Q(topic="") | ~Q(group_note="") | ~Q(homework="") + ) + + for doc in documentations: + logging.info(f"Check lesson documentation {doc}") + day = week_weekday_to_date(doc.calendar_week, doc.lesson_period.period.weekday) + if len(list(filter(lambda h: h.date_start <= day <= h.date_end, holidays))) > 0: + logging.info(" ... on holidays") + result = DataCheckResult.objects.get_or_create( + check=cls.name, content_type=ct, object_id=doc.id + ) diff --git a/aleksis/apps/alsijil/models.py b/aleksis/apps/alsijil/models.py index 15700302b50f99e00dde171e43be75fc6a1465d1..b7d974572d3e0cfce4324ab96b4cc8494c198038 100644 --- a/aleksis/apps/alsijil/models.py +++ b/aleksis/apps/alsijil/models.py @@ -169,6 +169,14 @@ class LessonDocumentation(ExtensibleModel, WeekRelatedMixin): if changed: lesson_documentation.save() + def __str__(self): + return f"{self.lesson_period}, {date_format(self.date)}" + + def get_absolute_url(self): + return reverse( + "lesson_by_week_and_period", args=[self.year, self.week, self.lesson_period.pk], + ) + def save(self, *args, **kwargs): if get_site_preferences()["alsijil__carry_over"] and ( self.topic or self.homework or self.group_note