bgneal@500: """ bgneal@500: Views for the accounts application. gremmie@1: bgneal@500: """ bgneal@905: import json bgneal@74: import logging bgneal@74: bgneal@905: from django.http import HttpResponse bgneal@783: from django.shortcuts import render, redirect bgneal@6: from django.conf import settings bgneal@1115: from django.views.generic import TemplateView gremmie@1: gremmie@1: from accounts.models import PendingUser bgneal@905: from accounts.forms import RegisterForm bgneal@905: from accounts.forms import RegisterCodeForm bgneal@905: from accounts.forms import ForgotUsernameForm bgneal@347: from accounts import create_new_user bgneal@690: from antispam.decorators import log_auth_failures gremmie@1: gremmie@1: bgneal@690: logger = logging.getLogger('auth') bgneal@690: gremmie@1: ####################################################################### gremmie@1: bgneal@690: @log_auth_failures('Register') gremmie@1: def register(request): bgneal@74: if request.user.is_authenticated(): bgneal@783: return redirect(settings.LOGIN_REDIRECT_URL) gremmie@1: bgneal@74: if request.method == 'POST': bgneal@74: form = RegisterForm(request.POST, ip=request.META.get('REMOTE_ADDR', '?')) bgneal@74: if form.is_valid(): bgneal@905: form.save(request) bgneal@905: return redirect('accounts-register1') bgneal@74: else: bgneal@74: form = RegisterForm() gremmie@1: bgneal@1114: return render(request, 'accounts/register.html', { bgneal@1114: 'form': form, bgneal@1114: 'V3_DESIGN': True, bgneal@1114: }) gremmie@1: gremmie@1: ####################################################################### gremmie@1: bgneal@905: def register1(request): bgneal@905: """Displays the registration code.""" bgneal@905: if request.user.is_authenticated(): bgneal@905: return redirect(settings.LOGIN_REDIRECT_URL) bgneal@905: bgneal@1114: return render(request, 'accounts/register1.html', { bgneal@1114: 'V3_DESIGN': True, bgneal@1114: }) bgneal@905: bgneal@905: ####################################################################### bgneal@905: bgneal@905: @log_auth_failures('Register') bgneal@905: def register2(request): bgneal@905: """Processes the registration code and creates the user.""" bgneal@905: if request.user.is_authenticated(): bgneal@905: return redirect(settings.LOGIN_REDIRECT_URL) bgneal@905: bgneal@905: if request.method == 'POST': bgneal@905: form = RegisterCodeForm(request.POST, bgneal@905: session=request.session, bgneal@905: ip=request.META.get('REMOTE_ADDR', '?')) bgneal@905: if form.is_valid(): bgneal@905: form.save() bgneal@905: return redirect('accounts-register_thanks') bgneal@905: else: bgneal@905: form = RegisterCodeForm() bgneal@905: bgneal@1114: return render(request, 'accounts/register2.html', { bgneal@1114: 'form': form, bgneal@1114: 'V3_DESIGN': True, bgneal@1114: }) bgneal@905: bgneal@905: ####################################################################### bgneal@905: bgneal@905: def get_code(request): bgneal@905: code = {'code': 'FAIL-123'} bgneal@905: reg_info = request.session.get('reg_info') bgneal@905: if reg_info: bgneal@905: code['code'] = reg_info.get('code', code['code']) bgneal@905: bgneal@905: return HttpResponse(json.dumps(code), content_type='application/json') bgneal@905: bgneal@905: ####################################################################### bgneal@905: gremmie@1: def register_thanks(request): bgneal@74: if request.user.is_authenticated(): bgneal@783: return redirect(settings.LOGIN_REDIRECT_URL) gremmie@1: bgneal@1114: return render(request, 'accounts/register_thanks.html', { bgneal@1114: 'V3_DESIGN': True, bgneal@1114: }) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def register_confirm(request, username, key): bgneal@74: if request.user.is_authenticated(): bgneal@783: return redirect(settings.LOGIN_REDIRECT_URL) bgneal@316: bgneal@74: # purge expired users gremmie@1: bgneal@74: PendingUser.objects.purge_expired() gremmie@1: bgneal@74: ip = request.META.get('REMOTE_ADDR', '?') bgneal@74: try: bgneal@74: pending_user = PendingUser.objects.get(username = username) bgneal@74: except PendingUser.DoesNotExist: bgneal@1114: logger.error('Accounts register_confirm [%s]: user does not exist: %s', bgneal@1114: ip, username) bgneal@1114: return render(request, 'accounts/register_failure.html', { bgneal@1114: 'username': username, bgneal@1114: 'V3_DESIGN': True, bgneal@1114: }) gremmie@1: bgneal@74: if pending_user.key != key: bgneal@690: logger.error('Accounts register_confirm [%s]: key error: %s', ip, username) bgneal@1114: return render(request, 'accounts/register_failure.html', { bgneal@1114: 'username': username, bgneal@1114: 'V3_DESIGN': True, bgneal@1114: }) gremmie@1: bgneal@347: create_new_user(pending_user, ip) gremmie@1: bgneal@1114: return render(request, 'accounts/register_success.html', { bgneal@1114: 'username': username, bgneal@1114: 'V3_DESIGN': True, bgneal@1114: }) bgneal@500: bgneal@500: ####################################################################### bgneal@500: bgneal@659: def username_query(request): bgneal@659: """This view handles forgotten username queries.""" bgneal@659: if request.user.is_authenticated(): bgneal@783: return redirect(settings.LOGIN_REDIRECT_URL) bgneal@659: bgneal@659: if request.method == 'POST': bgneal@659: form = ForgotUsernameForm(data=request.POST) bgneal@659: if form.is_valid(): bgneal@659: form.save() bgneal@783: return redirect('accounts-username_sent') bgneal@659: else: bgneal@659: form = ForgotUsernameForm() bgneal@659: bgneal@1115: return render(request, 'accounts/username_query.html', { bgneal@1115: 'form': form, bgneal@1115: 'V3_DESIGN': True, bgneal@1115: }) bgneal@1115: bgneal@1115: bgneal@1115: class UsernameSentView(TemplateView): bgneal@1115: template_name = 'accounts/username_sent.html' bgneal@1115: bgneal@1115: def get_context_data(self, **kwargs): bgneal@1115: context = super(UsernameSentView, self).get_context_data(**kwargs) bgneal@1115: context['V3_DESIGN'] = True bgneal@1115: return context