gremmie@1
|
1 """views for the accounts application"""
|
gremmie@1
|
2
|
gremmie@1
|
3 import datetime
|
bgneal@74
|
4 import logging
|
bgneal@74
|
5
|
gremmie@1
|
6 from django.shortcuts import render_to_response
|
gremmie@1
|
7 from django.template import RequestContext
|
gremmie@1
|
8 from django.contrib import auth
|
gremmie@1
|
9 from django.http import HttpResponseRedirect
|
gremmie@1
|
10 from django.core.urlresolvers import reverse
|
bgneal@6
|
11 from django.conf import settings
|
gremmie@1
|
12
|
gremmie@1
|
13 from accounts.models import PendingUser
|
gremmie@1
|
14 from accounts.forms import RegisterForm
|
gremmie@1
|
15
|
gremmie@1
|
16
|
gremmie@1
|
17 #######################################################################
|
gremmie@1
|
18
|
gremmie@1
|
19 def register(request):
|
bgneal@74
|
20 if request.user.is_authenticated():
|
bgneal@74
|
21 return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
|
gremmie@1
|
22
|
bgneal@74
|
23 if request.method == 'POST':
|
bgneal@74
|
24 form = RegisterForm(request.POST, ip=request.META.get('REMOTE_ADDR', '?'))
|
bgneal@74
|
25 if form.is_valid():
|
bgneal@74
|
26 form.save()
|
bgneal@74
|
27 return HttpResponseRedirect(reverse('accounts.views.register_thanks'))
|
bgneal@74
|
28 else:
|
bgneal@74
|
29 form = RegisterForm()
|
gremmie@1
|
30
|
bgneal@74
|
31 return render_to_response('accounts/register.html', {
|
bgneal@74
|
32 'form': form,
|
bgneal@74
|
33 },
|
bgneal@74
|
34 context_instance = RequestContext(request))
|
gremmie@1
|
35
|
gremmie@1
|
36 #######################################################################
|
gremmie@1
|
37
|
gremmie@1
|
38 def register_thanks(request):
|
bgneal@74
|
39 if request.user.is_authenticated():
|
bgneal@74
|
40 return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
|
gremmie@1
|
41
|
bgneal@74
|
42 return render_to_response('accounts/register_thanks.html',
|
bgneal@74
|
43 context_instance = RequestContext(request))
|
gremmie@1
|
44
|
gremmie@1
|
45 #######################################################################
|
gremmie@1
|
46
|
gremmie@1
|
47 def register_confirm(request, username, key):
|
bgneal@74
|
48 if request.user.is_authenticated():
|
bgneal@74
|
49 return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
|
bgneal@74
|
50
|
bgneal@74
|
51 # purge expired users
|
gremmie@1
|
52
|
bgneal@74
|
53 PendingUser.objects.purge_expired()
|
gremmie@1
|
54
|
bgneal@74
|
55 ip = request.META.get('REMOTE_ADDR', '?')
|
bgneal@74
|
56 try:
|
bgneal@74
|
57 pending_user = PendingUser.objects.get(username = username)
|
bgneal@74
|
58 except PendingUser.DoesNotExist:
|
bgneal@74
|
59 logging.error('Accounts register_confirm [%s]: user does not exist: %s' % (ip, username))
|
bgneal@74
|
60 return render_to_response('accounts/register_failure.html', {
|
bgneal@74
|
61 'username': username,
|
bgneal@74
|
62 },
|
bgneal@74
|
63 context_instance = RequestContext(request))
|
gremmie@1
|
64
|
bgneal@74
|
65 if pending_user.key != key:
|
bgneal@74
|
66 logging.error('Accounts register_confirm [%s]: key error: %s' % (ip, username))
|
bgneal@74
|
67 return render_to_response('accounts/register_failure.html', {
|
bgneal@74
|
68 'username': username,
|
bgneal@74
|
69 },
|
bgneal@74
|
70 context_instance = RequestContext(request))
|
gremmie@1
|
71
|
bgneal@74
|
72 new_user = auth.models.User()
|
gremmie@1
|
73
|
bgneal@74
|
74 new_user.username = pending_user.username
|
bgneal@74
|
75 new_user.first_name = ''
|
bgneal@74
|
76 new_user.last_name = ''
|
bgneal@74
|
77 new_user.email = pending_user.email
|
bgneal@74
|
78 new_user.password = pending_user.password # already been hashed
|
bgneal@74
|
79 new_user.is_staff = False
|
bgneal@74
|
80 new_user.is_active = True
|
bgneal@74
|
81 new_user.is_superuser = False
|
bgneal@74
|
82 new_user.last_login = datetime.datetime.now()
|
bgneal@74
|
83 new_user.date_joined = new_user.last_login
|
gremmie@1
|
84
|
bgneal@74
|
85 new_user.save()
|
bgneal@74
|
86 pending_user.delete()
|
gremmie@1
|
87
|
bgneal@74
|
88 logging.info('Accounts register_confirm [%s]: success: %s' % (ip, username))
|
bgneal@74
|
89 return render_to_response('accounts/register_success.html', {
|
bgneal@74
|
90 'username': username,
|
bgneal@74
|
91 },
|
bgneal@74
|
92 context_instance = RequestContext(request))
|