Self hosted FLOSS fitness/workout, nutrition and weight tracker
Fix bug that wasn't properly replacing exercises
Roland Geider1c8335cb1fee
diff --git a/wger/exercises/models/base.py b/wger/exercises/models/base.pyindex 6f5f8dc66..4f836de8c 100644--- a/wger/exercises/models/base.py+++ b/wger/exercises/models/base.py@@ -251,14 +251,18 @@ class Exercise(AbstractLicenseModel, AbstractHistoryMixin, models.Model):def delete(self, using=None, keep_parents=False, replace_by: str = None):"""- Save entry to log+ Save entry to deletion log and optionally replace references in+ workout logs and routines before deleting."""# wgerfrom wger.exercises.models import DeletionLog+ from wger.manager.models import WorkoutLog+ from wger.manager.models.slot_entry import SlotEntry+ replacement = Noneif replace_by:try:- Exercise.objects.get(uuid=replace_by)+ replacement = Exercise.objects.get(uuid=replace_by)except Exercise.DoesNotExist:replace_by = None@@ -270,6 +274,12 @@ class Exercise(AbstractLicenseModel, AbstractHistoryMixin, models.Model):)log.save()+ # Replace references in workout logs and routines before deleting,+ # so that user data is not lost on this instance+ if replacement:+ SlotEntry.objects.filter(exercise=self).update(exercise=replacement)+ WorkoutLog.objects.filter(exercise=self).update(exercise=replacement)+reset_exercise_api_cache(self.uuid)return super().delete(using, keep_parents)diff --git a/wger/exercises/tests/test_deletion_log.py b/wger/exercises/tests/test_deletion_log.pyindex 6d7ac97fc..76ef30c8b 100644--- a/wger/exercises/tests/test_deletion_log.py+++ b/wger/exercises/tests/test_deletion_log.py@@ -22,6 +22,8 @@ from wger.exercises.models import (Exercise,Translation,)+from wger.manager.models import WorkoutLog+from wger.manager.models.slot_entry import SlotEntryclass DeletionLogTestCase(WgerTestCase):@@ -86,6 +88,53 @@ class DeletionLogTestCase(WgerTestCase):self.assertEqual(log.uuid, exercise.uuid)self.assertEqual(log.replaced_by, UUID('ae3328ba-9a35-4731-bc23-5da50720c5aa'))+ def test_exercise_replace_by_updates_workout_logs(self):+ """+ Test that workout logs referencing the deleted exercise are updated+ to point to the replacement exercise+ """+ exercise_to_delete = Exercise.objects.get(pk=1)+ replacement = Exercise.objects.get(pk=2)++ # Exercise 1 has 4 workout logs, exercise 2 has 1+ self.assertEqual(WorkoutLog.objects.filter(exercise=exercise_to_delete).count(), 4)+ self.assertEqual(WorkoutLog.objects.filter(exercise=replacement).count(), 1)++ exercise_to_delete.delete(replace_by=str(replacement.uuid))++ # All workout logs should now point to the replacement+ self.assertEqual(WorkoutLog.objects.filter(exercise=replacement).count(), 5)++ def test_exercise_replace_by_updates_slot_entries(self):+ """+ Test that routine slot entries referencing the deleted exercise are+ updated to point to the replacement exercise+ """+ exercise_to_delete = Exercise.objects.get(pk=1)+ replacement = Exercise.objects.get(pk=2)++ # Exercise 1 has 1 slot entry, exercise 2 has 1+ self.assertEqual(SlotEntry.objects.filter(exercise=exercise_to_delete).count(), 1)+ self.assertEqual(SlotEntry.objects.filter(exercise=replacement).count(), 1)++ exercise_to_delete.delete(replace_by=str(replacement.uuid))++ # The slot entry should now point to the replacement+ self.assertEqual(SlotEntry.objects.filter(exercise=replacement).count(), 2)++ def test_exercise_delete_without_replace_by_does_not_keep_references(self):+ """+ Test that deleting without replace_by does not attempt to update+ references (they will be cascade-deleted)+ """+ exercise_to_delete = Exercise.objects.get(pk=1)+ self.assertEqual(WorkoutLog.objects.filter(exercise=exercise_to_delete).count(), 4)++ exercise_to_delete.delete()++ # Workout logs for this exercise should be gone (cascade)+ self.assertEqual(WorkoutLog.objects.filter(exercise_id=1).count(), 0)+def test_exercise_with_nonexistent_replaced_by(self):"""Test that an entry is generated when an exercise is deleted and the replaced by is