Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Allow setting a replacement object when deleting exercise bases

Roland Geider51996b0edf31
diff --git a/wger/exercises/fixtures/test-exercises.json b/wger/exercises/fixtures/test-exercises.json
index 798edc8ab..5146c16e9 100644
--- a/wger/exercises/fixtures/test-exercises.json
+++ b/wger/exercises/fixtures/test-exercises.json
@@ -140,7 +140,7 @@
"pk": 1,
"model": "exercises.exercise",
"fields": {
- "uuid": "9838235ce38f4ca6921e9d237d8e0813",
+ "uuid": "9838235c-e38f-4ca6-921e-9d237d8e0813",
"language": 2,
"exercise_base": 1,
"description": "Lorem ipsum dolor sit amet",
diff --git a/wger/exercises/models/base.py b/wger/exercises/models/base.py
index d55b7b41b..a4cb936a6 100644
--- a/wger/exercises/models/base.py
+++ b/wger/exercises/models/base.py
@@ -234,3 +234,19 @@ class ExerciseBase(AbstractLicenseModel, AbstractHistoryMixin, models.Model):
exercise = self.exercises.filter(language__short_name=language).first()
return exercise
+
+ def delete(self, using=None, keep_parents=False, replace_by: str = None):
+ """
+ Save entry to log
+ """
+ # wger
+ from wger.exercises.models import DeletionLog
+ log = DeletionLog(
+ model_type=DeletionLog.MODEL_BASE,
+ uuid=self.uuid,
+ comment=f"Exercise base of {self.get_exercise(ENGLISH_SHORT_NAME).name}",
+ replaced_by=replace_by,
+ )
+ log.save()
+
+ return super().delete(using, keep_parents)
diff --git a/wger/exercises/signals.py b/wger/exercises/signals.py
index b0eaaecf9..63d175460 100644
--- a/wger/exercises/signals.py
+++ b/wger/exercises/signals.py
@@ -108,19 +108,18 @@ def delete_exercise_video_on_update(sender, instance: ExerciseVideo, **kwargs):
path.unlink()
-@receiver(pre_delete, sender=ExerciseBase)
-def add_deletion_log_base(sender, instance: ExerciseBase, **kwargs):
- log = DeletionLog(
- model_type=DeletionLog.MODEL_BASE,
- uuid=instance.uuid,
- )
- log.save()
+# Deletion log for exercise bases is handled in the model
+# @receiver(pre_delete, sender=ExerciseBase)
+# def add_deletion_log_base(sender, instance: ExerciseBase, **kwargs):
+# pass
@receiver(pre_delete, sender=Exercise)
def add_deletion_log_translation(sender, instance: Exercise, **kwargs):
log = DeletionLog(
- model_type=DeletionLog.MODEL_TRANSLATION, uuid=instance.uuid, comment=instance.name
+ model_type=DeletionLog.MODEL_TRANSLATION,
+ uuid=instance.uuid,
+ comment=instance.name,
)
log.save()
diff --git a/wger/exercises/tests/test_deletion_log.py b/wger/exercises/tests/test_deletion_log.py
index d5c0de7ce..c4639eed0 100644
--- a/wger/exercises/tests/test_deletion_log.py
+++ b/wger/exercises/tests/test_deletion_log.py
@@ -12,6 +12,9 @@
#
# You should have received a copy of the GNU Affero General Public License
+# Standard Library
+from uuid import UUID
+
# wger
from wger.core.tests.base_testcase import WgerTestCase
from wger.exercises.models import (
@@ -36,14 +39,52 @@ class DeletionLogTestCase(WgerTestCase):
base.delete()
# Base is deleted
- count_base = DeletionLog.objects.filter(model_type=DeletionLog.MODEL_BASE,
- uuid=base.uuid).count()
- self.assertEqual(count_base, 1)
+ count_base_logs = DeletionLog.objects.filter(
+ model_type=DeletionLog.MODEL_BASE, uuid=base.uuid
+ ).count()
+ log = DeletionLog.objects.get(pk=1)
+
+ self.assertEqual(count_base_logs, 1)
+ self.assertEqual(log.model_type, 'base')
+ self.assertEqual(log.uuid, base.uuid)
+ self.assertEqual(log.comment, 'Exercise base of An exercise')
+ self.assertEqual(log.replaced_by, None)
# All translations are also deleted
count = DeletionLog.objects.filter(model_type=DeletionLog.MODEL_TRANSLATION).count()
self.assertEqual(count, 2)
+ # First translation
+ log2 = DeletionLog.objects.get(pk=4)
+ self.assertEqual(log2.model_type, 'translation')
+ self.assertEqual(log2.uuid, UUID('9838235c-e38f-4ca6-921e-9d237d8e0813'))
+ self.assertEqual(log2.comment, 'An exercise')
+ self.assertEqual(log2.replaced_by, None)
+
+ # Second translation
+ log3 = DeletionLog.objects.get(pk=5)
+ self.assertEqual(log3.model_type, 'translation')
+ self.assertEqual(log3.uuid, UUID('13b532f9-d208-462e-a000-7b9982b2b53e'))
+ self.assertEqual(log3.comment, 'Test exercise 123')
+ self.assertEqual(log3.replaced_by, None)
+
+ def test_base_with_replaced_by(self):
+ """
+ Test that an entry is generated when a base is deleted and the replaced by is
+ set correctly
+ """
+ self.assertEqual(DeletionLog.objects.all().count(), 0)
+
+ base = ExerciseBase.objects.get(pk=1)
+ base.delete(replace_by="ae3328ba-9a35-4731-bc23-5da50720c5aa")
+
+ # Base is deleted
+ log = DeletionLog.objects.get(pk=1)
+
+ self.assertEqual(log.model_type, 'base')
+ self.assertEqual(log.uuid, base.uuid)
+ self.assertEqual(log.replaced_by, UUID('ae3328ba-9a35-4731-bc23-5da50720c5aa'))
+
def test_translation(self):
"""
Test that an entry is generated when a translation is deleted