# HG changeset patch # User Brian Neal # Date 1322880498 0 # Node ID 886cc99e84060ec0b6c98e06b4c3d3f1caf0feb0 # Parent 1a09a7bea0005b0096bb1ee21745ee4aec28742a For #240, add an "ajaxy" login via a jQuery UI pop-up dialog to streamline the login process. diff -r 1a09a7bea000 -r 886cc99e8406 gpp/accounts/static/js/ajax_login.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/accounts/static/js/ajax_login.js Sat Dec 03 02:48:18 2011 +0000 @@ -0,0 +1,54 @@ +$(function() { + var loginError = $('#login-error'); + var loginDialog = $('#login-dialog').dialog({ + autoOpen: false, + height: 355, + width: 380, + modal: true, + buttons: { + "Login": function() { + loginError.text('').hide(); + $.ajax({ + url: '/accounts/login/ajax/', + type: 'POST', + data: { + username: $('#id_username').val(), + password: $('#id_password').val() + }, + dataType: 'json', + success: function(data, textStatus) { + if (data.success) { + loginDialog.dialog("close"); + if (window.location.pathname == "/accounts/logout/") { + window.location.replace("/"); + } + else { + $('#header-nav').html(data.navbar_html); + } + } + else { + loginError.text(data.error).show(); + } + }, + error: function (xhr, textStatus, ex) { + if (xhr.status == 403) { + loginDialog.dialog("close"); + alert("Oops, we are detecting some strange behavior and are blocking this action. If you feel this is an error, please feel free to contact us. Thank you."); + } + else { + loginError.text('Oops, an error occurred. If this problem persists, please contact us.').show(); + } + } + }); + }, + "Cancel": function() { + loginDialog.dialog("close"); + } + } + }); + $('#login-link').click(function() { + loginError.text('').hide(); + loginDialog.dialog("open"); + return false; + }); +}); diff -r 1a09a7bea000 -r 886cc99e8406 gpp/accounts/urls.py --- a/gpp/accounts/urls.py Wed Nov 30 02:41:18 2011 +0000 +++ b/gpp/accounts/urls.py Sat Dec 03 02:48:18 2011 +0000 @@ -3,6 +3,7 @@ from django.conf import settings urlpatterns = patterns('accounts.views', + url(r'^login/ajax/$', 'login_ajax', name='accounts-login_ajax'), url(r'^register/$', 'register', name='accounts-register'), (r'^register/thanks/$', 'register_thanks'), (r'^register/confirm/(?P[\w.@+-]{1,30})/(?P[a-zA-Z0-9]{20})/$', 'register_confirm'), diff -r 1a09a7bea000 -r 886cc99e8406 gpp/accounts/views.py --- a/gpp/accounts/views.py Wed Nov 30 02:41:18 2011 +0000 +++ b/gpp/accounts/views.py Sat Dec 03 02:48:18 2011 +0000 @@ -1,14 +1,20 @@ -"""views for the accounts application""" +""" +Views for the accounts application. +""" import datetime import logging from django.shortcuts import render_to_response from django.template import RequestContext +from django.template.loader import render_to_string from django.contrib.auth.models import User -from django.http import HttpResponseRedirect +from django.http import HttpResponse, HttpResponseRedirect from django.core.urlresolvers import reverse from django.conf import settings +from django.contrib.auth.forms import AuthenticationForm +from django.contrib.auth import login +from django.utils import simplejson from accounts.models import PendingUser from accounts.forms import RegisterForm @@ -78,3 +84,33 @@ 'username': username, }, context_instance = RequestContext(request)) + +####################################################################### + +@rate_limit(count=10, interval=datetime.timedelta(minutes=1)) +def login_ajax(request): + """ + This view function handles a login via AJAX. + + """ + if not request.is_ajax(): + return HttpResponseRedirect(reverse('accounts-login')) + + response = { + 'success': False, + 'error': '', + 'navbar_html': '' + } + + if request.method == "POST": + form = AuthenticationForm(data=request.POST) + if form.is_valid(): + login(request, form.get_user()) + response['success'] = True + response['navbar_html'] = render_to_string('navbar.html', + {'user': request.user}, RequestContext(request)) + else: + response['error'] = 'Invalid username or password' + + return HttpResponse(simplejson.dumps(response), + content_type='application/json') diff -r 1a09a7bea000 -r 886cc99e8406 gpp/templates/accounts/ajax_login_form.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/templates/accounts/ajax_login_form.html Sat Dec 03 02:48:18 2011 +0000 @@ -0,0 +1,17 @@ +{% load url from future %} +
+

+
+
+ +
+ + +
+
+
    +
  • Forgot your password? You can reset it here.
  • +
  • Don't have an account? Why don't you register?
  • +
  • Having problems? Please contact us.
  • +
+
diff -r 1a09a7bea000 -r 886cc99e8406 gpp/templates/base.html --- a/gpp/templates/base.html Wed Nov 30 02:41:18 2011 +0000 +++ b/gpp/templates/base.html Sat Dec 03 02:48:18 2011 +0000 @@ -9,7 +9,6 @@ {% load shoutbox_tags %} {% load irc_tags %} {% load potd_tags %} -{% load messages_tags %} {% load script_tags %} {% load poll_tags %} {% load core_tags %} @@ -35,6 +34,10 @@ +{% if not user.is_authenticated %} +{% script_tags "jquery-ui" %} + +{% endif %} {% block begin_body %}{% endblock %}
@@ -42,18 +45,7 @@

SurfGuitar101.com Logo

- + {% include "navbar.html" %}
@@ -120,6 +112,10 @@ music alive.

-{% block end_body %}{% endblock %} + +{% if not user.is_authenticated %} + {% include "accounts/ajax_login_form.html" %} +{% endif %} +{% block end_body %}{% endblock %} diff -r 1a09a7bea000 -r 886cc99e8406 gpp/templates/navbar.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/templates/navbar.html Sat Dec 03 02:48:18 2011 +0000 @@ -0,0 +1,14 @@ +{% load url from future %} +{% load messages_tags %} +