Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add orphan variation handling to exercises health check command
Roland Geidere5bc2d2c925b
diff --git a/wger/exercises/fixtures/test-exercises.json b/wger/exercises/fixtures/test-exercises.jsonindex 8c87352ef..3de1862eb 100644--- a/wger/exercises/fixtures/test-exercises.json+++ b/wger/exercises/fixtures/test-exercises.json@@ -13,20 +13,6 @@"uuid": "a1b2c3d4-0002-0000-0000-000000000002"}},- {- "pk": 3,- "model": "exercises.variation",- "fields": {- "uuid": "a1b2c3d4-0003-0000-0000-000000000003"- }- },- {- "pk": 4,- "model": "exercises.variation",- "fields": {- "uuid": "a1b2c3d4-0004-0000-0000-000000000004"- }- },{"pk": 1,"model": "exercises.exercise",diff --git a/wger/exercises/management/commands/exercises-health-check.py b/wger/exercises/management/commands/exercises-health-check.pyindex cc7440377..067ce0f2c 100644--- a/wger/exercises/management/commands/exercises-health-check.py+++ b/wger/exercises/management/commands/exercises-health-check.py@@ -19,9 +19,15 @@ from argparse import RawTextHelpFormatter# Djangofrom django.core.management.base import BaseCommand+# Django+from django.db.models import Count+# wgerfrom wger.core.models import Language-from wger.exercises.models import Exercise+from wger.exercises.models import (+ Exercise,+ Variation,+)from wger.utils.constants import ENGLISH_SHORT_NAME@@ -38,6 +44,7 @@ class Command(BaseCommand):'- has at least one translation\n''- has a translation in English\n''- has no duplicate translations\n\n'+ 'It also checks for orphaned variation groups (0 or 1 exercises).\n\n''Each problem can be fixed individually by using the --delete-* flags\n')@@ -64,6 +71,14 @@ class Command(BaseCommand):help='Delete exercises without a fallback English translations',)+ parser.add_argument(+ '--delete-orphan-variations',+ action='store_true',+ dest='delete_orphan_variations',+ default=False,+ help='Delete variation groups with 0 or 1 exercises',+ )+parser.add_argument('--delete-all',action='store_true',@@ -75,6 +90,7 @@ class Command(BaseCommand):def handle(self, **options):delete_untranslated = options['delete_untranslated'] or options['delete_all']delete_no_english = options['delete_no_english'] or options['delete_all']+ delete_orphan_variations = options['delete_orphan_variations'] or options['delete_all']self.english = Language.objects.get(short_name=ENGLISH_SHORT_NAME)@@ -82,6 +98,8 @@ class Command(BaseCommand):self.handle_untranslated(exercise, delete_untranslated)self.handle_no_english(exercise, delete_no_english)+ self.handle_orphan_variations(delete_orphan_variations)+def handle_untranslated(self, exercise: Exercise, delete: bool):"""Delete exercises without translations@@ -104,3 +122,22 @@ class Command(BaseCommand):if delete:exercise.delete()self.stdout.write(' -> deleted')++ def handle_orphan_variations(self, delete: bool):+ """+ Find and optionally delete variation groups with fewer than 2 exercises.+ """+ orphaned = Variation.objects.annotate(+ exercise_count=Count('exercise')+ ).filter(exercise_count__lte=1)++ for variation in orphaned:+ self.stdout.write(+ self.style.WARNING(+ f'Variation {variation.uuid} has {variation.exercise_count} exercise(s)!'+ )+ )+ if delete:+ Exercise.objects.filter(variations=variation).update(variations=None)+ variation.delete()+ self.stdout.write(' -> deleted')diff --git a/wger/exercises/migrations/0007_auto_20201203_1042.py b/wger/exercises/migrations/0007_auto_20201203_1042.pyindex f02b6833d..b1776b4ba 100644--- a/wger/exercises/migrations/0007_auto_20201203_1042.py+++ b/wger/exercises/migrations/0007_auto_20201203_1042.py@@ -110,17 +110,14 @@ def insert_variations(apps, schema_editor):Variation = apps.get_model('exercises', 'Variation')for group in exercise_variation_ids:+ exercises = Exercise.objects.filter(pk__in=group)+ if exercises.count() < 2:+ continue+variation = Variation()variation.save()- for exercise_id in group:- # Exercises won't be found on an empty (new) database, just ignore- try:- exercise = Exercise.objects.get(pk=exercise_id)- exercise.variations = variation- exercise.save()- except Exercise.DoesNotExist:- pass+ exercises.update(variations=variation)def remove_variations(apps, schema_editor):diff --git a/wger/exercises/tests/test_exercise.py b/wger/exercises/tests/test_exercise.pyindex cf566b237..99208cf7a 100644--- a/wger/exercises/tests/test_exercise.py+++ b/wger/exercises/tests/test_exercise.py@@ -105,7 +105,7 @@ class ExerciseCustomApiTestCase(ExerciseCrudApiTestCase):'muscles': [1, 3],'muscles_secondary': [2],'equipment': [3],- 'variations': 4,+ 'variations': 1,}def get_resource_name(self):diff --git a/wger/exercises/tests/test_management_commands.py b/wger/exercises/tests/test_management_commands.pyindex 63f7b7275..86ad51972 100644--- a/wger/exercises/tests/test_management_commands.py+++ b/wger/exercises/tests/test_management_commands.py@@ -28,6 +28,7 @@ from wger.core.tests.base_testcase import WgerTestCasefrom wger.exercises.models import (Exercise,Translation,+ Variation,)@@ -119,3 +120,39 @@ class TestHealthCheckManagementCommands(WgerTestCase):call_command('exercises-health-check', '--delete-no-english', stdout=self.out)self.assertIn('-> deleted', self.out.getvalue())self.assertRaises(Exercise.DoesNotExist, Exercise.objects.get, pk=1)++ def test_find_orphan_variation_empty(self):+ """Variation with 0 exercises is reported"""++ orphan = Variation.objects.create()++ call_command('exercises-health-check', stdout=self.out)+ self.assertIn(f'Variation {orphan.uuid} has 0 exercise(s)!', self.out.getvalue())+ self.assertNotIn('-> deleted', self.out.getvalue())++ def test_find_orphan_variation_single(self):+ """Variation with 1 exercise is reported"""++ variation = Variation.objects.get(pk=1)+ # Remove one exercise so only 1 remains+ exercise = Exercise.objects.filter(variations=variation).first()+ Exercise.objects.filter(pk=exercise.pk).update(variations=None)++ call_command('exercises-health-check', stdout=self.out)+ self.assertIn(f'Variation {variation.uuid} has 1 exercise(s)!', self.out.getvalue())++ def test_fix_orphan_variations(self):+ """--delete-orphan-variations removes orphaned variations"""++ orphan = Variation.objects.create()+ orphan_pk = orphan.pk++ call_command('exercises-health-check', '--delete-orphan-variations', stdout=self.out)+ self.assertIn('-> deleted', self.out.getvalue())+ self.assertFalse(Variation.objects.filter(pk=orphan_pk).exists())++ def test_healthy_variations_not_affected(self):+ """Variations with 2+ exercises are not reported"""++ call_command('exercises-health-check', stdout=self.out)+ self.assertNotIn('Variation', self.out.getvalue())