Mercurial > public > sg101
comparison accounts/views.py @ 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 | 004b3a90de66 |
comparison
equal
deleted
inserted
replaced
691:81e0be69b3a5 | 692:4a49d4ac319f |
---|---|
1 """ | 1 """ |
2 Views for the accounts application. | 2 Views for the accounts application. |
3 | 3 |
4 """ | 4 """ |
5 import json | |
6 import logging | 5 import logging |
7 | 6 |
8 from django.shortcuts import render | 7 from django.shortcuts import render |
9 from django.template import RequestContext | 8 from django.http import HttpResponseRedirect |
10 from django.template.loader import render_to_string | |
11 from django.http import HttpResponse, HttpResponseRedirect | |
12 from django.core.urlresolvers import reverse | 9 from django.core.urlresolvers import reverse |
13 from django.conf import settings | 10 from django.conf import settings |
14 from django.contrib.auth.forms import AuthenticationForm | |
15 from django.contrib.auth import login | |
16 | 11 |
17 from accounts.models import PendingUser | 12 from accounts.models import PendingUser |
18 from accounts.forms import RegisterForm, ForgotUsernameForm | 13 from accounts.forms import RegisterForm, ForgotUsernameForm |
19 from accounts import create_new_user | 14 from accounts import create_new_user |
20 from antispam.decorators import log_auth_failures | 15 from antispam.decorators import log_auth_failures |
78 'accounts/register_success.html', | 73 'accounts/register_success.html', |
79 {'username': username}) | 74 {'username': username}) |
80 | 75 |
81 ####################################################################### | 76 ####################################################################### |
82 | 77 |
83 @log_auth_failures('Login') | |
84 def login_ajax(request): | |
85 """ | |
86 This view function handles a login via AJAX. | |
87 | |
88 """ | |
89 if not request.is_ajax(): | |
90 return HttpResponseRedirect(reverse('accounts-login')) | |
91 | |
92 response = { | |
93 'success': False, | |
94 'error': '', | |
95 'navbar_html': '' | |
96 } | |
97 | |
98 if request.method == "POST": | |
99 form = AuthenticationForm(data=request.POST) | |
100 if form.is_valid(): | |
101 login(request, form.get_user()) | |
102 response['success'] = True | |
103 response['navbar_html'] = render_to_string('navbar.html', | |
104 {'user': request.user}, RequestContext(request)) | |
105 else: | |
106 response['error'] = 'Invalid username or password' | |
107 | |
108 return HttpResponse(json.dumps(response), content_type='application/json') | |
109 | |
110 ####################################################################### | |
111 | |
112 def username_query(request): | 78 def username_query(request): |
113 """This view handles forgotten username queries.""" | 79 """This view handles forgotten username queries.""" |
114 if request.user.is_authenticated(): | 80 if request.user.is_authenticated(): |
115 return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL) | 81 return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL) |
116 | 82 |