Self hosted FLOSS fitness/workout, nutrition and weight tracker
Make routine deletions more robust
Roland Geider3e065e201740
diff --git a/wger/manager/signals.py b/wger/manager/signals.pyindex 068dd7593..51ee82428 100644--- a/wger/manager/signals.py+++ b/wger/manager/signals.py@@ -12,8 +12,12 @@## You should have received a copy of the GNU Affero General Public License+# Standard Library+from functools import wraps+# Djangofrom django.core.cache import cache+from django.core.exceptions import ObjectDoesNotExistfrom django.db.models.signals import (post_save,pre_delete,@@ -44,6 +48,26 @@ from wger.manager.models import (from wger.utils.cache import CacheKeyMapper+def ignore_missing_relations(handler):+ """+ Skips the handler if a related object no longer exists in the database.++ A concurrent request may have already deleted the rows the handler traverses,+ e.g. two simultaneous DELETEs of the same routine: the second one still holds+ the collected child instances and sends pre_delete signals for them, but the+ fresh FK lookups find nothing. In that case there is no cache left to reset.+ """++ @wraps(handler)+ def wrapper(sender, instance, **kwargs):+ try:+ handler(sender, instance, **kwargs)+ except ObjectDoesNotExist:+ pass++ return wrapper++def update_activity_cache(sender, instance, **kwargs):"""Update the user's cached last activity date@@ -58,22 +82,27 @@ def update_cache_routine(sender, instance: Routine, **kwargs):reset_routine_cache(instance)+@ignore_missing_relationsdef update_cache_day(sender, instance: Day, **kwargs):reset_routine_cache(instance.routine)+@ignore_missing_relationsdef update_cache_slot(sender, instance: Slot, **kwargs):reset_routine_cache(instance.day.routine)+@ignore_missing_relationsdef update_cache_slot_entry(sender, instance: SlotEntry, **kwargs):reset_routine_cache(instance.slot.day.routine)+@ignore_missing_relationsdef handle_config_change(sender, instance: AbstractChangeConfig, **kwargs):reset_routine_cache(instance.slot_entry.slot.day.routine)+@ignore_missing_relationsdef handle_workout_log_change(sender, instance: WorkoutLog, **kwargs):update_activity_cache(sender, instance, **kwargs)if instance.routine:@@ -81,6 +110,7 @@ def handle_workout_log_change(sender, instance: WorkoutLog, **kwargs):reset_routine_cache(instance.routine, structure=False)+@ignore_missing_relationsdef handle_workout_session_change(sender, instance: WorkoutSession, **kwargs):update_activity_cache(sender, instance, **kwargs)if instance.routine:diff --git a/wger/manager/tests/test_signals.py b/wger/manager/tests/test_signals.pynew file mode 100644index 000000000..3ab1788d7--- /dev/null+++ b/wger/manager/tests/test_signals.py@@ -0,0 +1,99 @@+# 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.manager.models import (+ Day,+ Routine,+ Slot,+ SlotEntry,+ WeightConfig,+ WorkoutLog,+ WorkoutSession,+)+++class StaleRelationSignalsTestCase(WgerTestCase):+ """+ Test that deleting objects whose related rows are already gone does not error++ The stale instances mirror what the deletion collector of a second, concurrent+ request holds after the first request's cascade delete has been committed.+ """++ def test_delete_day_with_deleted_routine(self):+ """A day whose routine is already deleted can be deleted without error"""++ day = Slot.objects.get(pk=1).day+ stale_day = Day.objects.get(pk=day.pk)++ day.routine.delete()++ stale_day.delete()++ def test_delete_slot_with_deleted_day(self):+ """A slot whose day is already deleted can be deleted without error"""++ slot = Slot.objects.get(pk=1)+ stale_slot = Slot.objects.get(pk=1)++ slot.day.delete()++ stale_slot.delete()++ def test_delete_slot_entry_with_deleted_slot(self):+ """A slot entry whose slot is already deleted can be deleted without error"""++ entry = SlotEntry(slot_id=1, exercise_id=1)+ entry.save()+ stale_entry = SlotEntry.objects.get(pk=entry.pk)++ Slot.objects.get(pk=1).delete()++ stale_entry.delete()++ def test_delete_config_with_deleted_slot_entry(self):+ """A config whose slot entry is already deleted can be deleted without error"""++ entry = SlotEntry(slot_id=1, exercise_id=1)+ entry.save()+ WeightConfig(slot_entry=entry, iteration=1, value=80).save()+ stale_config = WeightConfig.objects.get(slot_entry_id=entry.pk)++ entry.delete()++ stale_config.delete()++ def test_delete_workout_log_with_deleted_routine(self):+ """A workout log whose routine is already deleted can be deleted without error"""++ log = WorkoutLog(user_id=1, exercise_id=1, routine_id=1, weight=80, repetitions=5)+ log.save()+ stale_log = WorkoutLog.objects.get(pk=log.pk)++ Routine.objects.get(pk=1).delete()++ stale_log.delete()++ def test_delete_workout_session_with_deleted_routine(self):+ """A workout session whose routine is already deleted can be deleted without error"""++ session = WorkoutSession(user_id=1, routine_id=1)+ session.save()+ stale_session = WorkoutSession.objects.get(pk=session.pk)++ Routine.objects.get(pk=1).delete()++ stale_session.delete()