From 60b52a141231ab13845b9895095da1eca50be665 Mon Sep 17 00:00:00 2001
From: Jonathan Weth <git@jonathanweth.de>
Date: Sat, 19 Sep 2020 19:05:00 +0200
Subject: [PATCH] Move data check to own (optional) celery task

---
 aleksis/apps/alsijil/data_checks.py           |  3 +++
 .../templates/alsijil/data_check/list.html    |  2 +-
 aleksis/apps/alsijil/urls.py                  |  1 +
 aleksis/apps/alsijil/views.py                 | 26 ++++++++++++++-----
 4 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/aleksis/apps/alsijil/data_checks.py b/aleksis/apps/alsijil/data_checks.py
index a3e3edd1c..54abf4678 100644
--- a/aleksis/apps/alsijil/data_checks.py
+++ b/aleksis/apps/alsijil/data_checks.py
@@ -7,6 +7,8 @@ from django.utils.translation import gettext as _
 import reversion
 from calendarweek import CalendarWeek
 
+from aleksis.core.util.core_helpers import celery_optional
+
 
 class SolveOption:
     name: str = "default"
@@ -90,6 +92,7 @@ DATA_CHECKS_BY_NAME = {check.name: check for check in DATA_CHECKS}
 DATA_CHECKS_CHOICES = [(check.name, check.verbose_name) for check in DATA_CHECKS]
 
 
+@celery_optional
 def check_data():
     for check in DATA_CHECKS:
         logging.info(f"Run check: {check.verbose_name}")
diff --git a/aleksis/apps/alsijil/templates/alsijil/data_check/list.html b/aleksis/apps/alsijil/templates/alsijil/data_check/list.html
index 3d84d2595..e1d763f5c 100644
--- a/aleksis/apps/alsijil/templates/alsijil/data_check/list.html
+++ b/aleksis/apps/alsijil/templates/alsijil/data_check/list.html
@@ -10,7 +10,7 @@
 {% block page_title %}{% blocktrans %}Data checks{% endblocktrans %}{% endblock %}
 
 {% block content %}
-  <a class="btn green waves-effect waves-light" href="#">
+  <a class="btn green waves-effect waves-light" href="{% url "data_check_run" %}">
     <i class="material-icons left">refresh</i>
     {% trans "Check data again" %}
   </a>
diff --git a/aleksis/apps/alsijil/urls.py b/aleksis/apps/alsijil/urls.py
index 1fab7d4e7..16f8a43dd 100644
--- a/aleksis/apps/alsijil/urls.py
+++ b/aleksis/apps/alsijil/urls.py
@@ -69,6 +69,7 @@ urlpatterns = [
         name="delete_excuse_type",
     ),
     path("data_check/", views.DataCheckView.as_view(), name="check_data",),
+    path("data_check/run/", views.run_data_checks, name="data_check_run",),
     path(
         "data_check/<int:id_>/<str:solve_option>/",
         views.solve_data_check_view,
diff --git a/aleksis/apps/alsijil/views.py b/aleksis/apps/alsijil/views.py
index 875207aaa..cb4ca7ab7 100644
--- a/aleksis/apps/alsijil/views.py
+++ b/aleksis/apps/alsijil/views.py
@@ -23,7 +23,11 @@ from aleksis.apps.chronos.util.date import get_weeks_for_year, week_weekday_to_d
 from aleksis.core.mixins import AdvancedCreateView, AdvancedDeleteView, AdvancedEditView
 from aleksis.core.models import Group, Person, SchoolTerm
 from aleksis.core.util import messages
-from aleksis.core.util.core_helpers import get_site_preferences, objectgetter_optional
+from aleksis.core.util.core_helpers import (
+    get_site_preferences,
+    is_celery_enabled,
+    objectgetter_optional,
+)
 
 from .forms import (
     ExcuseTypeForm,
@@ -765,15 +769,25 @@ class DataCheckView(ListView):
     template_name = "alsijil/data_check/list.html"
     context_object_name = "results"
 
-    def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
-        context = super().get_context_data(**kwargs)
-        check_data()
-        return context
-
     def get_queryset(self) -> QuerySet:
         return DataCheckResult.objects.filter(solved=False).order_by("check")
 
 
+def run_data_checks(request: HttpRequest) -> HttpResponse:
+    check_data()
+    if is_celery_enabled():
+        messages.success(
+            request,
+            _(
+                "The data check has been started. Please note that it may take "
+                "a while before you are able to fetch the data on this page."
+            ),
+        )
+    else:
+        messages.success(request, _("The data check has been finished."))
+    return redirect("check_data")
+
+
 def solve_data_check_view(
     request: HttpRequest, id_: int, solve_option: str = "default"
 ):
-- 
GitLab