Mercurial > public > sg101
comparison 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 |
comparison
equal
deleted
inserted
replaced
658:2adf01661ac5 | 659:8e6b8ffe5f34 |
---|---|
3 | 3 |
4 """ | 4 """ |
5 import datetime | 5 import datetime |
6 import logging | 6 import logging |
7 | 7 |
8 from django.shortcuts import render_to_response | 8 from django.shortcuts import render |
9 from django.template import RequestContext | 9 from django.template import RequestContext |
10 from django.template.loader import render_to_string | 10 from django.template.loader import render_to_string |
11 from django.contrib.auth.models import User | |
12 from django.http import HttpResponse, HttpResponseRedirect | 11 from django.http import HttpResponse, HttpResponseRedirect |
13 from django.core.urlresolvers import reverse | 12 from django.core.urlresolvers import reverse |
14 from django.conf import settings | 13 from django.conf import settings |
15 from django.contrib.auth.forms import AuthenticationForm | 14 from django.contrib.auth.forms import AuthenticationForm |
16 from django.contrib.auth import login | 15 from django.contrib.auth import login |
17 from django.utils import simplejson | 16 from django.utils import simplejson |
18 | 17 |
19 from accounts.models import PendingUser | 18 from accounts.models import PendingUser |
20 from accounts.forms import RegisterForm | 19 from accounts.forms import RegisterForm, ForgotUsernameForm |
21 from accounts import create_new_user | 20 from accounts import create_new_user |
22 from antispam.decorators import rate_limit | 21 from antispam.decorators import rate_limit |
23 | 22 |
24 | 23 |
25 ####################################################################### | 24 ####################################################################### |
35 form.save() | 34 form.save() |
36 return HttpResponseRedirect(reverse('accounts.views.register_thanks')) | 35 return HttpResponseRedirect(reverse('accounts.views.register_thanks')) |
37 else: | 36 else: |
38 form = RegisterForm() | 37 form = RegisterForm() |
39 | 38 |
40 return render_to_response('accounts/register.html', { | 39 return render(request, 'accounts/register.html', {'form': form}) |
41 'form': form, | |
42 }, | |
43 context_instance = RequestContext(request)) | |
44 | 40 |
45 ####################################################################### | 41 ####################################################################### |
46 | 42 |
47 def register_thanks(request): | 43 def register_thanks(request): |
48 if request.user.is_authenticated(): | 44 if request.user.is_authenticated(): |
49 return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL) | 45 return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL) |
50 | 46 |
51 return render_to_response('accounts/register_thanks.html', | 47 return render(request, 'accounts/register_thanks.html') |
52 context_instance = RequestContext(request)) | |
53 | 48 |
54 ####################################################################### | 49 ####################################################################### |
55 | 50 |
56 def register_confirm(request, username, key): | 51 def register_confirm(request, username, key): |
57 if request.user.is_authenticated(): | 52 if request.user.is_authenticated(): |
64 ip = request.META.get('REMOTE_ADDR', '?') | 59 ip = request.META.get('REMOTE_ADDR', '?') |
65 try: | 60 try: |
66 pending_user = PendingUser.objects.get(username = username) | 61 pending_user = PendingUser.objects.get(username = username) |
67 except PendingUser.DoesNotExist: | 62 except PendingUser.DoesNotExist: |
68 logging.error('Accounts register_confirm [%s]: user does not exist: %s', ip, username) | 63 logging.error('Accounts register_confirm [%s]: user does not exist: %s', ip, username) |
69 return render_to_response('accounts/register_failure.html', { | 64 return render(request, |
70 'username': username, | 65 'accounts/register_failure.html', |
71 }, | 66 {'username': username}) |
72 context_instance = RequestContext(request)) | |
73 | 67 |
74 if pending_user.key != key: | 68 if pending_user.key != key: |
75 logging.error('Accounts register_confirm [%s]: key error: %s', ip, username) | 69 logging.error('Accounts register_confirm [%s]: key error: %s', ip, username) |
76 return render_to_response('accounts/register_failure.html', { | 70 return render(request, |
77 'username': username, | 71 'accounts/register_failure.html', |
78 }, | 72 {'username': username}) |
79 context_instance = RequestContext(request)) | |
80 | 73 |
81 create_new_user(pending_user, ip) | 74 create_new_user(pending_user, ip) |
82 | 75 |
83 return render_to_response('accounts/register_success.html', { | 76 return render(request, |
84 'username': username, | 77 'accounts/register_success.html', |
85 }, | 78 {'username': username}) |
86 context_instance = RequestContext(request)) | |
87 | 79 |
88 ####################################################################### | 80 ####################################################################### |
89 | 81 |
90 @rate_limit(count=10, interval=datetime.timedelta(minutes=1), | 82 @rate_limit(count=10, interval=datetime.timedelta(minutes=1), |
91 lockout=datetime.timedelta(minutes=2)) | 83 lockout=datetime.timedelta(minutes=2)) |
113 else: | 105 else: |
114 response['error'] = 'Invalid username or password' | 106 response['error'] = 'Invalid username or password' |
115 | 107 |
116 return HttpResponse(simplejson.dumps(response), | 108 return HttpResponse(simplejson.dumps(response), |
117 content_type='application/json') | 109 content_type='application/json') |
110 | |
111 ####################################################################### | |
112 | |
113 def username_query(request): | |
114 """This view handles forgotten username queries.""" | |
115 if request.user.is_authenticated(): | |
116 return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL) | |
117 | |
118 if request.method == 'POST': | |
119 form = ForgotUsernameForm(data=request.POST) | |
120 if form.is_valid(): | |
121 form.save() | |
122 return HttpResponseRedirect(reverse('accounts-username_sent')) | |
123 else: | |
124 form = ForgotUsernameForm() | |
125 | |
126 return render(request, 'accounts/username_query.html', {'form': form}) |