Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Fix exercise image upload bug

Roland Geider1bf18060d076
diff --git a/wger/exercises/signals.py b/wger/exercises/signals.py
index 3275063dc..f2b3d8c17 100644
--- a/wger/exercises/signals.py
+++ b/wger/exercises/signals.py
@@ -61,11 +61,11 @@ def delete_exercise_image_on_update(sender, instance: ExerciseImage, **kwargs):
return False
new_file = instance.image
- if not old_file == new_file:
- # Deletes the image as well as the thumbnails
- thumbnailer = get_thumbnailer(instance.image)
+ if not old_file == new_file and old_file:
+ # Deletes the old image as well as its thumbnails
+ thumbnailer = get_thumbnailer(old_file)
thumbnailer.delete_thumbnails()
- instance.image.delete(save=False)
+ old_file.delete(save=False)
return None
diff --git a/wger/exercises/tests/test_exercise_images.py b/wger/exercises/tests/test_exercise_images.py
index e1fbe9544..8c885c50d 100644
--- a/wger/exercises/tests/test_exercise_images.py
+++ b/wger/exercises/tests/test_exercise_images.py
@@ -128,6 +128,27 @@ class MainImageTestCase(WgerTestCase):
self.assertTrue(image.is_ai_generated)
+ def test_replacing_image_keeps_new_file(self):
+ """
+ Replacing the file on an existing ExerciseImage must keep the new file
+ on the model. The pre_save signal cleans up the *old* file, not the new
+ one being saved (regression test for the signal deleting instance.image).
+ """
+
+ translation = Translation.objects.get(pk=2)
+ pk = self.save_image(translation.exercise, 'protestschwein.jpg')
+
+ image = ExerciseImage.objects.get(pk=pk)
+ old_name = image.image.name
+ self.assertTrue(old_name)
+
+ with open('wger/exercises/tests/wildschwein.jpg', 'rb') as new_file:
+ image.image.save('wildschwein.jpg', File(new_file))
+
+ image.refresh_from_db()
+ self.assertTrue(image.image.name, 'image field was cleared after replacing the file')
+ self.assertNotEqual(image.image.name, old_name)
+
class ExerciseImageFromJsonSimpleTests(SimpleTestCase):
def test_from_json_sets_fields_when_generate_uuid_false(self):