From 3eaa186b666e49e17fea2fc9adeff0d4bfe60642 Mon Sep 17 00:00:00 2001 From: Commander1024 Date: Fri, 25 Dec 2020 12:53:52 +0100 Subject: [PATCH] Delete profile_image on user_deletion. --- django_lostplaces/lostplaces/models/models.py | 15 +++++++++++++++ .../templates/explorer/profile_update.html | 2 +- django_lostplaces/lostplaces/views/place_views.py | 3 ++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/django_lostplaces/lostplaces/models/models.py b/django_lostplaces/lostplaces/models/models.py index cbf0a29..de31c44 100644 --- a/django_lostplaces/lostplaces/models/models.py +++ b/django_lostplaces/lostplaces/models/models.py @@ -68,9 +68,24 @@ class Explorer(models.Model): @receiver(post_save, sender=User) def create_user_profile(sender, instance, created, **kwargs): + """ + Delete Explorer profile when User gets deleted. + Deletion on profile_pic update is not necessary, as it gets + simply overwritten. + """ if created: Explorer.objects.create(user=instance) +def auto_delete_file_on_delete(sender, instance, **kwargs): + """ + Deletes file (including thumbnails) from filesystem + when corresponding `profile_image` object is deleted. + """ + if instance.profile_image: + # Get and delete all files and thumbnails from instance + thumbmanager = get_thumbnailer(instance.profile_image) + thumbmanager.delete(save=False) + @receiver(post_save, sender=User) def save_user_profile(sender, instance, **kwargs): instance.explorer.save() diff --git a/django_lostplaces/lostplaces/templates/explorer/profile_update.html b/django_lostplaces/lostplaces/templates/explorer/profile_update.html index 0da5ec8..6e8c491 100644 --- a/django_lostplaces/lostplaces/templates/explorer/profile_update.html +++ b/django_lostplaces/lostplaces/templates/explorer/profile_update.html @@ -7,7 +7,7 @@ {% block maincontent %} -
+
{% trans 'Edit Explorer profile' %} {% csrf_token %} diff --git a/django_lostplaces/lostplaces/views/place_views.py b/django_lostplaces/lostplaces/views/place_views.py index c96726b..c069dc0 100644 --- a/django_lostplaces/lostplaces/views/place_views.py +++ b/django_lostplaces/lostplaces/views/place_views.py @@ -150,4 +150,5 @@ class PlaceUnfavoriteView(IsAuthenticatedMixin, View): else: return redirect( reverse_lazy('place_detail', kwargs={'pk': place.pk}) - ) \ No newline at end of file + ) + \ No newline at end of file