Self hosted FLOSS fitness/workout, nutrition and weight tracker
Small improvement to deletion log handling
Roland Geiderf36704da9798
diff --git a/wger/exercises/models/base.py b/wger/exercises/models/base.pyindex 39dbc6abc..aa6aec235 100644--- a/wger/exercises/models/base.py+++ b/wger/exercises/models/base.py@@ -272,7 +272,11 @@ class Exercise(AbstractLicenseModel, AbstractHistoryMixin, models.Model):from wger.exercises.models.image import ExerciseImagefrom wger.exercises.models.translation import Translationfrom wger.exercises.models.video import ExerciseVideo- from wger.manager.models import WorkoutLog+ from wger.manager.helpers import reset_routine_cache+ from wger.manager.models import (+ Routine,+ WorkoutLog,+ )from wger.manager.models.slot_entry import SlotEntryreplacement = None@@ -299,9 +303,27 @@ class Exercise(AbstractLicenseModel, AbstractHistoryMixin, models.Model):# Replace references in workout logs and routines before deleting,# so that user data is not lost on this instanceif replacement:+ # Repointing the references with a bulk .update() bypasses the+ # signals that keep the routine caches fresh. Collect the+ # affected ones first, then reset their caches explicitly,+ # otherwise the cached routine structure keeps pointing at this+ # now-deleted exercise.+ routine_ids = set(+ SlotEntry.objects.filter(exercise=self).values_list(+ 'slot__day__routine_id', flat=True+ )+ )+ routine_ids.update(+ WorkoutLog.objects.filter(exercise=self).values_list('routine_id', flat=True)+ )+ routine_ids.discard(None)+SlotEntry.objects.filter(exercise=self).update(exercise=replacement)WorkoutLog.objects.filter(exercise=self).update(exercise=replacement)+ for routine in Routine.objects.filter(id__in=routine_ids):+ reset_routine_cache(routine)+if transfer_media:ExerciseImage.objects.filter(exercise=self).update(exercise=replacement)ExerciseVideo.objects.filter(exercise=self).update(exercise=replacement)diff --git a/wger/exercises/sync.py b/wger/exercises/sync.pyindex e8b205743..db004173a 100644--- a/wger/exercises/sync.py+++ b/wger/exercises/sync.py@@ -320,17 +320,17 @@ def handle_deleted_entries(try:old_exercise = Exercise.objects.get(uuid=uuid)- # Replace exercise in routines and logs+ # Count the references before they are repointed, so we can+ # report how many were moved to the replacementif obj_replaced:- nr_slot_entries = SlotEntry.objects.filter(exercise=old_exercise).update(- exercise=obj_replaced- )+ nr_slot_entries = SlotEntry.objects.filter(exercise=old_exercise).count()+ nr_logs = WorkoutLog.objects.filter(exercise=old_exercise).count()- nr_logs = WorkoutLog.objects.filter(exercise=old_exercise).update(- exercise=obj_replaced- )+ # Let the model repoint the references in routines and logs to+ # the replacement (and reset the affected routine caches) before+ # deleting, so that user data is not lost on this instance+ old_exercise.delete(replace_by=replaced_by_uuid if obj_replaced else None)- old_exercise.delete()replaced_by_info = f' (replaced by {obj_replaced.uuid})' if obj_replaced else ''print_fn(f'Deleted exercise {uuid}{replaced_by_info}')if nr_slot_entries:diff --git a/wger/exercises/tests/test_deletion_log.py b/wger/exercises/tests/test_deletion_log.pyindex 95bcbba2f..21ae46538 100644--- a/wger/exercises/tests/test_deletion_log.py+++ b/wger/exercises/tests/test_deletion_log.py@@ -16,6 +16,9 @@from unittest.mock import patchfrom uuid import UUID+# Django+from django.core.cache import cache+# wgerfrom wger.core.tests.base_testcase import WgerTestCasefrom wger.exercises.models import (@@ -27,6 +30,7 @@ from wger.exercises.models.image import ExerciseImagefrom wger.exercises.models.video import ExerciseVideofrom wger.manager.models import WorkoutLogfrom wger.manager.models.slot_entry import SlotEntry+from wger.utils.cache import CacheKeyMapperclass DeletionLogTestCase(WgerTestCase):@@ -116,6 +120,28 @@ class DeletionLogTestCase(WgerTestCase):# The slot entry should now point to the replacementself.assertEqual(SlotEntry.objects.filter(exercise=replacement).count(), 2)+ def test_exercise_replace_by_resets_routine_cache(self):+ """+ Test that replacing a deleted exercise invalidates the cache of every+ routine that referenced it.++ The references are repointed with a bulk update that does not fire the+ signals which normally refresh the routine caches. If the cache is not+ reset explicitly, the cached routine structure keeps pointing at the+ now-deleted exercise.+ """+ exercise_to_delete = Exercise.objects.get(pk=1)+ replacement = Exercise.objects.get(pk=2)++ routine = SlotEntry.objects.get(exercise=exercise_to_delete).slot.day.routine+ cache_key = CacheKeyMapper.routine_api_structure_key(routine.id, routine.user_id)+ cache.set(cache_key, {'stale': 'data'})+ self.assertIsNotNone(cache.get(cache_key))++ exercise_to_delete.delete(replace_by=str(replacement.uuid))++ self.assertIsNone(cache.get(cache_key))+def test_exercise_replace_by_delete_is_atomic_on_failure(self):"""Test that a failure during the delete rolls back the deletion log anddiff --git a/wger/exercises/tests/test_sync.py b/wger/exercises/tests/test_sync.pyindex bc3690830..5365e3ea9 100644--- a/wger/exercises/tests/test_sync.py+++ b/wger/exercises/tests/test_sync.py@@ -17,6 +17,7 @@ import iofrom unittest.mock import patch# Django+from django.core.cache import cachefrom django.core.exceptions import ValidationError# Third Party@@ -52,6 +53,7 @@ from wger.manager.models import (SlotEntry,WorkoutLog,)+from wger.utils.cache import CacheKeyMapperfrom wger.utils.requests import wger_headers@@ -822,6 +824,23 @@ class TestSyncMethods(WgerTestCase):for pk in logs:self.assertEqual(WorkoutLog.objects.get(pk=pk).exercise_id, 2)+ @patch('requests.get', return_value=MockDeletionLogResponse())+ def test_deletion_log_resets_routine_cache(self, mock_request):+ """+ Test that syncing a deletion that replaces an exercise invalidates the+ cache of every routine that referenced it. Otherwise the cached routine+ structure keeps pointing at the now-deleted exercise.+ """+ exercise1 = Exercise.objects.get(pk=1)+ routine = SlotEntry.objects.filter(exercise=exercise1).first().slot.day.routine+ cache_key = CacheKeyMapper.routine_api_structure_key(routine.id, routine.user_id)+ cache.set(cache_key, {'stale': 'data'})+ self.assertIsNotNone(cache.get(cache_key))++ handle_deleted_entries(print)++ self.assertIsNone(cache.get(cache_key))+@patch('requests.get', return_value=MockExerciseResponse())def test_exercise_sync(self, mock_request):self.assertEqual(Exercise.objects.count(), 8)