comparison gpp/accounts/views.py @ 74:df56795771a6

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