Self hosted FLOSS fitness/workout, nutrition and weight tracker
Further renaming base to exercise
Roland Geiderb1bfb7770102
diff --git a/wger/exercises/management/commands/change-exercise-author.py b/wger/exercises/management/commands/change-exercise-author.pyindex 9127fb822..c55e65eb4 100644--- a/wger/exercises/management/commands/change-exercise-author.py+++ b/wger/exercises/management/commands/change-exercise-author.py@@ -39,47 +39,50 @@ class Command(BaseCommand):'--author-name', action='store', dest='author_name', help='The name of the new author')parser.add_argument(- '--exercise-base-id',+ '--exercise-id',action='store',- dest='exercise_base_id',+ dest='exercise_id',help='The ID of the exercise base',)parser.add_argument(- '--exercise-id', action='store', dest='exercise_id', help='The ID of the exercise'+ '--translation-id',+ action='store',+ dest='translation_id',+ help='The ID of the exercise',)def handle(self, **options):author_name = options['author_name']- exercise_base_id = options['exercise_base_id']exercise_id = options['exercise_id']+ translation_id = options['translation_id']if author_name is None:self.print_error('Please enter an author name')return- if exercise_base_id is None and exercise_id is None:- self.print_error('Please enter an exercise base or exercise ID')+ if exercise_id is None and translation_id is None:+ self.print_error('Please enter an exercise or translation ID')return- if exercise_base_id is not None:+ if exercise_id is not None:try:- exercise_base = Exercise.objects.get(id=exercise_base_id)+ exercise_base = Exercise.objects.get(id=exercise_id)except Exercise.DoesNotExist:self.print_error('Failed to find exercise base')returnexercise_base.license_author = author_nameexercise_base.save()- if exercise_id is not None:+ if translation_id is not None:try:- exercise = Translation.objects.get(id=exercise_id)+ exercise = Translation.objects.get(id=translation_id)except Exercise.DoesNotExist:self.print_error('Failed to find exercise')returnexercise.license_author = author_nameexercise.save()- self.stdout.write(self.style.SUCCESS(f'Exercise and/or exercise base has been updated'))+ self.stdout.write(self.style.SUCCESS(f'Exercise and/or translation has been updated'))def print_error(self, error_message):self.stdout.write(self.style.WARNING(f'{error_message}'))diff --git a/wger/exercises/managers.py b/wger/exercises/managers.pyindex 6a37e8d0f..06cd136b0 100644--- a/wger/exercises/managers.py+++ b/wger/exercises/managers.py@@ -19,21 +19,21 @@ from django.db import modelsfrom django.db.models import Count-class ExerciseBaseManagerTranslations(models.Manager):- """Returns all exercise bases that have at least one translation"""+class ExerciseManagerTranslations(models.Manager):+ """Returns all exercises that have at least one translation"""def get_queryset(self):return super().get_queryset().annotate(count=Count('translations')).filter(count__gt=0)-class ExerciseBaseManagerNoTranslations(models.Manager):- """Returns all exercise bases that have no translations"""+class ExerciseManagerNoTranslations(models.Manager):+ """Returns all exercises that have no translations"""def get_queryset(self):return super().get_queryset().annotate(count=Count('translations')).filter(count=0)-class ExerciseBaseManagerAll(models.Manager):- """Returns all exercise bases"""+class ExerciseManagerAll(models.Manager):+ """Returns all exercises"""passdiff --git a/wger/exercises/models/base.py b/wger/exercises/models/base.pyindex 8e853656c..b77382741 100644--- a/wger/exercises/models/base.py+++ b/wger/exercises/models/base.py@@ -41,9 +41,9 @@ from simple_history.models import HistoricalRecords# wgerfrom wger.core.models import Languagefrom wger.exercises.managers import (- ExerciseBaseManagerAll,- ExerciseBaseManagerNoTranslations,- ExerciseBaseManagerTranslations,+ ExerciseManagerAll,+ ExerciseManagerNoTranslations,+ ExerciseManagerTranslations,)from wger.utils.cache import reset_exercise_api_cachefrom wger.utils.constants import ENGLISH_SHORT_NAME@@ -65,9 +65,9 @@ class Exercise(AbstractLicenseModel, AbstractHistoryMixin, models.Model):Model for an exercise base"""- objects = ExerciseBaseManagerAll()- no_translations = ExerciseBaseManagerNoTranslations()- with_translations = ExerciseBaseManagerTranslations()+ objects = ExerciseManagerAll()+ no_translations = ExerciseManagerNoTranslations()+ with_translations = ExerciseManagerTranslations()"""Custom Query Manager"""diff --git a/wger/exercises/sync.py b/wger/exercises/sync.pyindex 22a238cc6..173eb70ee 100644--- a/wger/exercises/sync.py+++ b/wger/exercises/sync.py@@ -84,16 +84,16 @@ def sync_exercises(muscles = [Muscle.objects.get(pk=i['id']) for i in data['muscles']]muscles_sec = [Muscle.objects.get(pk=i['id']) for i in data['muscles_secondary']]- base, base_created = Exercise.objects.update_or_create(+ exercise, exercise_created = Exercise.objects.update_or_create(uuid=uuid,defaults={'category_id': category_id, 'created': created},)- print_fn(f"{'created' if base_created else 'updated'} exercise {uuid}")+ print_fn(f"{'created' if exercise_created else 'updated'} exercise {uuid}")- base.muscles.set(muscles)- base.muscles_secondary.set(muscles_sec)- base.equipment.set(equipment)- base.save()+ exercise.muscles.set(muscles)+ exercise.muscles_secondary.set(muscles_sec)+ exercise.equipment.set(equipment)+ exercise.save()for translation_data in data['exercises']:trans_uuid = translation_data['uuid']@@ -104,7 +104,7 @@ def sync_exercises(translation, translation_created = Translation.objects.update_or_create(uuid=trans_uuid,defaults={- 'exercise_base': base,+ 'exercise_base': exercise,'name': name,'description': description,'license_id': license_id,@@ -395,7 +395,7 @@ def download_exercise_images(try:exercise = Exercise.objects.get(uuid=image_data['exercise_base_uuid'])except Exercise.DoesNotExist:- print_fn(' Remote exercise base not found in local DB, skipping...')+ print_fn(' Remote exercise not found in local DB, skipping...')continuetry:@@ -427,7 +427,7 @@ def download_exercise_videos(try:exercise = Exercise.objects.get(uuid=video_data['exercise_base_uuid'])except Exercise.DoesNotExist:- print_fn(' Remote exercise base not found in local DB, skipping...')+ print_fn(' Remote exercise not found in local DB, skipping...')continuetry:diff --git a/wger/exercises/tests/test_change_exercise_author.py b/wger/exercises/tests/test_change_exercise_author.pyindex 1098aad2c..8b1d6e377 100644--- a/wger/exercises/tests/test_change_exercise_author.py+++ b/wger/exercises/tests/test_change_exercise_author.py@@ -46,7 +46,7 @@ class ChangeExerciseAuthorTestCase(WgerTestCase):"""args = ['--author-name', 'tom']call_command('change-exercise-author', *args, stdout=self.out, no_color=True)- self.assertIn('Please enter an exercise base or exercise ID', self.out.getvalue())+ self.assertIn('Please enter an exercise or translation ID', self.out.getvalue())def test_can_update_exercise_base(self):"""@@ -55,9 +55,9 @@ class ChangeExerciseAuthorTestCase(WgerTestCase):exercise = Exercise.objects.get(id=2)self.assertNotEqual(exercise.license_author, 'tom')- args = ['--author-name', 'tom', '--exercise-base-id', '2']+ args = ['--author-name', 'tom', '--exercise-id', '2']call_command('change-exercise-author', *args, stdout=self.out, no_color=True)- self.assertIn('Exercise and/or exercise base has been updated', self.out.getvalue())+ self.assertIn('Exercise and/or translation has been updated', self.out.getvalue())exercise = Exercise.objects.get(id=2)self.assertEqual(exercise.license_author, 'tom')@@ -69,9 +69,9 @@ class ChangeExerciseAuthorTestCase(WgerTestCase):exercise = Translation.objects.get(id=1)self.assertNotEqual(exercise.license_author, 'tom')- args = ['--author-name', 'tom', '--exercise-id', '1']+ args = ['--author-name', 'tom', '--translation-id', '1']call_command('change-exercise-author', *args, stdout=self.out, no_color=True)- self.assertIn('Exercise and/or exercise base has been updated', self.out.getvalue())+ self.assertIn('Exercise and/or translation has been updated', self.out.getvalue())exercise = Translation.objects.get(id=1)self.assertEqual(exercise.license_author, 'tom')diff --git a/wger/exercises/tests/test_deletion_log.py b/wger/exercises/tests/test_deletion_log.pyindex d9d0a1a08..6d7ac97fc 100644--- a/wger/exercises/tests/test_deletion_log.py+++ b/wger/exercises/tests/test_deletion_log.py@@ -71,7 +71,7 @@ class DeletionLogTestCase(WgerTestCase):def test_exercise_with_replaced_by(self):"""- Test that an entry is generated when a base is deleted and the replaced by is+ Test that an entry is generated when a exercise is deleted and the replaced by isset correctly"""self.assertEqual(DeletionLog.objects.all().count(), 0)@@ -79,7 +79,7 @@ class DeletionLogTestCase(WgerTestCase):exercise = Exercise.objects.get(pk=1)exercise.delete(replace_by='ae3328ba-9a35-4731-bc23-5da50720c5aa')- # Base is deleted+ # Exercise is deletedlog = DeletionLog.objects.get(pk=1)self.assertEqual(log.model_type, DeletionLog.MODEL_EXERCISE)@@ -88,7 +88,7 @@ class DeletionLogTestCase(WgerTestCase):def test_exercise_with_nonexistent_replaced_by(self):"""- Test that an entry is generated when a base is deleted and the replaced by is+ Test that an entry is generated when an exercise is deleted and the replaced by isset correctly. If the UUID is not found in the DB, it's set to None"""self.assertEqual(DeletionLog.objects.all().count(), 0)@@ -96,7 +96,7 @@ class DeletionLogTestCase(WgerTestCase):exercise = Exercise.objects.get(pk=1)exercise.delete(replace_by='12345678-1234-1234-1234-1234567890ab')- # Base is deleted+ # Exercise is deletedlog = DeletionLog.objects.get(pk=1)self.assertEqual(log.model_type, DeletionLog.MODEL_EXERCISE)diff --git a/wger/exercises/tests/test_exercise_base.py b/wger/exercises/tests/test_exercise.pysimilarity index 97%rename from wger/exercises/tests/test_exercise_base.pyrename to wger/exercises/tests/test_exercise.pyindex 43f1d9ab1..5eb96b6f4 100644--- a/wger/exercises/tests/test_exercise_base.py+++ b/wger/exercises/tests/test_exercise.py@@ -29,9 +29,9 @@ from wger.exercises.models import (from wger.utils.constants import CC_BY_SA_4_ID-class ExerciseBaseTestCase(WgerTestCase):+class ExerciseTestCase(WgerTestCase):"""- Test the different features of an exercise and its base+ Test the different features of an exercise"""@staticmethod@@ -39,7 +39,7 @@ class ExerciseBaseTestCase(WgerTestCase):"""Helper to return the IDs of the objects in a queryset"""return sorted([i.id for i in queryset.all()])- def test_base(self):+ def test_exercise(self):"""Test that the properties return the correct data"""diff --git a/wger/exercises/tests/test_exercise_base_model.py b/wger/exercises/tests/test_exercise_base_model.pydeleted file mode 100644index 28445c24a..000000000--- a/wger/exercises/tests/test_exercise_base_model.py+++ /dev/null@@ -1,93 +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--# Standard Library-import datetime--# wger-from wger.core.tests.base_testcase import WgerTestCase-from wger.exercises.models import (- Exercise,- Translation,-)---class ExerciseBaseTranslationHandlingTestCase(WgerTestCase):- """- Test the logic used to handle bases without translations- """-- def setUp(self):- super().setUp()- Translation.objects.get(pk=1).delete()- Translation.objects.get(pk=5).delete()-- def test_managers(self):- self.assertEqual(Exercise.with_translations.all().count(), 7)- self.assertEqual(Exercise.no_translations.all().count(), 1)- self.assertEqual(Exercise.objects.all().count(), 8)-- def test_checks(self):- out = Exercise.check()- self.assertEqual(len(out), 1)- self.assertEqual(out[0].id, 'wger.W002')---class ExerciseBaseModelTestCase(WgerTestCase):- """- Test custom model logic- """-- exercise: Exercise-- def setUp(self):- super().setUp()- self.exercise = Exercise.objects.get(pk=1)-- def test_access_date(self):- utc = datetime.timezone.utc- self.assertEqual(- self.exercise.last_update_global, datetime.datetime(2023, 8, 9, 23, 0, tzinfo=utc)- )-- self.assertEqual(- self.exercise.last_update, datetime.datetime(2020, 11, 1, 21, 10, tzinfo=utc)- )-- self.assertEqual(- max(*[translation.last_update for translation in self.exercise.translations.all()]),- datetime.datetime(2022, 2, 2, 5, 45, 11, tzinfo=utc),- )-- self.assertEqual(- max(*[video.last_update for video in self.exercise.exerciseimage_set.all()]),- datetime.datetime(2023, 8, 9, 23, 0, tzinfo=utc),- )-- def test_exercise_en(self):- translation = self.exercise.get_translation()- self.assertEqual(translation.language.short_name, 'en')-- def test_get_exercise_fr(self):- translation = self.exercise.get_translation('fr')- self.assertEqual(translation.language.short_name, 'fr')-- def test_get_exercise_unknown(self):- translation = self.exercise.get_translation('kg')- self.assertEqual(translation.language.short_name, 'en')-- def test_get_languages(self):- languages = self.exercise.languages- self.assertEqual(languages[0].short_name, 'en')- self.assertEqual(languages[1].short_name, 'fr')diff --git a/wger/exercises/tests/test_exercise_model.py b/wger/exercises/tests/test_exercise_model.pyindex 11da16f6c..ecdf6ef04 100644--- a/wger/exercises/tests/test_exercise_model.py+++ b/wger/exercises/tests/test_exercise_model.py@@ -12,27 +12,84 @@## You should have received a copy of the GNU Affero General Public License+# Standard Library+import datetime+# wgerfrom wger.core.tests.base_testcase import WgerTestCase-from wger.exercises.models import Translation+from wger.exercises.models import (+ Exercise,+ Translation,+)+++class ExerciseTranslationHandlingTestCase(WgerTestCase):+ """+ Test the logic used to handle exercises without translations+ """++ def setUp(self):+ super().setUp()+ Translation.objects.get(pk=1).delete()+ Translation.objects.get(pk=5).delete()++ def test_managers(self):+ self.assertEqual(Exercise.with_translations.all().count(), 7)+ self.assertEqual(Exercise.no_translations.all().count(), 1)+ self.assertEqual(Exercise.objects.all().count(), 8)++ def test_checks(self):+ out = Exercise.check()+ self.assertEqual(len(out), 1)+ self.assertEqual(out[0].id, 'wger.W002')class ExerciseModelTestCase(WgerTestCase):"""- Test the logic in the exercise model+ Test custom model logic"""- def test_absolute_url_name(self):- """Test that the get_absolute_url returns the correct URL"""- exercise = Translation(exercise_base_id=1, description='abc', name='foo')- self.assertEqual(exercise.get_absolute_url(), '/en/exercise/1/view-base/foo')+ exercise: Exercise++ def setUp(self):+ super().setUp()+ self.exercise = Exercise.objects.get(pk=1)++ def test_access_date(self):+ utc = datetime.timezone.utc+ self.assertEqual(+ self.exercise.last_update_global,+ datetime.datetime(2023, 8, 9, 23, 0, tzinfo=utc),+ )++ self.assertEqual(+ self.exercise.last_update,+ datetime.datetime(2020, 11, 1, 21, 10, tzinfo=utc),+ )++ self.assertEqual(+ max(*[translation.last_update for translation in self.exercise.translations.all()]),+ datetime.datetime(2022, 2, 2, 5, 45, 11, tzinfo=utc),+ )++ self.assertEqual(+ max(*[video.last_update for video in self.exercise.exerciseimage_set.all()]),+ datetime.datetime(2023, 8, 9, 23, 0, tzinfo=utc),+ )++ def test_exercise_en(self):+ translation = self.exercise.get_translation()+ self.assertEqual(translation.language.short_name, 'en')++ def test_get_exercise_fr(self):+ translation = self.exercise.get_translation('fr')+ self.assertEqual(translation.language.short_name, 'fr')- def test_absolute_url_no_name(self):- """Test that the get_absolute_url returns the correct URL"""- exercise = Translation(exercise_base_id=2, description='abc', name='')- self.assertEqual(exercise.get_absolute_url(), '/en/exercise/2/view-base')+ def test_get_exercise_unknown(self):+ translation = self.exercise.get_translation('kg')+ self.assertEqual(translation.language.short_name, 'en')- def test_absolute_url_no_name2(self):- """Test that the get_absolute_url returns the correct URL"""- exercise = Translation(exercise_base_id=42, description='abc', name='@@@@@')- self.assertEqual(exercise.get_absolute_url(), '/en/exercise/42/view-base')+ def test_get_languages(self):+ languages = self.exercise.languages+ self.assertEqual(languages[0].short_name, 'en')+ self.assertEqual(languages[1].short_name, 'fr')diff --git a/wger/exercises/tests/test_exercise_translation.py b/wger/exercises/tests/test_exercise_translation.pyindex cab842417..54c72f46d 100644--- a/wger/exercises/tests/test_exercise_translation.py+++ b/wger/exercises/tests/test_exercise_translation.py@@ -94,16 +94,16 @@ class ExercisesTestCase(WgerTestCase):"""Test that changing exercise details generates a historical record"""- exercise = Translation.objects.get(pk=2)- self.assertEqual(len(exercise.history.all()), 0)+ translation = Translation.objects.get(pk=2)+ self.assertEqual(len(translation.history.all()), 0)- exercise.name = 'Very cool exercise 2'- exercise.description = 'New description'- exercise.exercise_base.muscles_secondary.add(Muscle.objects.get(pk=2))- exercise.save()+ translation.name = 'Very cool exercise 2'+ translation.description = 'New description'+ translation.exercise_base.muscles_secondary.add(Muscle.objects.get(pk=2))+ translation.save()- exercise = Translation.objects.get(pk=2)- self.assertEqual(len(exercise.history.all()), 1)+ translation = Translation.objects.get(pk=2)+ self.assertEqual(len(translation.history.all()), 1)class MuscleTemplateTagTest(WgerTestCase):@@ -275,10 +275,10 @@ class ExerciseCustomApiTestCase(ExerciseCrudApiTestCase):def get_resource_name(self):return 'exercise-translation'- def test_cant_change_base_id(self):+ def test_cant_change_exercise_id(self):"""- Test that it is not possible to change the base id of an existing- exercise translation.+ Test that it is not possible to change the exercise id of an existing+ translation."""translation = Translation.objects.get(pk=self.pk)self.assertEqual(translation.exercise_base_id, 1)diff --git a/wger/exercises/tests/test_translation_model.py b/wger/exercises/tests/test_translation_model.pynew file mode 100644index 000000000..c48aabf25--- /dev/null+++ b/wger/exercises/tests/test_translation_model.py@@ -0,0 +1,38 @@+# 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++# wger+from wger.core.tests.base_testcase import WgerTestCase+from wger.exercises.models import Translation+++class TranslationModelTestCase(WgerTestCase):+ """+ Test the logic in the translation model+ """++ def test_absolute_url_name(self):+ """Test that the get_absolute_url returns the correct URL"""+ translation = Translation(exercise_base_id=1, description='abc', name='foo')+ self.assertEqual(translation.get_absolute_url(), '/en/exercise/1/view-base/foo')++ def test_absolute_url_no_name(self):+ """Test that the get_absolute_url returns the correct URL"""+ translation = Translation(exercise_base_id=2, description='abc', name='')+ self.assertEqual(translation.get_absolute_url(), '/en/exercise/2/view-base')++ def test_absolute_url_no_name2(self):+ """Test that the get_absolute_url returns the correct URL"""+ translation = Translation(exercise_base_id=42, description='abc', name='@@@@@')+ self.assertEqual(translation.get_absolute_url(), '/en/exercise/42/view-base')