annotate accounts/views.py @ 697:67f8d49a9377

Cleaned up the code a bit. Separated the S3 stuff out into its own class. This class maybe should be in core. Still want to do some kind of context manager around the temporary file we are creating to ensure it gets deleted.
author Brian Neal <bgneal@gmail.com>
date Sun, 08 Sep 2013 21:02:58 -0500
parents 4a49d4ac319f
children 004b3a90de66
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@659 7 from django.shortcuts import render
bgneal@692 8 from django.http import HttpResponseRedirect
gremmie@1 9 from django.core.urlresolvers import reverse
bgneal@6 10 from django.conf import settings
gremmie@1 11
gremmie@1 12 from accounts.models import PendingUser
bgneal@659 13 from accounts.forms import RegisterForm, ForgotUsernameForm
bgneal@347 14 from accounts import create_new_user
bgneal@690 15 from antispam.decorators import log_auth_failures
gremmie@1 16
gremmie@1 17
bgneal@690 18 logger = logging.getLogger('auth')
bgneal@690 19
gremmie@1 20 #######################################################################
gremmie@1 21
bgneal@690 22 @log_auth_failures('Register')
gremmie@1 23 def register(request):
bgneal@74 24 if request.user.is_authenticated():
bgneal@74 25 return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
gremmie@1 26
bgneal@74 27 if request.method == 'POST':
bgneal@74 28 form = RegisterForm(request.POST, ip=request.META.get('REMOTE_ADDR', '?'))
bgneal@74 29 if form.is_valid():
bgneal@74 30 form.save()
bgneal@74 31 return HttpResponseRedirect(reverse('accounts.views.register_thanks'))
bgneal@74 32 else:
bgneal@74 33 form = RegisterForm()
gremmie@1 34
bgneal@659 35 return render(request, 'accounts/register.html', {'form': form})
gremmie@1 36
gremmie@1 37 #######################################################################
gremmie@1 38
gremmie@1 39 def register_thanks(request):
bgneal@74 40 if request.user.is_authenticated():
bgneal@74 41 return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
gremmie@1 42
bgneal@659 43 return render(request, 'accounts/register_thanks.html')
gremmie@1 44
gremmie@1 45 #######################################################################
gremmie@1 46
gremmie@1 47 def register_confirm(request, username, key):
bgneal@74 48 if request.user.is_authenticated():
bgneal@74 49 return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
bgneal@316 50
bgneal@74 51 # purge expired users
gremmie@1 52
bgneal@74 53 PendingUser.objects.purge_expired()
gremmie@1 54
bgneal@74 55 ip = request.META.get('REMOTE_ADDR', '?')
bgneal@74 56 try:
bgneal@74 57 pending_user = PendingUser.objects.get(username = username)
bgneal@74 58 except PendingUser.DoesNotExist:
bgneal@690 59 logger.error('Accounts register_confirm [%s]: user does not exist: %s', ip, username)
bgneal@659 60 return render(request,
bgneal@659 61 'accounts/register_failure.html',
bgneal@659 62 {'username': username})
gremmie@1 63
bgneal@74 64 if pending_user.key != key:
bgneal@690 65 logger.error('Accounts register_confirm [%s]: key error: %s', ip, username)
bgneal@659 66 return render(request,
bgneal@659 67 'accounts/register_failure.html',
bgneal@659 68 {'username': username})
gremmie@1 69
bgneal@347 70 create_new_user(pending_user, ip)
gremmie@1 71
bgneal@659 72 return render(request,
bgneal@659 73 'accounts/register_success.html',
bgneal@659 74 {'username': username})
bgneal@500 75
bgneal@500 76 #######################################################################
bgneal@500 77
bgneal@659 78 def username_query(request):
bgneal@659 79 """This view handles forgotten username queries."""
bgneal@659 80 if request.user.is_authenticated():
bgneal@659 81 return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
bgneal@659 82
bgneal@659 83 if request.method == 'POST':
bgneal@659 84 form = ForgotUsernameForm(data=request.POST)
bgneal@659 85 if form.is_valid():
bgneal@659 86 form.save()
bgneal@659 87 return HttpResponseRedirect(reverse('accounts-username_sent'))
bgneal@659 88 else:
bgneal@659 89 form = ForgotUsernameForm()
bgneal@659 90
bgneal@659 91 return render(request, 'accounts/username_query.html', {'form': form})