annotate accounts/views.py @ 953:8647a669edb4

Fix excessive cache usage for forum date/times. Issue #84. Hitting the cache 30+ times while browsing the forums to adjust all the dates/times into the user's time zone. Just hit the user's profile and be done with it. It should be loaded.
author Brian Neal <bgneal@gmail.com>
date Tue, 19 May 2015 21:08:45 -0500
parents be233ba7ca31
children 6dd1f0065859
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@659 37 return render(request, 'accounts/register.html', {'form': form})
gremmie@1 38
gremmie@1 39 #######################################################################
gremmie@1 40
bgneal@905 41 def register1(request):
bgneal@905 42 """Displays the registration code."""
bgneal@905 43 if request.user.is_authenticated():
bgneal@905 44 return redirect(settings.LOGIN_REDIRECT_URL)
bgneal@905 45
bgneal@905 46 return render(request, 'accounts/register1.html')
bgneal@905 47
bgneal@905 48 #######################################################################
bgneal@905 49
bgneal@905 50 @log_auth_failures('Register')
bgneal@905 51 def register2(request):
bgneal@905 52 """Processes the registration code and creates the user."""
bgneal@905 53 if request.user.is_authenticated():
bgneal@905 54 return redirect(settings.LOGIN_REDIRECT_URL)
bgneal@905 55
bgneal@905 56 if request.method == 'POST':
bgneal@905 57 form = RegisterCodeForm(request.POST,
bgneal@905 58 session=request.session,
bgneal@905 59 ip=request.META.get('REMOTE_ADDR', '?'))
bgneal@905 60 if form.is_valid():
bgneal@905 61 form.save()
bgneal@905 62 return redirect('accounts-register_thanks')
bgneal@905 63 else:
bgneal@905 64 form = RegisterCodeForm()
bgneal@905 65
bgneal@905 66 return render(request, 'accounts/register2.html', {'form': form})
bgneal@905 67
bgneal@905 68 #######################################################################
bgneal@905 69
bgneal@905 70 def get_code(request):
bgneal@905 71 code = {'code': 'FAIL-123'}
bgneal@905 72 reg_info = request.session.get('reg_info')
bgneal@905 73 if reg_info:
bgneal@905 74 code['code'] = reg_info.get('code', code['code'])
bgneal@905 75
bgneal@905 76 return HttpResponse(json.dumps(code), content_type='application/json')
bgneal@905 77
bgneal@905 78 #######################################################################
bgneal@905 79
gremmie@1 80 def register_thanks(request):
bgneal@74 81 if request.user.is_authenticated():
bgneal@783 82 return redirect(settings.LOGIN_REDIRECT_URL)
gremmie@1 83
bgneal@659 84 return render(request, 'accounts/register_thanks.html')
gremmie@1 85
gremmie@1 86 #######################################################################
gremmie@1 87
gremmie@1 88 def register_confirm(request, username, key):
bgneal@74 89 if request.user.is_authenticated():
bgneal@783 90 return redirect(settings.LOGIN_REDIRECT_URL)
bgneal@316 91
bgneal@74 92 # purge expired users
gremmie@1 93
bgneal@74 94 PendingUser.objects.purge_expired()
gremmie@1 95
bgneal@74 96 ip = request.META.get('REMOTE_ADDR', '?')
bgneal@74 97 try:
bgneal@74 98 pending_user = PendingUser.objects.get(username = username)
bgneal@74 99 except PendingUser.DoesNotExist:
bgneal@690 100 logger.error('Accounts register_confirm [%s]: user does not exist: %s', ip, username)
bgneal@659 101 return render(request,
bgneal@659 102 'accounts/register_failure.html',
bgneal@659 103 {'username': username})
gremmie@1 104
bgneal@74 105 if pending_user.key != key:
bgneal@690 106 logger.error('Accounts register_confirm [%s]: key error: %s', ip, username)
bgneal@659 107 return render(request,
bgneal@659 108 'accounts/register_failure.html',
bgneal@659 109 {'username': username})
gremmie@1 110
bgneal@347 111 create_new_user(pending_user, ip)
gremmie@1 112
bgneal@659 113 return render(request,
bgneal@659 114 'accounts/register_success.html',
bgneal@659 115 {'username': username})
bgneal@500 116
bgneal@500 117 #######################################################################
bgneal@500 118
bgneal@659 119 def username_query(request):
bgneal@659 120 """This view handles forgotten username queries."""
bgneal@659 121 if request.user.is_authenticated():
bgneal@783 122 return redirect(settings.LOGIN_REDIRECT_URL)
bgneal@659 123
bgneal@659 124 if request.method == 'POST':
bgneal@659 125 form = ForgotUsernameForm(data=request.POST)
bgneal@659 126 if form.is_valid():
bgneal@659 127 form.save()
bgneal@783 128 return redirect('accounts-username_sent')
bgneal@659 129 else:
bgneal@659 130 form = ForgotUsernameForm()
bgneal@659 131
bgneal@659 132 return render(request, 'accounts/username_query.html', {'form': form})