From 7e9c9b666b3bd59f60f420368607fe0429a1352f Mon Sep 17 00:00:00 2001
From: Jonathan Weth <git@jonathanweth.de>
Date: Sat, 19 Sep 2020 19:05:00 +0200
Subject: [PATCH] Add missing migration

---
 aleksis/apps/alsijil/data_checks.py           |  3 +
 .../migrations/0008_data_check_result.py      | 83 +++++++++++++++++++
 .../templates/alsijil/data_check/list.html    |  2 +-
 aleksis/apps/alsijil/urls.py                  |  1 +
 aleksis/apps/alsijil/views.py                 | 26 ++++--
 5 files changed, 108 insertions(+), 7 deletions(-)
 create mode 100644 aleksis/apps/alsijil/migrations/0008_data_check_result.py

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/migrations/0008_data_check_result.py b/aleksis/apps/alsijil/migrations/0008_data_check_result.py
new file mode 100644
index 000000000..7ea8edfd2
--- /dev/null
+++ b/aleksis/apps/alsijil/migrations/0008_data_check_result.py
@@ -0,0 +1,83 @@
+# Generated by Django 3.0.10 on 2020-09-19 10:45
+
+import django.contrib.postgres.fields.jsonb
+import django.contrib.sites.managers
+import django.db.models.deletion
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ("contenttypes", "0002_remove_content_type_name"),
+        ("sites", "0002_alter_domain_unique"),
+        ("alsijil", "0007_personal_note_lesson_documentation_year"),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name="DataCheckResult",
+            fields=[
+                (
+                    "id",
+                    models.AutoField(
+                        auto_created=True,
+                        primary_key=True,
+                        serialize=False,
+                        verbose_name="ID",
+                    ),
+                ),
+                (
+                    "extended_data",
+                    django.contrib.postgres.fields.jsonb.JSONField(
+                        default=dict, editable=False
+                    ),
+                ),
+                (
+                    "check",
+                    models.CharField(
+                        choices=[
+                            (
+                                "no_personal_notes_in_cancelled_lessons",
+                                "Ensure that there are no personal notes in cancelled lessons",
+                            )
+                        ],
+                        max_length=255,
+                        verbose_name="Related data check task",
+                    ),
+                ),
+                ("object_id", models.CharField(max_length=255)),
+                (
+                    "solved",
+                    models.BooleanField(default=False, verbose_name="Issue solved"),
+                ),
+                (
+                    "sent",
+                    models.BooleanField(
+                        default=False, verbose_name="Notification sent"
+                    ),
+                ),
+                (
+                    "content_type",
+                    models.ForeignKey(
+                        on_delete=django.db.models.deletion.CASCADE,
+                        to="contenttypes.ContentType",
+                    ),
+                ),
+                (
+                    "site",
+                    models.ForeignKey(
+                        default=1,
+                        editable=False,
+                        on_delete=django.db.models.deletion.CASCADE,
+                        to="sites.Site",
+                    ),
+                ),
+            ],
+            options={
+                "verbose_name": "Data check result",
+                "verbose_name_plural": "Data check results",
+            },
+            managers=[("objects", django.contrib.sites.managers.CurrentSiteManager()),],
+        ),
+    ]
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