Self hosted FLOSS fitness/workout, nutrition and weight tracker
Refactor the exercises-health-check.py management command
Roland Geider69e2bb0063b0
diff --git a/wger/core/models/profile.py b/wger/core/models/profile.pyindex 8edda7b73..ba7ca646f 100644--- a/wger/core/models/profile.py+++ b/wger/core/models/profile.py@@ -523,8 +523,8 @@ by the US Department of Agriculture. It is extremely complete, with around"""if (not WeightEntry.objects.filter(user=self.user).exists() or (- datetime.date.today() - WeightEntry.objects.filter(user=self.user).latest().date- > datetime.timedelta(days=3)+ datetime.date.today() - WeightEntry.objects.filter(user=self.user).latest().date >+ datetime.timedelta(days=3))):entry = WeightEntry()diff --git a/wger/core/tests/base_testcase.py b/wger/core/tests/base_testcase.pyindex b76b5ba59..b4234768f 100644--- a/wger/core/tests/base_testcase.py+++ b/wger/core/tests/base_testcase.py@@ -100,6 +100,8 @@ class BaseTestCase:REST API tests"""+ media_root = None+fixtures = ('days_of_week','gym_config',@@ -151,10 +153,6 @@ class BaseTestCase:# Set logging levellogging.disable(logging.INFO)- # Set MEDIA_ROOT- self.media_root = tempfile.mkdtemp()- settings.MEDIA_ROOT = self.media_root-# Disable django-axes# https://django-axes.readthedocs.io/en/latest/3_usage.html#authenticating-userssettings.AXES_ENABLED = False@@ -170,7 +168,33 @@ class BaseTestCase:cache.clear()# Clear MEDIA_ROOT folder- shutil.rmtree(self.media_root)+ if self.media_root:+ shutil.rmtree(self.media_root)++ def init_media_root(self):+ """+ Init the media root and copy the used images to it++ This is error-prone and ugly, but it's probably ok for the time being+ """+ self.media_root = tempfile.mkdtemp()+ settings.MEDIA_ROOT = self.media_root++ os.makedirs(self.media_root + '/exercise-images/1/')+ os.makedirs(self.media_root + '/exercise-images/2/')++ shutil.copy(+ 'wger/exercises/tests/protestschwein.jpg',+ self.media_root + '/exercise-images/1/protestschwein.jpg'+ )+ shutil.copy(+ 'wger/exercises/tests/wildschwein.jpg',+ self.media_root + '/exercise-images/1/wildschwein.jpg'+ )+ shutil.copy(+ 'wger/exercises/tests/wildschwein.jpg',+ self.media_root + '/exercise-images/2/wildschwein.jpg'+ )class WgerTestCase(BaseTestCase, TestCase):diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.pyindex 34dca9910..82af85801 100644--- a/wger/exercises/api/views.py+++ b/wger/exercises/api/views.py@@ -37,6 +37,7 @@ from drf_spectacular.utils import (inline_serializer,)from easy_thumbnails.alias import aliases+from easy_thumbnails.exceptions import InvalidImageFormatErrorfrom easy_thumbnails.files import get_thumbnailerfrom rest_framework import viewsetsfrom rest_framework.decorators import (@@ -348,7 +349,11 @@ def search(request):image_obj = translation.main_imageimage = image_obj.image.urlt = get_thumbnailer(image_obj.image)- thumbnail = t.get_thumbnail(aliases.get('micro_cropped')).url+ thumbnail = None+ try:+ thumbnail = t.get_thumbnail(aliases.get('micro_cropped')).url+ except InvalidImageFormatError:+ passresult_json = {'value': translation.name,diff --git a/wger/exercises/fixtures/test-exercises.json b/wger/exercises/fixtures/test-exercises.jsonindex 19e755112..7c7ce93b1 100644--- a/wger/exercises/fixtures/test-exercises.json+++ b/wger/exercises/fixtures/test-exercises.json@@ -27,7 +27,7 @@"pk": 1,"model": "exercises.exercisebase","fields": {- "uuid": "acad394936fb44819a72be2ddae2bc05",+ "uuid": "acad3949-36fb-4481-9a72-be2ddae2bc05","category": 2,"creation_date": "2020-01-10","update_date": "2020-11-01T22:10:00+01:00",@@ -63,7 +63,7 @@"pk": 3,"model": "exercises.exercisebase","fields": {- "uuid": "c2dc11e9b14a49cb9d11d1b9441f16f4",+ "uuid": "c2dc11e9-b14a-49cb-9d11-d1b9441f16f4","category": 3,"creation_date": "2020-01-10","update_date": "2020-11-01T22:10:00+01:00",@@ -141,7 +141,7 @@"model": "exercises.exercise","fields": {"uuid": "9838235ce38f4ca6921e9d237d8e0813",- "language": 1,+ "language": 2,"exercise_base": 1,"description": "Lorem ipsum dolor sit amet","name": "An exercise",@@ -167,7 +167,7 @@"model": "exercises.exercise","fields": {"uuid": "946afe7b54a644a69c36c3e31e6b4c3b",- "language": 1,+ "language": 2,"exercise_base": 3,"description": "","name": "Boring exercise",@@ -180,7 +180,7 @@"model": "exercises.exercise","fields": {"uuid": "5e80d257-3ece-43a1-b01d-2b28c6c288be",- "language": 1,+ "language": 2,"description": "","exercise_base": 4,"license_author": "test",@@ -235,7 +235,7 @@"model": "exercises.exercise","fields": {"uuid": "e75c421a-6839-4c14-b89a-bd2023818089",- "language": 1,+ "language": 2,"exercise_base": 5,"description": "","name": "Needed for demo user",diff --git a/wger/exercises/management/commands/exercises-health-check.py b/wger/exercises/management/commands/exercises-health-check.pyindex 5b8717b5c..803e1345d 100644--- a/wger/exercises/management/commands/exercises-health-check.py+++ b/wger/exercises/management/commands/exercises-health-check.py@@ -14,6 +14,7 @@# Standard Libraryimport collections+from argparse import RawTextHelpFormatter# Djangofrom django.core.management.base import BaseCommand@@ -28,74 +29,116 @@ class Command(BaseCommand):"""Performs some sanity checks on the exercise database"""+ english: Language- help = """Performs some sanity checks on the database+ help = "Performs some sanity checks on the exercise database. " \+ "At the moment this script checks that each exercise:\n" \+ "- has at least one translation\n" \+ "- has a translation in English\n" \+ "- has no duplicate translations\n\n" \+ "Each problem can be fixed individually by using the --delete-* flags\n"- At the moment this script checks the following:- - each base has at least one exercise- - each exercise base has a translation in English- - exercise bases have no duplicate translations- """+ def create_parser(self, *args, **kwargs):+ parser = super(Command, self).create_parser(*args, **kwargs)+ parser.formatter_class = RawTextHelpFormatter+ return parserdef add_arguments(self, parser):- # Add dry run argumentparser.add_argument(- '--delete',+ '--delete-untranslated',action='store_true',- dest='delete',+ dest='delete_untranslated',default=False,- help='Delete problematic exercise bases from the database (use with care!!)',+ help="Delete exercises without translations (safe to use since these can't be "+ "accessed over the UI)",)- def handle(self, **options):-- delete = options['delete']- english = Language.objects.get(short_name=ENGLISH_SHORT_NAME)-- for base in ExerciseBase.objects.all():-- if not base.exercises.count():- warning = f'Exercise base {base.uuid} has no translations!'- self.stdout.write(self.style.WARNING(warning))-- if delete:- base.delete()- self.stdout.write(' Deleting base...')- continue-- if not base.exercises.filter(language=english).exists():- warning = f'Exercise base {base.uuid} has no English translation!'- self.stdout.write(self.style.WARNING(warning))-- if delete:- base.delete()- self.stdout.write(' Deleting base...')+ parser.add_argument(+ '--delete-duplicate-translations',+ action='store_true',+ dest='delete_duplicates',+ default=False,+ help='Delete duplicate translations (e.g. if an exercise has two French entries, '+ 'the first one will be removed)',+ )- exercise_languages = base.exercises.values_list('language_id', flat=True)- duplicates = [- item for item, count in collections.Counter(exercise_languages).items() if count > 1- ]+ parser.add_argument(+ '--delete-no-english',+ action='store_true',+ dest='delete_no_english',+ default=False,+ help='Delete exercises without a fallback English translations',+ )- if not duplicates:- continue+ parser.add_argument(+ '--delete-all',+ action='store_true',+ dest='delete_all',+ default=False,+ help='Sets all deletion flags to true',+ )- warning = f'Exercise base {base.uuid} has duplicate translations for language IDs: {duplicates}!'- self.stdout.write(self.style.WARNING(warning))+ def handle(self, **options):- # Output the duplicates- for language_id in duplicates:- exercises = base.exercises.filter(language_id=language_id)- self.stdout.write(f'Language {language_id}:')- for exercise in exercises:- self.stdout.write(f' * {exercise.name} (uuid: {exercise.uuid} )')- self.stdout.write('')+ delete_untranslated = options['delete_untranslated'] or options['delete_all']+ delete_duplicates = options['delete_duplicates'] or options['delete_all']+ delete_no_english = options['delete_no_english'] or options['delete_all']++ self.english = Language.objects.get(short_name=ENGLISH_SHORT_NAME)++ for base in ExerciseBase.all.all():+ self.handle_untranslated(base, delete_untranslated)+ self.handle_no_english(base, delete_no_english)+ self.handle_duplicate_translations(base, delete_duplicates)++ def handle_untranslated(self, base: ExerciseBase, delete: bool):+ """+ Delete exercises without translations+ """+ if not base.pk or base.exercises.count():+ return++ self.stdout.write(self.style.WARNING(f'Exercise {base.uuid} has no translations!'))+ if delete:+ base.delete()+ self.stdout.write(' -> deleted')++ def handle_no_english(self, base: ExerciseBase, delete: bool):+ if not base.pk or base.exercises.filter(language=self.english).exists():+ return++ self.stdout.write(self.style.WARNING(f'Exercise {base.uuid} has no English translation!'))+ if delete:+ base.delete()+ self.stdout.write(' -> deleted')++ def handle_duplicate_translations(self, base: ExerciseBase, delete: bool):+ if not base.pk:+ return++ exercise_languages = base.exercises.values_list('language', flat=True)+ duplicates = [+ Language.objects.get(pk=item)+ for item, count in collections.Counter(exercise_languages).items() if count > 1+ ]++ if not duplicates:+ return++ warning = f'Exercise {base.uuid} has duplicate translations!'+ self.stdout.write(self.style.WARNING(warning))++ # Output the duplicates+ for language in duplicates:+ translations = base.exercises.filter(language=language)+ self.stdout.write(f'language {language.short_name}:')+ for translation in translations:+ self.stdout.write(f' * {translation.name} {translation.uuid}')+ self.stdout.write('')# And delete them- exercises = base.exercises.filter(language_id__in=duplicates)if delete:- for exercise in exercises[1:]:- self.stdout.write(- f' Deleting translation {exercise.uuid} for language ID {exercise.language_id}...'- )- exercise.delete()+ self.stdout.write(f' Deleting all but first {language.short_name} translation')+ for translation in translations[1:]:+ translation.delete()diff --git a/wger/exercises/models/base.py b/wger/exercises/models/base.pyindex 67aaad7f7..032c373c2 100644--- a/wger/exercises/models/base.py+++ b/wger/exercises/models/base.py@@ -127,7 +127,7 @@ class ExerciseBase(AbstractLicenseModel, AbstractHistoryMixin, models.Model):@propertydef total_authors_history(self):"""- All athors history related to the BaseExercise.+ All authors history related to the BaseExercise."""collect_for_models = [*self.exercises.all(),@@ -159,7 +159,7 @@ class ExerciseBase(AbstractLicenseModel, AbstractHistoryMixin, models.Model):'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"',+ 'exercises-health-check --help"',id='wger.W002',))diff --git a/wger/exercises/tests/test_exercise_translation.py b/wger/exercises/tests/test_exercise_translation.pyindex 44ec9b0c7..4aae9df49 100644--- a/wger/exercises/tests/test_exercise_translation.py+++ b/wger/exercises/tests/test_exercise_translation.py@@ -75,7 +75,7 @@ class ExercisesTestCase(WgerTestCase):self.assertEqual(result['suggestions'][0]['data']['image_thumbnail'], None)# 0 hits, "Pending exercise"- response = self.client.get(reverse('exercise-search'), {'term': 'Pending'})+ response = self.client.get(reverse('exercise-search'), {'term': 'Foobar'})self.assertEqual(response.status_code, 200)result = json.loads(response.content.decode('utf8'))self.assertEqual(len(result['suggestions']), 0)@@ -340,14 +340,14 @@ class ExerciseCustomApiTestCase(ExerciseCrudApiTestCase):exercise translation."""exercise = Exercise.objects.get(pk=self.pk)- self.assertEqual(exercise.language_id, 1)+ self.assertEqual(exercise.language_id, 2)self.authenticate('trainer1')- response = self.client.patch(self.url_detail, data={'language': 2})+ response = self.client.patch(self.url_detail, data={'language': 1})self.assertEqual(response.status_code, status.HTTP_200_OK)exercise = Exercise.objects.get(pk=self.pk)- self.assertEqual(exercise.exercise_base_id, 1)+ self.assertEqual(exercise.language_id, 2)def test_cant_change_license(self):"""diff --git a/wger/exercises/tests/test_management_commands.py b/wger/exercises/tests/test_management_commands.pyindex 9f4b71cea..a9d76e7fb 100644--- a/wger/exercises/tests/test_management_commands.py+++ b/wger/exercises/tests/test_management_commands.py@@ -11,16 +11,23 @@# GNU General Public License for more details.## You should have received a copy of the GNU Affero General Public License-# Standard Library+from io import StringIOfrom unittest.mock import patch# Djangofrom django.core.management import call_commandfrom django.test import SimpleTestCase+# wger+from wger.core.tests.base_testcase import WgerTestCase+from wger.exercises.models import (+ Exercise,+ ExerciseBase,+)+-class TestManagementCommands(SimpleTestCase):+class TestSyncManagementCommands(SimpleTestCase):@patch('wger.exercises.sync.delete_entries')@patch('wger.exercises.sync.sync_muscles')@@ -60,3 +67,70 @@ class TestManagementCommands(SimpleTestCase):call_command('download-exercise-videos')mock_download_exercise_videos.assert_called()+++class TestHealthCheckManagementCommands(WgerTestCase):++ def setUp(self):+ super().setUp()+ self.out = StringIO()++ def test_find_no_problems(self):+ call_command('exercises-health-check', stdout=self.out)+ self.assertEqual('', self.out.getvalue())++ def test_find_untranslated(self):+ Exercise.objects.get(pk=1).delete()+ Exercise.objects.get(pk=5).delete()++ call_command('exercises-health-check', stdout=self.out)+ self.assertIn(+ "Exercise acad3949-36fb-4481-9a72-be2ddae2bc05 has no translations!",+ self.out.getvalue()+ )+ self.assertNotIn("-> deleted", self.out.getvalue())++ def atest_fix_untranslated(self):+ Exercise.objects.get(pk=1).delete()++ call_command('exercises-health-check', '--delete-untranslated', stdout=self.out)+ self.assertIn("-> deleted", self.out.getvalue())+ self.assertRaises(ExerciseBase.DoesNotExist, ExerciseBase.objects.get, pk=1)++ def test_find_no_english_translation(self):+ Exercise.objects.get(pk=1).delete()++ call_command('exercises-health-check', stdout=self.out)+ self.assertIn(+ "Exercise acad3949-36fb-4481-9a72-be2ddae2bc05 has no English translation!",+ self.out.getvalue()+ )+ self.assertNotIn("-> deleted", self.out.getvalue())++ def test_fix_no_english_translation(self):+ Exercise.objects.get(pk=1).delete()++ call_command('exercises-health-check', '--delete-no-english', stdout=self.out)+ self.assertIn("-> deleted", self.out.getvalue())+ self.assertRaises(ExerciseBase.DoesNotExist, ExerciseBase.objects.get, pk=1)++ def test_find_duplicate_translations(self):+ exercise = Exercise.objects.get(pk=1)+ exercise.language_id = 3+ exercise.save()++ call_command('exercises-health-check', stdout=self.out)+ self.assertIn(+ "Exercise acad3949-36fb-4481-9a72-be2ddae2bc05 has duplicate translations!",+ self.out.getvalue()+ )+ self.assertNotIn("-> deleted", self.out.getvalue())++ def test_fix_duplicate_translations(self):+ exercise = Exercise.objects.get(pk=1)+ exercise.language_id = 3+ exercise.save()++ call_command('exercises-health-check', '--delete-duplicate-translations', stdout=self.out)+ self.assertIn("Deleting all but first fr translation", self.out.getvalue())+ self.assertRaises(Exercise.DoesNotExist, Exercise.objects.get, pk=5)diff --git a/wger/exercises/tests/test_search_api.py b/wger/exercises/tests/test_search_api.pyindex 87214c264..2849cbdff 100644--- a/wger/exercises/tests/test_search_api.py+++ b/wger/exercises/tests/test_search_api.py@@ -24,6 +24,10 @@ from wger.core.tests.base_testcase import BaseTestCaseclass SearchExerciseApiTestCase(BaseTestCase, ApiBaseTestCase):url = '/api/v2/exercise/search/'+ def setUp(self):+ super().setUp()+ self.init_media_root()+def test_basic_search_logged_out(self):"""Logged-out users are also allowed to use the search@@ -32,9 +36,9 @@ class SearchExerciseApiTestCase(BaseTestCase, ApiBaseTestCase):result1 = response.data['suggestions'][0]self.assertEqual(response.status_code, status.HTTP_200_OK)- self.assertEqual(len(response.data['suggestions']), 1)- self.assertEqual(result1['value'], 'Very cool exercise')- self.assertEqual(result1['data']['id'], 2)+ self.assertEqual(len(response.data['suggestions']), 4)+ self.assertEqual(result1['value'], 'An exercise')+ self.assertEqual(result1['data']['id'], 1)def test_basic_search_logged_in(self):"""@@ -45,9 +49,9 @@ class SearchExerciseApiTestCase(BaseTestCase, ApiBaseTestCase):result1 = response.data['suggestions'][0]self.assertEqual(response.status_code, status.HTTP_200_OK)- self.assertEqual(len(response.data['suggestions']), 1)- self.assertEqual(result1['value'], 'Very cool exercise')- self.assertEqual(result1['data']['id'], 2)+ self.assertEqual(len(response.data['suggestions']), 4)+ self.assertEqual(result1['value'], 'An exercise')+ self.assertEqual(result1['data']['id'], 1)def test_search_language_code_en(self):"""@@ -57,9 +61,9 @@ class SearchExerciseApiTestCase(BaseTestCase, ApiBaseTestCase):result1 = response.data['suggestions'][0]self.assertEqual(response.status_code, status.HTTP_200_OK)- self.assertEqual(len(response.data['suggestions']), 1)- self.assertEqual(result1['value'], 'Very cool exercise')- self.assertEqual(result1['data']['id'], 2)+ self.assertEqual(len(response.data['suggestions']), 4)+ self.assertEqual(result1['value'], 'An exercise')+ self.assertEqual(result1['data']['id'], 1)def test_search_language_code_en_no_results(self):"""