-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
73 lines (56 loc) · 2.57 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import sha
import datetime
import pytz
from random import random
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound, HttpResponseBadRequest
from django.shortcuts import render_to_response, get_object_or_404
from handball.forms import SignUpForm
from handball.models import Person
from handball.api import PersonResource
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
def index(request):
return HttpResponse("This is the index page. There's nothing here yet!")
def sign_up(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
email = form.cleaned_data['email']
user = User.objects.create(username=username, password=password, email=email)
user_profile = user.get_profile()
# Build the activation key
salt = sha.new(str(random())).hexdigest()[:5]
activation_key = sha.new(salt + user.username).hexdigest()
key_expires = datetime.datetime.now(pytz.utc) + datetime.timedelta(2)
# User is unactive until visiting activation link
user.is_active = False
user_profile.activation_key = activation_key
user_profile.key_expires = key_expires
activation_link = 'http://127.0.0.1/activate/' + activation_key
user.save()
user_profile.save() # Is it necessary to explicitly call the profiles save method?
from django.core.mail import send_mail
subject = _('Welcome to ScoreIt!')
message = _('To activate, please click the following link:\n' + activation_link)
sender = _('noreply@score-it.de')
recipients = [email]
send_mail(subject, message, sender, recipients)
return HttpResponseRedirect('/thanks/')
else:
form = SignUpForm()
return render_to_response('signup.html', {
'form': form,
})
def activate(request, activation_key):
user_profile = get_object_or_404(Person, activation_key=activation_key)
user_account = user_profile.user
if user_profile.key_expires < datetime.datetime.now(pytz.utc):
user_account.delete()
return render_to_response('activate.html', {'expired': True})
user_account.is_active = True
user_account.save()
return render_to_response('activate.html', {'success': True})
def thanks(request):
return render_to_response('thanks.html')