comparison gpp/accounts/views.py @ 500:886cc99e8406

For #240, add an "ajaxy" login via a jQuery UI pop-up dialog to streamline the login process.
author Brian Neal <bgneal@gmail.com>
date Sat, 03 Dec 2011 02:48:18 +0000
parents 7c3816d76c6c
children 2c2df8545112
comparison
equal deleted inserted replaced
499:1a09a7bea000 500:886cc99e8406
1 """views for the accounts application""" 1 """
2 Views for the accounts application.
2 3
4 """
3 import datetime 5 import datetime
4 import logging 6 import logging
5 7
6 from django.shortcuts import render_to_response 8 from django.shortcuts import render_to_response
7 from django.template import RequestContext 9 from django.template import RequestContext
10 from django.template.loader import render_to_string
8 from django.contrib.auth.models import User 11 from django.contrib.auth.models import User
9 from django.http import HttpResponseRedirect 12 from django.http import HttpResponse, HttpResponseRedirect
10 from django.core.urlresolvers import reverse 13 from django.core.urlresolvers import reverse
11 from django.conf import settings 14 from django.conf import settings
15 from django.contrib.auth.forms import AuthenticationForm
16 from django.contrib.auth import login
17 from django.utils import simplejson
12 18
13 from accounts.models import PendingUser 19 from accounts.models import PendingUser
14 from accounts.forms import RegisterForm 20 from accounts.forms import RegisterForm
15 from accounts import create_new_user 21 from accounts import create_new_user
16 from antispam.decorators import rate_limit 22 from antispam.decorators import rate_limit
76 82
77 return render_to_response('accounts/register_success.html', { 83 return render_to_response('accounts/register_success.html', {
78 'username': username, 84 'username': username,
79 }, 85 },
80 context_instance = RequestContext(request)) 86 context_instance = RequestContext(request))
87
88 #######################################################################
89
90 @rate_limit(count=10, interval=datetime.timedelta(minutes=1))
91 def login_ajax(request):
92 """
93 This view function handles a login via AJAX.
94
95 """
96 if not request.is_ajax():
97 return HttpResponseRedirect(reverse('accounts-login'))
98
99 response = {
100 'success': False,
101 'error': '',
102 'navbar_html': ''
103 }
104
105 if request.method == "POST":
106 form = AuthenticationForm(data=request.POST)
107 if form.is_valid():
108 login(request, form.get_user())
109 response['success'] = True
110 response['navbar_html'] = render_to_string('navbar.html',
111 {'user': request.user}, RequestContext(request))
112 else:
113 response['error'] = 'Invalid username or password'
114
115 return HttpResponse(simplejson.dumps(response),
116 content_type='application/json')