Skip to content

Commit

Permalink
(#92) - Associated images now delete along with a user
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy-Wu12 committed Sep 16, 2022
1 parent 105d21e commit 84be873
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions settings/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os.path

from django.shortcuts import render, reverse
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
Expand All @@ -6,7 +8,7 @@

from .forms import PasswordForm, EmailForm, DeleteForm
from access.models import CustomUser

from logs.models import Log

# Create your views here.
@login_required
Expand Down Expand Up @@ -67,11 +69,10 @@ def delete(request):
# Still need to validate correct password
user = CustomUser.objects.get(pk=request.user.id)
if user.check_password(form.cleaned_data['password']):
user.delete()
delete_account_data(user)
return HttpResponseRedirect(reverse('access:signup'))

context['error'] = 'Incorrect password!'

context['error'] = 'Incorrect password!'
context['form'] = DeleteForm()
return render(request, 'settings/delete.html', context)

Expand All @@ -82,3 +83,20 @@ def delete(request):
@login_required
def privacy(request):
pass


def delete_account_data(user: CustomUser):
logs = Log.objects.filter(creator=user.id)

# Delete all log images
for log in logs:
img_path = log.food.image.path
if os.path.exists(img_path):
os.remove(img_path)

# Delete profile picture
pro_pic_path = user.profile_picture.path
if os.path.exists(pro_pic_path):
os.remove(pro_pic_path)

user.delete()

0 comments on commit 84be873

Please sign in to comment.