1
# Create your views here.
4
from django.urls import reverse
5
from django.contrib.auth.decorators import login_required
6
from django.contrib.auth.models import User
7
from django.shortcuts import render, redirect
8
from django.http import HttpResponseRedirect
9
from django.shortcuts import get_object_or_404
10
from django.conf import settings
12
from forms import EditProfileForm
16
def delete_me(request):
17
"""Show a page to inform the user what deleting means."""
22
return render(request, 'wlprofile/delete_me.html',
27
def do_delete(request):
28
"""Delete user specific data.
30
We can't really delete a user who has posted some valid posts (no spam) because otherwise
31
we have foreign keys constraints in the database. All we can do is to do some cleanup and
35
from django.contrib.auth import logout
36
from wlprofile.models import Profile
37
from notification.models import NoticeSetting
39
user = get_object_or_404(User, username=request.user)
41
# Log the user out. We do this as early as possible but after
42
# we get the User object.
45
# Clean possible Playtime availabilities
46
from wlscheduling.models import Availabilities
48
events = Availabilities.objects.filter(user=user)
54
# Clean the Online gaming password
55
from wlggz.models import GGZAuth
57
ggz_user = GGZAuth.objects.get(user=user)
63
profile = user.wlprofile
64
upload_to = Profile._meta.get_field('avatar').upload_to
66
if upload_to in profile.avatar.name:
67
# Delete the avatar file
68
profile.avatar.delete()
70
# Delete the profile and recreate it to get a clean profile page
71
# We create a new one to have the anymous.png as avatar
73
profile = Profile(user=user, deleted=True)
76
# Deactivate all subscriptions
77
notice_settings = NoticeSetting.objects.filter(user=user)
78
for setting in notice_settings:
82
# Put all PMs in the trash of the user. They stay as long in the trash until the sender or recipient
83
# has also put the message in the trash.
84
from django_messages.models import Message
85
from datetime import datetime
86
messages = Message.objects.inbox_for(user)
87
for message in messages:
88
message.recipient_deleted_at = datetime.now()
90
messages = Message.objects.outbox_for(user)
91
for message in messages:
92
message.sender_deleted_at = datetime.now()
95
# Do some settings in django.auth.User
96
user.is_active = False
98
user.is_superuser = False
99
user.email = settings.DELETED_MAIL_ADDRESS
102
return redirect('mainpage')
106
def view(request, user=None):
109
Note that login is required here to make sure that not all spam post
114
profile = request.user.wlprofile
116
profile = get_object_or_404(User, username=user).wlprofile
122
return render(request, 'wlprofile/view_profile.html',
128
instance = request.user.wlprofile
130
if request.method == 'POST':
131
form = EditProfileForm(request.POST,
132
instance=instance, files=request.FILES)
136
return HttpResponseRedirect(reverse(view))
138
form = EditProfileForm(instance=instance)
142
'profile_form': form,
144
return render(request, 'wlprofile/edit_profile.html',