Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Don't allow an exercise to be replaced with itself when it's deleted

Roland Geider5a6a7da4c783
diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.py
index 4baf07a91..56eeda3e0 100644
--- a/wger/exercises/api/views.py
+++ b/wger/exercises/api/views.py
@@ -118,6 +118,10 @@ class ExerciseViewSet(ModelViewSet):
except ValueError:
uuid = None
+ # An exercise can't be replaced by itself, treat it as a plain deletion
+ if uuid == str(instance.uuid):
+ uuid = None
+
transfer_media = 'transfer_media' in self.request.query_params
transfer_translations = 'transfer_translations' in self.request.query_params
diff --git a/wger/exercises/models/base.py b/wger/exercises/models/base.py
index aa6aec235..03f21773e 100644
--- a/wger/exercises/models/base.py
+++ b/wger/exercises/models/base.py
@@ -281,10 +281,14 @@ class Exercise(AbstractLicenseModel, AbstractHistoryMixin, models.Model):
replacement = None
if replace_by:
- try:
- replacement = Exercise.objects.get(uuid=replace_by)
- except Exercise.DoesNotExist:
+ # An exercise can't be replaced by itself
+ if str(replace_by) == str(self.uuid):
replace_by = None
+ else:
+ try:
+ replacement = Exercise.objects.get(uuid=replace_by)
+ except Exercise.DoesNotExist:
+ replace_by = None
# Wrap everything in a single transaction so the deletion-log entry,
# the reference repointing and the delete itself are all-or-nothing.
diff --git a/wger/exercises/tests/test_deletion_log.py b/wger/exercises/tests/test_deletion_log.py
index 21ae46538..6a541f390 100644
--- a/wger/exercises/tests/test_deletion_log.py
+++ b/wger/exercises/tests/test_deletion_log.py
@@ -285,6 +285,24 @@ class DeletionLogTestCase(WgerTestCase):
)
self.assertEqual(log.replaced_by, None)
+ def test_exercise_replace_by_self_is_ignored(self):
+ """
+ Test that deleting an exercise that points to its own UUID as the
+ replacement is treated as a plain deletion: the deletion log entry must
+ not reference itself, otherwise the exercise would be served in both the
+ active feed and the deletion log and trigger an endless sync loop.
+ """
+ exercise = Exercise.objects.get(pk=1)
+
+ exercise.delete(replace_by=str(exercise.uuid))
+
+ log = DeletionLog.objects.get(
+ model_type=DeletionLog.MODEL_EXERCISE,
+ uuid=exercise.uuid,
+ )
+ self.assertIsNone(log.replaced_by)
+ self.assertFalse(Exercise.objects.filter(pk=1).exists())
+
def test_translation(self):
"""
Test that an entry is generated when a translation is deleted
diff --git a/wger/exercises/tests/test_exercise.py b/wger/exercises/tests/test_exercise.py
index 78a5ff4a6..f84d51ae6 100644
--- a/wger/exercises/tests/test_exercise.py
+++ b/wger/exercises/tests/test_exercise.py
@@ -128,6 +128,21 @@ class ExerciseCustomApiTestCase(ActstreamApiMixin, ExerciseCrudApiTestCase):
)
self.assertEqual(log.replaced_by, UUID('ae3328ba-9a35-4731-bc23-5da50720c5aa'))
+ def test_delete_replace_by_self_is_ignored(self):
+ """Test that an exercise can't be replaced by itself on deletion"""
+
+ self.authenticate('admin')
+
+ url = self.url_detail + '?replaced_by=acad3949-36fb-4481-9a72-be2ddae2bc05'
+ response = self.client.delete(url)
+ self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
+
+ log = DeletionLog.objects.get(
+ model_type=DeletionLog.MODEL_EXERCISE,
+ uuid=UUID('acad3949-36fb-4481-9a72-be2ddae2bc05'),
+ )
+ self.assertIsNone(log.replaced_by)
+
def test_cant_change_license(self):
"""
Test that it is not possible to change the license of an existing