Self hosted FLOSS fitness/workout, nutrition and weight tracker
Move the check function to the model
Roland Geider59c800cbe46e
diff --git a/wger/exercises/apps.py b/wger/exercises/apps.pyindex bde39e333..66a7f3c85 100644--- a/wger/exercises/apps.py+++ b/wger/exercises/apps.py@@ -24,7 +24,6 @@ class ExerciseConfig(AppConfig):def ready(self):import wger.exercises.signals- import wger.exercises.checksfrom actstream import registryregistry.register(self.get_model('Alias'))diff --git a/wger/exercises/checks.py b/wger/exercises/checks.pydeleted file mode 100644index cf0439056..000000000--- a/wger/exercises/checks.py+++ /dev/null@@ -1,44 +0,0 @@-# This file is part of wger Workout Manager.-#-# wger Workout Manager is free software: you can redistribute it and/or modify-# it under the terms of the GNU Affero General Public License as published by-# the Free Software Foundation, either version 3 of the License, or-# (at your option) any later version.-#-# wger Workout Manager is distributed in the hope that it will be useful,-# but WITHOUT ANY WARRANTY; without even the implied warranty of-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-# GNU General Public License for more details.-#-# You should have received a copy of the GNU Affero General Public License-# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.--# Django-from django.conf import settings-from django.core.checks import (- Warning,- register,-)---@register()-def exercise_checks(app_configs, **kwargs):- errors = []-- # All bases should have at least one translation- # wger- from wger.exercises.models import ExerciseBase- no_translations = ExerciseBase.no_translations.all().count()- if no_translations:- errors.append(- Warning(- 'exercises without translations',- hint=f'There are {no_translations} exercises without translations, this will '- 'cause problems! You can output or delete them with "python manage.py '- 'exercises-health-check"',- obj=settings,- id='wger.W002',- )- )-- return errorsdiff --git a/wger/exercises/models/base.py b/wger/exercises/models/base.pyindex d8abe9e63..67aaad7f7 100644--- a/wger/exercises/models/base.py+++ b/wger/exercises/models/base.py@@ -22,6 +22,7 @@ from typing import ()# Django+from django.core.checks import Warningfrom django.db import modelsfrom django.db.models import Qfrom django.urls import reverse@@ -147,6 +148,24 @@ class ExerciseBase(AbstractLicenseModel, AbstractHistoryMixin, models.Model):"""return reverse('exercise:exercise:view-base', kwargs={'pk': self.id})+ @classmethod+ def check(cls, **kwargs):+ errors = super().check(**kwargs)++ no_translations = cls.no_translations.all().count()+ if no_translations:+ errors.append(+ Warning(+ 'exercises without translations',+ hint=f'There are {no_translations} exercises without translations, this will '+ 'cause problems! You can output or delete them with "python manage.py '+ 'exercises-health-check"',+ id='wger.W002',+ )+ )++ return errors+## Own methods#diff --git a/wger/exercises/tests/test_exercise_base.py b/wger/exercises/tests/test_exercise_base.pyindex be6eca402..337602744 100644--- a/wger/exercises/tests/test_exercise_base.py+++ b/wger/exercises/tests/test_exercise_base.py@@ -135,17 +135,22 @@ class ExerciseCustomApiTestCase(ExerciseCrudApiTestCase):self.assertEqual(exercise.license_id, CC_BY_SA_4_ID)-class ExerciseBaseManagerTestCase(WgerTestCase):+class ExerciseBaseTranslationHandlingTestCase(WgerTestCase):"""- Test the custom managers for the exercise bases+ Test the logic used to handle bases without translations"""- def test_managers(self):- base = ExerciseBase.objects.get(pk=1)+ def setUp(self):+ super().setUp()Exercise.objects.get(pk=1).delete()Exercise.objects.get(pk=5).delete()- self.assertEqual(base.exercises.count(), 0)+ def test_managers(self):self.assertEqual(ExerciseBase.objects.all().count(), 7)self.assertEqual(ExerciseBase.no_translations.all().count(), 1)self.assertEqual(ExerciseBase.all.all().count(), 8)++ def test_checks(self):+ out = ExerciseBase.check()+ self.assertEqual(len(out), 1)+ self.assertEqual(out[0].id, 'wger.W002')