changeset 692:4a49d4ac319f

For #51, remove the ajax popup login window.
author Brian Neal <bgneal@gmail.com>
date Sun, 01 Sep 2013 15:58:45 -0500
parents 81e0be69b3a5
children ad69236e8501
files accounts/static/js/ajax_login.js accounts/urls.py accounts/views.py antispam/decorators.py sg101/templates/accounts/ajax_login_form.html sg101/templates/base.html sg101/templates/navbar.html
diffstat 7 files changed, 12 insertions(+), 155 deletions(-) [+]
line wrap: on
line diff
--- a/accounts/static/js/ajax_login.js	Sun Sep 01 11:59:28 2013 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,82 +0,0 @@
-$(function() {
-   var loginError = $('#login-error');
-   var userBox = $('#ajax-login-username');
-   var passBox = $('#ajax-login-password');
-   var loginDialog = $('#login-dialog').dialog({
-      autoOpen: false,
-      height: 375,
-      width: 380,
-      modal: true,
-      buttons: [
-         {
-            id: "login-button",
-            text: "Login",
-            click: function() {
-               loginError.text('').hide();
-               $("#login-button").button("disable");
-               $.ajax({
-                  url: '/accounts/login/ajax/',
-                  type: 'POST',
-                  data: {
-                     username: userBox.val(),
-                     password: passBox.val(),
-                     csrfmiddlewaretoken: csrf_token
-                  },
-                  dataType: 'json',
-                  success: function(data, textStatus) {
-                     $("#login-button").button("enable");
-                     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();
-                        userBox.val('');
-                        passBox.val('');
-                        userBox.focus();
-                     }
-                  },
-                  error: function (xhr, textStatus, ex) {
-                     $("#login-button").button("enable");
-                     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.");
-                        window.location.href = "/";
-                     }
-                     else {
-                        loginError.text('Oops, an error occurred. If this problem persists, please contact us.').show();
-                     }
-                  }
-               });
-            }
-         },
-         {
-            id: "cancel-button",
-            text: "Cancel",
-            click: function() {
-               loginDialog.dialog("close");
-            }
-         }
-      ],
-      focus: function() {
-         $(':input', this).keyup(function(event) {
-            if (event.keyCode == 13) {
-                var loginButton = $("#login-button");
-                if (!loginButton.button("option", "disabled")) {
-                   loginButton.click();
-                }
-            }
-         });
-      }
-   });
-   $('#login-link').click(function() {
-      loginError.text('').hide();
-      loginDialog.dialog("open");
-      return false;
-   });
-});
--- a/accounts/urls.py	Sun Sep 01 11:59:28 2013 -0500
+++ b/accounts/urls.py	Sun Sep 01 15:58:45 2013 -0500
@@ -4,7 +4,6 @@
 from django.views.generic import TemplateView
 
 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<username>[\w.@+-]{1,30})/(?P<key>[a-zA-Z0-9]{20})/$', 'register_confirm'),
--- a/accounts/views.py	Sun Sep 01 11:59:28 2013 -0500
+++ b/accounts/views.py	Sun Sep 01 15:58:45 2013 -0500
@@ -2,17 +2,12 @@
 Views for the accounts application.
 
 """
-import json
 import logging
 
 from django.shortcuts import render
-from django.template import RequestContext
-from django.template.loader import render_to_string
-from django.http import HttpResponse, HttpResponseRedirect
+from django.http import 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 accounts.models import PendingUser
 from accounts.forms import RegisterForm, ForgotUsernameForm
@@ -80,35 +75,6 @@
 
 #######################################################################
 
-@log_auth_failures('Login')
-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(json.dumps(response), content_type='application/json')
-
-#######################################################################
-
 def username_query(request):
     """This view handles forgotten username queries."""
     if request.user.is_authenticated():
--- a/antispam/decorators.py	Sun Sep 01 11:59:28 2013 -0500
+++ b/antispam/decorators.py	Sun Sep 01 15:58:45 2013 -0500
@@ -2,7 +2,6 @@
 This module contains decorators for the antispam application.
 
 """
