Mercurial > public > sg101
diff accounts/views.py @ 905:be233ba7ca31
Reworked registration process.
Previous one proved too challenging for some humans.
Hopefully made it simpler but still unusual to confuse bots.
Increased test coverage also.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 08 Mar 2015 11:06:07 -0500 |
parents | 004b3a90de66 |
children | 6dd1f0065859 |
line wrap: on
line diff
--- a/accounts/views.py Sun Mar 08 11:01:00 2015 -0500 +++ b/accounts/views.py Sun Mar 08 11:06:07 2015 -0500 @@ -2,13 +2,17 @@ Views for the accounts application. """ +import json import logging +from django.http import HttpResponse from django.shortcuts import render, redirect from django.conf import settings from accounts.models import PendingUser -from accounts.forms import RegisterForm, ForgotUsernameForm +from accounts.forms import RegisterForm +from accounts.forms import RegisterCodeForm +from accounts.forms import ForgotUsernameForm from accounts import create_new_user from antispam.decorators import log_auth_failures @@ -25,8 +29,8 @@ if request.method == 'POST': form = RegisterForm(request.POST, ip=request.META.get('REMOTE_ADDR', '?')) if form.is_valid(): - form.save() - return redirect('accounts.views.register_thanks') + form.save(request) + return redirect('accounts-register1') else: form = RegisterForm() @@ -34,6 +38,45 @@ ####################################################################### +def register1(request): + """Displays the registration code.""" + if request.user.is_authenticated(): + return redirect(settings.LOGIN_REDIRECT_URL) + + return render(request, 'accounts/register1.html') + +####################################################################### + +@log_auth_failures('Register') +def register2(request): + """Processes the registration code and creates the user.""" + if request.user.is_authenticated(): + return redirect(settings.LOGIN_REDIRECT_URL) + + if request.method == 'POST': + form = RegisterCodeForm(request.POST, + session=request.session, + ip=request.META.get('REMOTE_ADDR', '?')) + if form.is_valid(): + form.save() + return redirect('accounts-register_thanks') + else: + form = RegisterCodeForm() + + return render(request, 'accounts/register2.html', {'form': form}) + +####################################################################### + +def get_code(request): + code = {'code': 'FAIL-123'} + reg_info = request.session.get('reg_info') + if reg_info: + code['code'] = reg_info.get('code', code['code']) + + return HttpResponse(json.dumps(code), content_type='application/json') + +####################################################################### + def register_thanks(request): if request.user.is_authenticated(): return redirect(settings.LOGIN_REDIRECT_URL)