Fix FileNotFoundError when deleting avatar with absent resized/ directory

This commit is contained in:
David Fischer 2026-03-25 22:46:00 +01:00
parent 12e4745f29
commit ce50e9fd8a
2 changed files with 17 additions and 1 deletions

View file

@ -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)

View file

@ -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)