gremmie@1: """views for the accounts application""" gremmie@1: gremmie@1: import datetime gremmie@1: import settings 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 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): gremmie@1: if request.user.is_authenticated(): gremmie@1: return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL) gremmie@1: gremmie@1: if request.method == 'POST': gremmie@1: form = RegisterForm(request.POST) gremmie@1: if form.is_valid(): gremmie@1: form.save() gremmie@1: return HttpResponseRedirect(reverse('accounts.views.register_thanks')) gremmie@1: else: gremmie@1: form = RegisterForm() gremmie@1: gremmie@1: return render_to_response('accounts/register.html', { gremmie@1: 'form': form, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def register_thanks(request): gremmie@1: if request.user.is_authenticated(): gremmie@1: return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL) gremmie@1: gremmie@1: return render_to_response('accounts/register_thanks.html', gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: ####################################################################### gremmie@1: gremmie@1: def register_confirm(request, username, key): gremmie@1: if request.user.is_authenticated(): gremmie@1: return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL) gremmie@1: gremmie@1: # purge expired users gremmie@1: gremmie@1: PendingUser.objects.purge_expired() gremmie@1: gremmie@1: try: gremmie@1: pending_user = PendingUser.objects.get(username = username) gremmie@1: except PendingUser.DoesNotExist: gremmie@1: return render_to_response('accounts/register_failure.html', { gremmie@1: 'username': username, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: if pending_user.key != key: gremmie@1: return render_to_response('accounts/register_failure.html', { gremmie@1: 'username': username, gremmie@1: }, gremmie@1: context_instance = RequestContext(request)) gremmie@1: gremmie@1: new_user = auth.models.User() gremmie@1: gremmie@1: new_user.username = pending_user.username gremmie@1: new_user.first_name = '' gremmie@1: new_user.last_name = '' gremmie@1: new_user.email = pending_user.email gremmie@1: new_user.password = pending_user.password # already been hashed gremmie@1: new_user.is_staff = False gremmie@1: new_user.is_active = True gremmie@1: new_user.is_superuser = False gremmie@1: new_user.last_login = datetime.datetime.now() gremmie@1: new_user.date_joined = new_user.last_login gremmie@1: gremmie@1: new_user.save() gremmie@1: pending_user.delete() gremmie@1: gremmie@1: return render_to_response('accounts/register_success.html', { gremmie@1: 'username': username, gremmie@1: }, gremmie@1: context_instance = RequestContext(request))