annotate accounts/views.py @ 1127:829d3b7fc0f7

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