annotate accounts/views.py @ 847:ba9216950eb1

Update NSSR Halloween banner.
author Brian Neal <bgneal@gmail.com>
date Mon, 27 Oct 2014 18:53:07 -0500
parents 004b3a90de66
children be233ba7ca31
rev   line source
bgneal@500 1 """
bgneal@500 2 Views for the accounts application.
gremmie@1 3
bgneal@500 4 """
bgneal@74 5 import logging
bgneal@74 6
bgneal@783 7 from django.shortcuts import render, redirect
bgneal@6 8 from django.conf import settings
gremmie@1 9
gremmie@1 10 from accounts.models import PendingUser
bgneal@659 11 from accounts.forms import RegisterForm, ForgotUsernameForm
bgneal@347 12 from accounts import create_new_user
bgneal@690 13 from antispam.decorators import log_auth_failures
gremmie@1 14
gremmie@1 15
bgneal@690 16 logger = logging.getLogger('auth')
bgneal@690 17
gremmie@1 18 #######################################################################
gremmie@1 19
bgneal@690 20 @log_auth_failures('Register')
gremmie@1 21 def register(request):
bgneal@74 22 if request.user.is_authenticated():
bgneal@783 23 return redirect(settings.LOGIN_REDIRECT_URL)
gremmie@1 24
bgneal@74 25 if request.method == 'POST':
bgneal@74 26 form = RegisterForm(request.POST, ip=request.META.get('REMOTE_ADDR', '?'))
bgneal@74 27 if form.is_valid():
bgneal@74 28 form.save()
bgneal@783 29 return redirect('accounts.views.register_thanks')
bgneal@74 30 else:
bgneal@74 31 form = RegisterForm()
gremmie@1 32
bgneal@659 33 return render(request, 'accounts/register.html', {'form': form})
gremmie@1 34
gremmie@1 35 #######################################################################
gremmie@1 36
gremmie@1 37 def register_thanks(request):
bgneal@74 38 if request.user.is_authenticated():
bgneal@783 39 return redirect(settings.LOGIN_REDIRECT_URL)
gremmie@1 40
bgneal@659 41 return render(request, 'accounts/register_thanks.html')
gremmie@1 42
gremmie@1 43 #######################################################################
gremmie@1 44
gremmie@1 45 def register_confirm(request, username, key):
bgneal@74 46 if request.user.is_authenticated():
bgneal@783 47 return redirect(settings.LOGIN_REDIRECT_URL)
bgneal@316 48
bgneal@74 49 # purge expired users
gremmie@1 50
bgneal@74 51 PendingUser.objects.purge_expired()
gremmie@1 52
bgneal@74 53 ip = request.META.get('REMOTE_ADDR', '?')
bgneal@74 54 try:
bgneal@74 55 pending_user = PendingUser.objects.get(username = username)
bgneal@74 56 except PendingUser.DoesNotExist:
bgneal@690 57 logger.error('Accounts register_confirm [%s]: user does not exist: %s', ip, username)
bgneal@659 58 return render(request,
bgneal@659 59 'accounts/register_failure.html',
bgneal@659 60 {'username': username})
gremmie@1 61
bgneal@74 62 if pending_user.key != key:
bgneal@690 63 logger.error('Accounts register_confirm [%s]: key error: %s', ip, username)
bgneal@659 64 return render(request,
bgneal@659 65 'accounts/register_failure.html',
bgneal@659 66 {'username': username})
gremmie@1 67
bgneal@347 68 create_new_user(pending_user, ip)
gremmie@1 69
bgneal@659 70 return render(request,
bgneal@659 71 'accounts/register_success.html',
bgneal@659 72 {'username': username})
bgneal@500 73
bgneal@500 74 #######################################################################
bgneal@500 75
bgneal@659 76 def username_query(request):
bgneal@659 77 """This view handles forgotten username queries."""
bgneal@659 78 if request.user.is_authenticated():
bgneal@783 79 return redirect(settings.LOGIN_REDIRECT_URL)
bgneal@659 80
bgneal@659 81 if request.method == 'POST':
bgneal@659 82 form = ForgotUsernameForm(data=request.POST)
bgneal@659 83 if form.is_valid():
bgneal@659 84 form.save()
bgneal@783 85 return redirect('accounts-username_sent')
bgneal@659 86 else:
bgneal@659 87 form = ForgotUsernameForm()
bgneal@659 88
bgneal@659 89 return render(request, 'accounts/username_query.html', {'form': form})