From dab3f542ee0f02efe271be83bf423011bbe95770 Mon Sep 17 00:00:00 2001 From: Jonathan Weth <git@jonathanweth.de> Date: Sun, 25 Apr 2021 16:18:33 +0200 Subject: [PATCH] Delete old data check results for no longer persisting problems --- aleksis/core/data_checks.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/aleksis/core/data_checks.py b/aleksis/core/data_checks.py index a7d43d293..1cc70b6ed 100644 --- a/aleksis/core/data_checks.py +++ b/aleksis/core/data_checks.py @@ -1,5 +1,6 @@ import logging +from django.apps import apps from django.contrib.contenttypes.models import ContentType from django.db.models.aggregates import Count from django.utils.functional import classproperty @@ -156,11 +157,19 @@ class DataCheck: solve_options = {IgnoreSolveOption.name: IgnoreSolveOption} + _current_results = [] + @classmethod def check_data(cls): """Find all objects with data issues and register them.""" pass + @classmethod + def run_check_data(cls): + """Wrap ``check_data`` to ensure that post-processing tasks are run.""" + cls.check_data() + cls.delete_old_results() + @classmethod def solve(cls, check_result: "DataCheckResult", solve_option: str): """Execute a solve option for an object detected by this check. @@ -188,11 +197,30 @@ class DataCheck: from aleksis.core.models import DataCheckResult ct = ContentType.objects.get_for_model(instance) - result = DataCheckResult.objects.get_or_create( + result, __ = DataCheckResult.objects.get_or_create( check=cls.name, content_type=ct, object_id=instance.id ) + + # Track all existing problems (for deleting old results) + cls._current_results.append(result) + return result + @classmethod + def delete_old_results(cls): + """Delete old data check results for problems which exist no longer.""" + DataCheckResult = apps.get_model("core", "DataCheckResult") + + pks = [r.pk for r in cls._current_results] + old_results = DataCheckResult.objects.filter(check=cls.name).exclude(pk__in=pks) + + if old_results: + logging.info(f"Delete {old_results.count()} old data check results.") + old_results.delete() + + # Reset list with existing problems + cls._current_results = [] + class DataCheckRegistry: """Create central registry for all data checks in AlekSIS.""" @@ -213,7 +241,7 @@ def check_data(recorder: ProgressRecorder): """Execute all registered data checks and send email if activated.""" for check in recorder.iterate(DataCheckRegistry.data_checks): logging.info(f"Run check: {check.verbose_name}") - check.check_data() + check.run_check_data() if get_site_preferences()["general__data_checks_send_emails"]: send_emails_for_data_checks() -- GitLab