annotate accounts/views.py @ 1114:6dd1f0065859

Convert registration views to V3 design.
author Brian Neal <bgneal@gmail.com>
date Tue, 12 Jul 2016 20:44:20 -0500
parents be233ba7ca31
children d613d25a021e
rev   line source
bgneal@500 1 """
bgneal@500 2 Views for the accounts application.
gremmie@1 3
bgneal@500 4 """
bgneal@905 5 import json
bgneal@74 6 import logging
bgneal@74 7
bgneal@905 8 from django.http import HttpResponse
bgneal@783 9 from django.shortcuts import render, redirect
bgneal@6 10 from django.conf import settings
gremmie@1 11
gremmie@1 12 from accounts.models import PendingUser
bgneal@905 13 from accounts.forms import RegisterForm
bgneal@905 14 from accounts.forms import RegisterCodeForm
bgneal@905 15 from accounts.forms import ForgotUsernameForm
bgneal@347 16 from accounts import create_new_user
bgneal@690 17 from antispam.decorators import log_auth_failures
gremmie@1 18
gremmie@1 19
bgneal@690 20 logger = logging.getLogger('auth')
bgneal@690 21
gremmie@1 22 #######################################################################
gremmie@1 23
bgneal@690 24 @log_auth_failures('Register')
gremmie@1 25 def register(request):
bgneal@74 26 if request.user.is_authenticated():
bgneal@783 27 return redirect(settings.LOGIN_REDIRECT_URL)
gremmie@1 28
bgneal@74 29 if request.method == 'POST':
bgneal@74 30 form = RegisterForm(request.POST, ip=request.META.get('REMOTE_ADDR', '?'))
bgneal@74 31 if form.is_valid():
bgneal@905 32 form.save(request)
bgneal@905 33 return redirect('accounts-register1')
bgneal@74 34 else:
bgneal@74 35 form = RegisterForm()
gremmie@1 36
bgneal@1114 37 return render(request, 'accounts/register.html', {
bgneal@1114 38 'form': form,
bgneal@1114 39 'V3_DESIGN': True,
bgneal@1114 40 })
gremmie@1 41
gremmie@1 42 #######################################################################
gremmie@1 43
bgneal@905 44 def register1(request):
bgneal@905 45 """Displays the registration code."""
bgneal@905 46 if request.user.is_authenticated():
bgneal@905 47 return redirect(settings.LOGIN_REDIRECT_URL)
bgneal@905 48
bgneal@1114 49 return render(request, 'accounts/register1.html', {
bgneal@1114 50 'V3_DESIGN': True,
bgneal@1114 51 })
bgneal@905 52
bgneal@905 53 #######################################################################
bgneal@905 54
bgneal@905 55 @log_auth_failures('Register')
bgneal@905 56 def register2(request):
bgneal@905 57 """Processes the registration code and creates the user."""
bgneal@905 58 if request.user.is_authenticated():
bgneal@905 59 return redirect(settings.LOGIN_REDIRECT_URL)
bgneal@905 60
bgneal@905 61 if request.method == 'POST':
bgneal@905 62 form = RegisterCodeForm(request.POST,
bgneal@905 63 session=request.session,
bgneal@905 64 ip=request.META.get('REMOTE_ADDR', '?'))
bgneal@905 65 if form.is_valid():
bgneal@905 66 form.save()
bgneal@905 67 return redirect('accounts-register_thanks')
bgneal@905 68 else:
bgneal@905 69 form = RegisterCodeForm()
bgneal@905 70
bgneal@1114 71 return render(request, 'accounts/register2.html', {
bgneal@1114 72 'form': form,
bgneal@1114 73 'V3_DESIGN': True,
bgneal@1114 74 })
bgneal@905 75
bgneal@905 76 #######################################################################
bgneal@905 77
bgneal@905 78 def get_code(request):
bgneal@905 79 code = {'code': 'FAIL-123'}
bgneal@905 80 reg_info = request.session.get('reg_info')
bgneal@905 81 if reg_info:
bgneal@905 82 code['code'] = reg_info.get('code', code['code'])
bgneal@905 83
bgneal@905 84 return HttpResponse(json.dumps(code), content_type='application/json')
bgneal@905 85
bgneal@905 86 #######################################################################
bgneal@905 87
gremmie@1 88 def register_thanks(request):
bgneal@74 89 if request.user.is_authenticated():
bgneal@783 90 return redirect(settings.LOGIN_REDIRECT_URL)
gremmie@1 91
bgneal@1114 92 return render(request, 'accounts/register_thanks.html', {
bgneal@1114 93 'V3_DESIGN': True,
bgneal@1114 94 })
gremmie@1 95
gremmie@1 96 #######################################################################
gremmie@1 97
gremmie@1 98 def register_confirm(request, username, key):
bgneal@74 99 if request.user.is_authenticated():
bgneal@783 100 return redirect(settings.LOGIN_REDIRECT_URL)
bgneal@316 101
bgneal@74 102 # purge expired users
gremmie@1 103
bgneal@74 104 PendingUser.objects.purge_expired()
gremmie@1 105
bgneal@74 106 ip = request.META.get('REMOTE_ADDR', '?')
bgneal@74 107 try:
bgneal@74 108 pending_user = PendingUser.objects.get(username = username)
bgneal@74 109 except PendingUser.DoesNotExist:
bgneal@1114 110 logger.error('Accounts register_confirm [%s]: user does not exist: %s',
bgneal@1114 111 ip, username)
bgneal@1114 112 return render(request, 'accounts/register_failure.html', {
bgneal@1114 113 'username': username,
bgneal@1114 114 'V3_DESIGN': True,
bgneal@1114 115 })
gremmie@1 116
bgneal@74 117 if pending_user.key != key:
bgneal@690 118 logger.error('Accounts register_confirm [%s]: key error: %s', ip, username)
bgneal@1114 119 return render(request, 'accounts/register_failure.html', {
bgneal@1114 120 'username': username,
bgneal@1114 121 'V3_DESIGN': True,
bgneal@1114 122 })
gremmie@1 123
bgneal@347 124 create_new_user(pending_user, ip)
gremmie@1 125
bgneal@1114 126 return render(request, 'accounts/register_success.html', {
bgneal@1114 127 'username': username,
bgneal@1114 128 'V3_DESIGN': True,
bgneal@1114 129 })
bgneal@500 130
bgneal@500 131 #######################################################################
bgneal@500 132
bgneal@659 133 def username_query(request):
bgneal@659 134 """This view handles forgotten username queries."""
bgneal@659 135 if request.user.is_authenticated():
bgneal@783 136 return redirect(settings.LOGIN_REDIRECT_URL)
bgneal@659 137
bgneal@659 138 if request.method == 'POST':
bgneal@659 139 form = ForgotUsernameForm(data=request.POST)
bgneal@659 140 if form.is_valid():
bgneal@659 141 form.save()
bgneal@783 142 return redirect('accounts-username_sent')
bgneal@659 143 else:
bgneal@659 144 form = ForgotUsernameForm()
bgneal@659 145
bgneal@659 146 return render(request, 'accounts/username_query.html', {'form': form})