mirror of
https://github.com/jazzband/django-avatar.git
synced 2026-05-09 16:14:45 +00:00
Fix FileNotFoundError when deleting avatar with absent resized/ directory
This commit is contained in:
parent
12e4745f29
commit
ce50e9fd8a
2 changed files with 17 additions and 1 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue