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

Delete old data check results for no longer persisting problems

parent c7fae2a0
No related branches found
No related tags found
1 merge request!580Resolve "Remove old data check results if the data problem persists no longer"
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()
......
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