From ce50e9fd8a0885a9e7805ab9e4365382b83a5c08 Mon Sep 17 00:00:00 2001 From: David Fischer Date: Wed, 25 Mar 2026 22:46:00 +0100 Subject: [PATCH 1/2] 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) From b187e21fb1c598574c8b1507c41e55863b652824 Mon Sep 17 00:00:00 2001 From: David Fischer Date: Sat, 28 Mar 2026 23:18:57 +0100 Subject: [PATCH 2/2] Fix duplicate ROOT_URLCONF setting and extra blank line in tests/settings.py --- tests/settings.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/settings.py b/tests/settings.py index f17ab14..ee03654 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -50,8 +50,6 @@ SITE_ID = 1 SECRET_KEY = "something-something" -ROOT_URLCONF = "tests.urls" - STATIC_URL = "/site_media/static/" AVATAR_ALLOWED_FILE_EXTS = (".jpg", ".png") @@ -59,5 +57,4 @@ AVATAR_MAX_SIZE = 1024 * 1024 AVATAR_MAX_AVATARS_PER_USER = 20 AVATAR_AUTO_GENERATE_SIZES = [51, 62, (33, 22), 80] - MEDIA_ROOT = os.path.join(SETTINGS_DIR, "../test-media")