From ce50e9fd8a0885a9e7805ab9e4365382b83a5c08 Mon Sep 17 00:00:00 2001 From: David Fischer Date: Wed, 25 Mar 2026 22:46:00 +0100 Subject: [PATCH] Fix FileNotFoundError when deleting avatar with absent resized/ directory --- avatar/models.py | 5 ++++- tests/tests.py | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/avatar/models.py b/avatar/models.py index 8c5d38f..5525ce9 100644 --- a/avatar/models.py +++ b/avatar/models.py @@ -210,7 +210,10 @@ def remove_avatar_images(instance=None, delete_main_avatar=True, **kwargs): path, filename = os.path.split(base_filepath) # iterate through resized avatars directories and delete resized avatars resized_path = os.path.join(path, "resized") - resized_widths, _ = instance.avatar.storage.listdir(resized_path) + try: + resized_widths, _ = instance.avatar.storage.listdir(resized_path) + except FileNotFoundError: + resized_widths = [] for width in resized_widths: resized_width_path = os.path.join(resized_path, width) resized_heights, _ = instance.avatar.storage.listdir(resized_width_path) diff --git a/tests/tests.py b/tests/tests.py index 36579a3..086973e 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -217,6 +217,19 @@ class AvatarTests(TestCase): self.assertEqual(receiver.sender, Avatar) self.assertEqual(receiver.signal_sent_count, 1) + def test_delete_avatar_without_resized_directory(self): + self.test_normal_image_upload() + avatar = Avatar.objects.get(user=self.user) + resized_path = os.path.join( + self.testmediapath, + os.path.dirname(avatar.avatar.name), + "resized", + ) + if os.path.exists(resized_path): + rmtree(resized_path) + avatar.delete() + self.assertEqual(Avatar.objects.filter(user=self.user).count(), 0) + def test_delete_primary_avatar_and_new_primary(self): self.test_there_can_be_only_one_primary_avatar() primary = get_primary_avatar(self.user)