-import json
 from functools import wraps
 import logging
 
@@ -19,16 +18,10 @@
 
             if request.method == 'POST':
 
-                # Figure out if the view succeeded; if it is a non-ajax view,
-                # then success means a redirect is about to occur. If it is
-                # an ajax view, we have to decode the json response.
-                success = False
-                if not request.is_ajax():
-                    success = (response and response.has_header('location') and
-                            response.status_code == 302)
-                elif response:
-                    json_resp = json.loads(response.content)
-                    success = json_resp['success']
+                # Figure out if the view succeeded; success means a redirect is
+                # about to occur.
+                success = (response and response.has_header('location') and
+                        response.status_code == 302)
 
                 if not success:
                     username = request.POST.get('username')
--- a/sg101/templates/accounts/ajax_login_form.html	Sun Sep 01 11:59:28 2013 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,17 +0,0 @@
-<div id="login-dialog" title="SurfGuitar101 Login">
-   <p id="login-error" class="error"></p>
-   <form>
-      <fieldset>
-         <label for="ajax-login-username">Username:</label>
-         <input id="ajax-login-username" type="text" name="username" maxlength="30" class="text" /><br />
-         <label for="ajax-login-password">Password:</label>
-         <input type="password" name="password" id="ajax-login-password" class="text" />
-      </fieldset>
-   </form>
-<ul>
-<li>Forgot your username? You can recover it <a href="{% url 'accounts-username_query' %}">here</a>.</li>
-<li>Forgot your password? You can reset it <a href="{% url 'accounts-password_reset' %}">here</a>.</li>
-<li>Don't have an account? Why don't you <a href="{% url 'accounts-register' %}">register</a>?</li>
-<li>Having problems? Please <a href="{% url 'contact-form' %}">contact us</a>.</li>
-</ul>
-</div>
--- a/sg101/templates/base.html	Sun Sep 01 11:59:28 2013 -0500
+++ b/sg101/templates/base.html	Sun Sep 01 15:58:45 2013 -0500
@@ -34,11 +34,6 @@
 <script type="text/javascript" src="{{ STATIC_URL }}js/jquery.cycle.all.min.js"></script>
 <script type="text/javascript" src="{{ STATIC_URL }}js/shoutbox.js"></script>
 <link rel="shortcut icon" type="image/vnd.microsoft.com" href="{{ STATIC_URL }}favicon.ico" />
-{% if not user.is_authenticated %}
-{% script_tags "jquery-ui" %}
-<script type="text/javascript">var csrf_token = "{{ csrf_token }}";</script>
-<script type="text/javascript" src="{{ STATIC_URL }}js/ajax_login.js"></script>
-{% endif %}
 </head>
 <body>{% block begin_body %}{% endblock %}
 <div id="page" class="container">
@@ -122,9 +117,6 @@
 </div>
 
 </div>
-{% if not user.is_authenticated %}
-   {% include "accounts/ajax_login_form.html" %}
-{% endif %}
 {% block end_body %}{% endblock %}
 </body>
 </html>
--- a/sg101/templates/navbar.html	Sun Sep 01 11:59:28 2013 -0500
+++ b/sg101/templates/navbar.html	Sun Sep 01 15:58:45 2013 -0500
@@ -7,7 +7,13 @@
    <li><a href="{% url 'haystack_search' %}">Search</a></li>
    <li><a href="{% url 'accounts-logout' %}">Logout</a></li>
    {% else %}
-   <li><a id="login-link" href="{% url 'accounts-login' %}">Login</a></li>
+   <li>
+   {% if request.path != "/accounts/logout/" %}
+      <a id="login-link" href="{% url 'accounts-login' %}?next={{ request.path }}">Login</a>
+   {% else %}
+      <a id="login-link" href="{% url 'accounts-login' %}">Login</a>
+   {% endif %}
+   </li>
    <li><a href="{% url 'accounts-register' %}">Register</a></li>
    {% endif %}
 </ul>