diff 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
line wrap: on
line diff
--- a/gpp/accounts/views.py	Wed Jul 01 18:50:49 2009 +0000
+++ b/gpp/accounts/views.py	Wed Jul 01 20:02:14 2009 +0000
@@ -1,6 +1,8 @@
 """views for the accounts application"""
 
 import datetime
+import logging
+
 from django.shortcuts import render_to_response
 from django.template import RequestContext
 from django.contrib import auth
@@ -15,72 +17,76 @@
 #######################################################################
 
 def register(request):
-   if request.user.is_authenticated():
-      return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
+    if request.user.is_authenticated():
+        return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
 
-   if request.method == 'POST':
-      form = RegisterForm(request.POST)
-      if form.is_valid():
-         form.save()
-         return HttpResponseRedirect(reverse('accounts.views.register_thanks'))
-   else:
-      form = RegisterForm()
+    if request.method == 'POST':
+        form = RegisterForm(request.POST, ip=request.META.get('REMOTE_ADDR', '?'))
+        if form.is_valid():
+            form.save()
+            return HttpResponseRedirect(reverse('accounts.views.register_thanks'))
+    else:
+        form = RegisterForm()
 
-   return render_to_response('accounts/register.html', {
-            'form': form,
-         },
-         context_instance = RequestContext(request))
+    return render_to_response('accounts/register.html', {
+                'form': form,
+            },
+            context_instance = RequestContext(request))
 
 #######################################################################
 
 def register_thanks(request):
-   if request.user.is_authenticated():
-      return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
+    if request.user.is_authenticated():
+        return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
 
-   return render_to_response('accounts/register_thanks.html',
-         context_instance = RequestContext(request))
+    return render_to_response('accounts/register_thanks.html',
+            context_instance = RequestContext(request))
 
 #######################################################################
 
 def register_confirm(request, username, key):
-   if request.user.is_authenticated():
-      return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
-   
-   # purge expired users
+    if request.user.is_authenticated():
+        return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
+    
+    # purge expired users
 
-   PendingUser.objects.purge_expired()
+    PendingUser.objects.purge_expired()
 
-   try:
-      pending_user = PendingUser.objects.get(username = username)
-   except PendingUser.DoesNotExist:
-      return render_to_response('accounts/register_failure.html', {
-         'username': username,
-         },
-         context_instance = RequestContext(request))
+    ip = request.META.get('REMOTE_ADDR', '?')
+    try:
+        pending_user = PendingUser.objects.get(username = username)
+    except PendingUser.DoesNotExist:
+        logging.error('Accounts register_confirm [%s]: user does not exist: %s' % (ip, username))
+        return render_to_response('accounts/register_failure.html', {
+            'username': username,
+            },
+            context_instance = RequestContext(request))
 
-   if pending_user.key != key:
-      return render_to_response('accounts/register_failure.html', {
-         'username': username,
-         },
-         context_instance = RequestContext(request))
+    if pending_user.key != key:
+        logging.error('Accounts register_confirm [%s]: key error: %s' % (ip, username))
+        return render_to_response('accounts/register_failure.html', {
+            'username': username,
+            },
+            context_instance = RequestContext(request))
 
-   new_user = auth.models.User()
+    new_user = auth.models.User()
 
-   new_user.username = pending_user.username
-   new_user.first_name = ''
-   new_user.last_name = ''
-   new_user.email = pending_user.email
-   new_user.password = pending_user.password    # already been hashed
-   new_user.is_staff = False
-   new_user.is_active = True
-   new_user.is_superuser = False
-   new_user.last_login = datetime.datetime.now()
-   new_user.date_joined = new_user.last_login
+    new_user.username = pending_user.username
+    new_user.first_name = ''
+    new_user.last_name = ''
+    new_user.email = pending_user.email
+    new_user.password = pending_user.password     # already been hashed
+    new_user.is_staff = False
+    new_user.is_active = True
+    new_user.is_superuser = False
+    new_user.last_login = datetime.datetime.now()
+    new_user.date_joined = new_user.last_login
 
-   new_user.save()
-   pending_user.delete()
+    new_user.save()
+    pending_user.delete()
 
-   return render_to_response('accounts/register_success.html', {
-      'username': username,
-      },
-      context_instance = RequestContext(request))
+    logging.info('Accounts register_confirm [%s]: success: %s' % (ip, username))
+    return render_to_response('accounts/register_success.html', {
+        'username': username,
+        },
+        context_instance = RequestContext(request))