gremmie@1: """views for the accounts application""" gremmie@1: gremmie@1: import datetime bgneal@74: import logging bgneal@74: gremmie@1: from django.shortcuts import render_to_response gremmie@1: from django.template import RequestContext gremmie@1: from django.contrib import auth gremmie@1: from django.http import HttpResponseRedirect gremmie@1: from django.core.urlresolvers import reverse bgneal@6: from django.conf import settings gremmie@1: gremmie@1: from accounts.models import PendingUser gremmie@1: from accounts.forms import RegisterForm gremmie@1: gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def register(request): bgneal@74: if request.user.is_authenticated(): bgneal@74: return HttpResponseRedirect(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@74: form.save() bgneal@74: return HttpResponseRedirect(reverse('accounts.views.register_thanks')) bgneal@74: else: bgneal@74: form = RegisterForm() gremmie@1: bgneal@74: return render_to_response('accounts/register.html', { bgneal@74: 'form': form, bgneal@74: }, bgneal@74: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def register_thanks(request): bgneal@74: if request.user.is_authenticated(): bgneal@74: return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL) gremmie@1: bgneal@74: return render_to_response('accounts/register_thanks.html', bgneal@74: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def register_confirm(request, username, key): bgneal@74: if request.user.is_authenticated(): bgneal@74: return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL) bgneal@74: 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@74: logging.error('Accounts register_confirm [%s]: user does not exist: %s' % (ip, username)) bgneal@74: return render_to_response('accounts/register_failure.html', { bgneal@74: 'username': username, bgneal@74: }, bgneal@74: context_instance = RequestContext(request)) gremmie@1: bgneal@74: if pending_user.key != key: bgneal@74: logging.error('Accounts register_confirm [%s]: key error: %s' % (ip, username)) bgneal@74: return render_to_response('accounts/register_failure.html', { bgneal@74: 'username': username, bgneal@74: }, bgneal@74: context_instance = RequestContext(request)) gremmie@1: bgneal@74: new_user = auth.models.User() gremmie@1: bgneal@74: new_user.username = pending_user.username bgneal@74: new_user.first_name = '' bgneal@74: new_user.last_name = '' bgneal@74: new_user.email = pending_user.email bgneal@74: new_user.password = pending_user.password # already been hashed bgneal@74: new_user.is_staff = False bgneal@74: new_user.is_active = True bgneal@74: new_user.is_superuser = False bgneal@74: new_user.last_login = datetime.datetime.now() bgneal@74: new_user.date_joined = new_user.last_login gremmie@1: bgneal@74: new_user.save() bgneal@74: pending_user.delete() gremmie@1: bgneal@74: logging.info('Accounts register_confirm [%s]: success: %s' % (ip, username)) bgneal@74: return render_to_response('accounts/register_success.html', { bgneal@74: 'username': username, bgneal@74: }, bgneal@74: context_instance = RequestContext(request))