Self hosted FLOSS fitness/workout, nutrition and weight tracker
Start adding custom managers to filter bases without translations
Roland Geiderc46535156d24
diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.pyindex f1a1746a5..34dca9910 100644--- a/wger/exercises/api/views.py+++ b/wger/exercises/api/views.py@@ -283,7 +283,7 @@ class ExerciseViewSet(viewsets.ReadOnlyModelViewSet):'term',OpenApiTypes.STR,OpenApiParameter.QUERY,- description='The name of the exercise to search"',+ description='The name of the exercise to search',required=True,),OpenApiParameter(diff --git a/wger/exercises/apps.py b/wger/exercises/apps.pyindex 47a1233f3..bde39e333 100644--- a/wger/exercises/apps.py+++ b/wger/exercises/apps.py@@ -24,6 +24,8 @@ class ExerciseConfig(AppConfig):def ready(self):import wger.exercises.signals+ import wger.exercises.checks+from actstream import registryregistry.register(self.get_model('Alias'))registry.register(self.get_model('Exercise'))diff --git a/wger/exercises/checks.py b/wger/exercises/checks.pynew file mode 100644index 000000000..cf0439056--- /dev/null+++ b/wger/exercises/checks.py@@ -0,0 +1,44 @@+# 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/managers.py b/wger/exercises/managers.pynew file mode 100644index 000000000..1dc96a267--- /dev/null+++ b/wger/exercises/managers.py@@ -0,0 +1,39 @@+# This file is part of wger Workout Manager <https://github.com/wger-project>.+# Copyright (C) 2013 - 2021 wger Team+#+# 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 Affero General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License+# along with this program. If not, see <http://www.gnu.org/licenses/>.++# Django+from django.db import models+from django.db.models import Count+++class ExerciseBaseManagerTranslations(models.Manager):+ """Returns all exercise bases that have at least one translation"""++ def get_queryset(self):+ return super().get_queryset().annotate(count=Count('exercises')).filter(count__gt=0)+++class ExerciseBaseManagerNoTranslations(models.Manager):+ """Returns all exercise bases that have no translations"""++ def get_queryset(self):+ return super().get_queryset().annotate(count=Count('exercises')).filter(count=0)+++class ExerciseBaseManagerAll(models.Manager):+ """Returns all exercise bases"""++ passdiff --git a/wger/exercises/models/base.py b/wger/exercises/models/base.pyindex 7a78cc54c..d8abe9e63 100644--- a/wger/exercises/models/base.py+++ b/wger/exercises/models/base.py@@ -35,6 +35,11 @@ from simple_history.models import HistoricalRecords# wgerfrom wger.core.models import Language+from wger.exercises.managers import (+ ExerciseBaseManagerAll,+ ExerciseBaseManagerNoTranslations,+ ExerciseBaseManagerTranslations,+)from wger.utils.constants import ENGLISH_SHORT_NAMEfrom wger.utils.models import (AbstractHistoryMixin,@@ -54,6 +59,13 @@ class ExerciseBase(AbstractLicenseModel, AbstractHistoryMixin, models.Model):Model for an exercise base"""+ objects = ExerciseBaseManagerTranslations()+ no_translations = ExerciseBaseManagerNoTranslations()+ all = ExerciseBaseManagerAll()+ """+ Custom Query Manager+ """+uuid = models.UUIDField(default=uuid.uuid4,editable=False,diff --git a/wger/settings_global.py b/wger/settings_global.pyindex 87fe1a410..e057b4d49 100644--- a/wger/settings_global.py+++ b/wger/settings_global.py@@ -171,8 +171,7 @@ TEMPLATES = ['django.template.loaders.filesystem.Loader','django.template.loaders.app_directories.Loader',],- 'debug':- False+ 'debug': False},},]