Skip to content

Commit

Permalink
Delete avatars from file storage when avatar is deleted (#174)
Browse files Browse the repository at this point in the history
* added custom delete method to Avatar model inorder to delete avatars from file storage

* simplified chained expression to pass linting

* linting

* stopped using reserved keyword dir

* changed remove_avatar_images so it deletes all generated avatars

* went back to using queryset delete

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: Johannes Wilm <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 14, 2022
1 parent 1dd9933 commit 8017d6f
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions avatar/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,13 @@ def create_default_thumbnails(sender, instance, created=False, **kwargs):


def remove_avatar_images(instance=None, **kwargs):
if hasattr(instance, "user"):
for size in settings.AVATAR_AUTO_GENERATE_SIZES:
if instance.thumbnail_exists(size):
instance.avatar.storage.delete(instance.avatar_name(size))
base_filepath = instance.avatar.name
path, filename = os.path.split(base_filepath)
# iterate through resized avatars directories and delete resized avatars
resized_sizes, _ = instance.avatar.storage.listdir(os.path.join(path, "resized"))
for size in resized_sizes:
if instance.thumbnail_exists(size):
instance.avatar.storage.delete(instance.avatar_name(size))
if instance.avatar.storage.exists(instance.avatar.name):
instance.avatar.storage.delete(instance.avatar.name)

Expand Down

0 comments on commit 8017d6f

Please sign in to comment.