diff accounts/views.py @ 659:8e6b8ffe5f34

For issue #31, implement a forgot username feature.
author Brian Neal <bgneal@gmail.com>
date Sat, 11 May 2013 23:39:46 -0500
parents ee87ea74d46b
children 89b240fe9297
line wrap: on
line diff
--- a/accounts/views.py	Sat May 11 16:21:55 2013 -0500
+++ b/accounts/views.py	Sat May 11 23:39:46 2013 -0500
@@ -5,10 +5,9 @@
 import datetime
 import logging
 
-from django.shortcuts import render_to_response
+from django.shortcuts import render
 from django.template import RequestContext
 from django.template.loader import render_to_string
-from django.contrib.auth.models import User
 from django.http import HttpResponse, HttpResponseRedirect
 from django.core.urlresolvers import reverse
 from django.conf import settings
@@ -17,7 +16,7 @@
 from django.utils import simplejson
 
 from accounts.models import PendingUser
-from accounts.forms import RegisterForm
+from accounts.forms import RegisterForm, ForgotUsernameForm
 from accounts import create_new_user
 from antispam.decorators import rate_limit
 
@@ -37,10 +36,7 @@
     else:
         form = RegisterForm()
 
-    return render_to_response('accounts/register.html', {
-                'form': form,
-            },
-            context_instance = RequestContext(request))
+    return render(request, 'accounts/register.html', {'form': form})
 
 #######################################################################
 
@@ -48,8 +44,7 @@
     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(request, 'accounts/register_thanks.html')
 
 #######################################################################
 
@@ -66,24 +61,21 @@
         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))
+        return render(request,
+                  'accounts/register_failure.html',
+                  {'username': username})
 
     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))
+        return render(request,
+                'accounts/register_failure.html',
+                {'username': username})
 
     create_new_user(pending_user, ip)
 
-    return render_to_response('accounts/register_success.html', {
-        'username': username,
-        },
-        context_instance = RequestContext(request))
+    return render(request,
+            'accounts/register_success.html',
+            {'username': username})
 
 #######################################################################
 
@@ -115,3 +107,20 @@
 
     return HttpResponse(simplejson.dumps(response),
             content_type='application/json')
+
+#######################################################################
+
+def username_query(request):
+    """This view handles forgotten username queries."""
+    if request.user.is_authenticated():
+        return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
+
+    if request.method == 'POST':
+        form = ForgotUsernameForm(data=request.POST)
+        if form.is_valid():
+            form.save()
+            return HttpResponseRedirect(reverse('accounts-username_sent'))
+    else:
+        form = ForgotUsernameForm()
+
+    return render(request, 'accounts/username_query.html', {'form': form})