Self hosted FLOSS fitness/workout, nutrition and weight tracker
Allow passing the replaced_by UUID over the API
Roland Geiderd8ef9f9134cc
diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.pyindex 724281dd5..3cdff5f12 100644--- a/wger/exercises/api/views.py+++ b/wger/exercises/api/views.py@@ -17,6 +17,7 @@# Standard Libraryimport logging+from uuid import UUID# Djangofrom django.conf import settings@@ -133,6 +134,17 @@ class ExerciseBaseViewSet(ModelViewSet):action_object=serializer.instance,)+ def perform_destroy(self, instance: ExerciseBase):+ """Manually delete the exercise and set the replacement, if any"""++ uuid = self.request.query_params.get('replaced_by', '')+ try:+ UUID(uuid, version=4)+ except ValueError:+ uuid = None++ instance.delete(replace_by=uuid)+class ExerciseTranslationViewSet(ModelViewSet):"""diff --git a/wger/exercises/models/base.py b/wger/exercises/models/base.pyindex a4cb936a6..47bbadee4 100644--- a/wger/exercises/models/base.py+++ b/wger/exercises/models/base.py@@ -241,6 +241,13 @@ class ExerciseBase(AbstractLicenseModel, AbstractHistoryMixin, models.Model):"""# wgerfrom wger.exercises.models import DeletionLog++ if replace_by:+ try:+ ExerciseBase.objects.get(uuid=replace_by)+ except ExerciseBase.DoesNotExist:+ replace_by = None+log = DeletionLog(model_type=DeletionLog.MODEL_BASE,uuid=self.uuid,diff --git a/wger/exercises/tests/test_deletion_log.py b/wger/exercises/tests/test_deletion_log.pyindex c4639eed0..0dd555ede 100644--- a/wger/exercises/tests/test_deletion_log.py+++ b/wger/exercises/tests/test_deletion_log.py@@ -85,6 +85,22 @@ class DeletionLogTestCase(WgerTestCase):self.assertEqual(log.uuid, base.uuid)self.assertEqual(log.replaced_by, UUID('ae3328ba-9a35-4731-bc23-5da50720c5aa'))+ def test_base_with_nonexistent_replaced_by(self):+ """+ Test that an entry is generated when a base is deleted and the replaced by is+ set correctly. If the UUID is not found in the DB, it's set to None+ """+ self.assertEqual(DeletionLog.objects.all().count(), 0)++ base = ExerciseBase.objects.get(pk=1)+ base.delete(replace_by="12345678-1234-1234-1234-1234567890ab")++ # Base is deleted+ log = DeletionLog.objects.get(pk=1)++ self.assertEqual(log.model_type, 'base')+ self.assertEqual(log.replaced_by, None)+def test_translation(self):"""Test that an entry is generated when a translation is deleteddiff --git a/wger/exercises/tests/test_exercise_base.py b/wger/exercises/tests/test_exercise_base.pyindex a0bb8eb9b..ab42a7507 100644--- a/wger/exercises/tests/test_exercise_base.py+++ b/wger/exercises/tests/test_exercise_base.py@@ -12,6 +12,9 @@## You should have received a copy of the GNU Affero General Public License+# Standard Library+from uuid import UUID+# Third Partyfrom rest_framework import status@@ -19,6 +22,7 @@ from rest_framework import statusfrom wger.core.tests.api_base_test import ExerciseCrudApiTestCasefrom wger.core.tests.base_testcase import WgerTestCasefrom wger.exercises.models import (+ DeletionLog,Exercise,ExerciseBase,)@@ -105,6 +109,21 @@ class ExerciseCustomApiTestCase(ExerciseCrudApiTestCase):def get_resource_name(self):return 'exercise-base'+ def test_delete_replace_by(self):+ """Test that setting the replaced_by attribute works"""++ self.authenticate('admin')++ url = self.url_detail + '?replaced_by=ae3328ba-9a35-4731-bc23-5da50720c5aa'+ response = self.client.delete(url)+ self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)++ log = DeletionLog.objects.get(pk=1)++ self.assertEqual(log.model_type, 'base')+ self.assertEqual(log.uuid, UUID('acad3949-36fb-4481-9a72-be2ddae2bc05'))+ self.assertEqual(log.replaced_by, UUID('ae3328ba-9a35-4731-bc23-5da50720c5aa'))+def test_cant_change_license(self):"""Test that it is not possible to change the license of an existing