Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Properly handle locally known deletion logs

Roland Geiderd68579306d4c
diff --git a/wger/exercises/models/base.py b/wger/exercises/models/base.py
index 3df7ede24..7ea4d3cc4 100644
--- a/wger/exercises/models/base.py
+++ b/wger/exercises/models/base.py
@@ -264,13 +264,14 @@ class Exercise(AbstractLicenseModel, AbstractHistoryMixin, models.Model):
except Exercise.DoesNotExist:
replace_by = None
- log = DeletionLog(
- model_type=DeletionLog.MODEL_EXERCISE,
+ DeletionLog.objects.update_or_create(
uuid=self.uuid,
- comment=f'Exercise base of {self.get_translation(ENGLISH_SHORT_NAME)}',
- replaced_by=replace_by,
+ defaults={
+ 'model_type': DeletionLog.MODEL_EXERCISE,
+ 'comment': f'Exercise base of {self.get_translation(ENGLISH_SHORT_NAME)}',
+ 'replaced_by': replace_by,
+ },
)
- log.save()
# Replace references in workout logs and routines before deleting,
# so that user data is not lost on this instance
diff --git a/wger/exercises/signals.py b/wger/exercises/signals.py
index 9ffaaae94..3275063dc 100644
--- a/wger/exercises/signals.py
+++ b/wger/exercises/signals.py
@@ -103,24 +103,26 @@ def delete_exercise_video_on_update(sender, instance: ExerciseVideo, **kwargs):
@receiver(pre_delete, sender=Translation)
def add_deletion_log_translation(sender, instance: Translation, **kwargs):
- DeletionLog(
- model_type=DeletionLog.MODEL_TRANSLATION,
+ DeletionLog.objects.update_or_create(
uuid=instance.uuid,
- comment=instance.name,
- ).save()
+ defaults={
+ 'model_type': DeletionLog.MODEL_TRANSLATION,
+ 'comment': instance.name,
+ },
+ )
@receiver(pre_delete, sender=ExerciseImage)
def add_deletion_log_image(sender, instance: ExerciseImage, **kwargs):
- DeletionLog(
- model_type=DeletionLog.MODEL_IMAGE,
+ DeletionLog.objects.update_or_create(
uuid=instance.uuid,
- ).save()
+ defaults={'model_type': DeletionLog.MODEL_IMAGE},
+ )
@receiver(pre_delete, sender=ExerciseVideo)
def add_deletion_log_video(sender, instance: ExerciseVideo, **kwargs):
- DeletionLog(
- model_type=DeletionLog.MODEL_VIDEO,
+ DeletionLog.objects.update_or_create(
uuid=instance.uuid,
- ).save()
+ defaults={'model_type': DeletionLog.MODEL_VIDEO},
+ )
diff --git a/wger/exercises/tests/test_deletion_log.py b/wger/exercises/tests/test_deletion_log.py
index 55bb28609..f17080df9 100644
--- a/wger/exercises/tests/test_deletion_log.py
+++ b/wger/exercises/tests/test_deletion_log.py
@@ -158,3 +158,40 @@ class DeletionLogTestCase(WgerTestCase):
uuid=translation.uuid,
).count()
self.assertEqual(count, 1)
+
+ def test_exercise_delete_with_existing_deletion_log(self):
+ """
+ Test that re-deleting an exercise whose UUID already has a deletion log
+ entry (e.g. it was deleted before, then re-imported via sync) updates
+ the existing entry instead of raising an IntegrityError.
+ """
+ exercise = Exercise.objects.get(pk=1)
+ DeletionLog.objects.create(
+ model_type=DeletionLog.MODEL_EXERCISE,
+ uuid=exercise.uuid,
+ comment='stale entry from a previous deletion',
+ )
+
+ exercise.delete()
+
+ log = DeletionLog.objects.get(uuid=exercise.uuid)
+ self.assertEqual(log.model_type, DeletionLog.MODEL_EXERCISE)
+ self.assertEqual(log.comment, 'Exercise base of An exercise')
+
+ def test_translation_delete_with_existing_deletion_log(self):
+ """
+ Test that re-deleting a translation whose UUID already has a deletion log
+ entry updates the existing entry instead of raising an IntegrityError.
+ """
+ translation = Translation.objects.get(pk=1)
+ DeletionLog.objects.create(
+ model_type=DeletionLog.MODEL_TRANSLATION,
+ uuid=translation.uuid,
+ comment='stale entry',
+ )
+
+ translation.delete()
+
+ log = DeletionLog.objects.get(uuid=translation.uuid)
+ self.assertEqual(log.model_type, DeletionLog.MODEL_TRANSLATION)
+ self.assertEqual(log.comment, translation.name)