Mercurial > public > sg101
comparison accounts/views.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/accounts/views.py@2c2df8545112 |
children | 8e6b8ffe5f34 |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """ | |
2 Views for the accounts application. | |
3 | |
4 """ | |
5 import datetime | |
6 import logging | |
7 | |
8 from django.shortcuts import render_to_response | |
9 from django.template import RequestContext | |
10 from django.template.loader import render_to_string | |
11 from django.contrib.auth.models import User | |
12 from django.http import HttpResponse, HttpResponseRedirect | |
13 from django.core.urlresolvers import reverse | |
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 | |
18 | |
19 from accounts.models import PendingUser | |
20 from accounts.forms import RegisterForm | |
21 from accounts import create_new_user | |
22 from antispam.decorators import rate_limit | |
23 | |
24 | |
25 ####################################################################### | |
26 | |
27 @rate_limit(count=10, interval=datetime.timedelta(minutes=1)) | |
28 def register(request): | |
29 if request.user.is_authenticated(): | |
30 return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL) | |
31 | |
32 if request.method == 'POST': | |
33 form = RegisterForm(request.POST, ip=request.META.get('REMOTE_ADDR', '?')) | |
34 if form.is_valid(): | |
35 form.save() | |
36 return HttpResponseRedirect(reverse('accounts.views.register_thanks')) | |
37 else: | |
38 form = RegisterForm() | |
39 | |
40 return render_to_response('accounts/register.html', { | |
41 'form': form, | |
42 }, | |
43 context_instance = RequestContext(request)) | |
44 | |
45 ####################################################################### | |
46 | |
47 def register_thanks(request): | |
48 if request.user.is_authenticated(): | |
49 return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL) | |
50 | |
51 return render_to_response('accounts/register_thanks.html', | |
52 context_instance = RequestContext(request)) | |
53 | |
54 ####################################################################### | |
55 | |
56 def register_confirm(request, username, key): | |
57 if request.user.is_authenticated(): | |
58 return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL) | |
59 | |
60 # purge expired users | |
61 | |
62 PendingUser.objects.purge_expired() | |
63 | |
64 ip = request.META.get('REMOTE_ADDR', '?') | |
65 try: | |
66 pending_user = PendingUser.objects.get(username = username) | |
67 except PendingUser.DoesNotExist: | |
68 logging.error('Accounts register_confirm [%s]: user does not exist: %s', ip, username) | |
69 return render_to_response('accounts/register_failure.html', { | |
70 'username': username, | |
71 }, | |
72 context_instance = RequestContext(request)) | |
73 | |
74 if pending_user.key != key: | |
75 logging.error('Accounts register_confirm [%s]: key error: %s', ip, username) | |
76 return render_to_response('accounts/register_failure.html', { | |
77 'username': username, | |
78 }, | |
79 context_instance = RequestContext(request)) | |
80 | |
81 create_new_user(pending_user, ip) | |
82 | |
83 return render_to_response('accounts/register_success.html', { | |
84 'username': username, | |
85 }, | |
86 context_instance = RequestContext(request)) | |
87 | |
88 ####################################################################### | |
89 | |
90 @rate_limit(count=10, interval=datetime.timedelta(minutes=1), | |
91 lockout=datetime.timedelta(minutes=2)) | |
92 def login_ajax(request): | |
93 """ | |
94 This view function handles a login via AJAX. | |
95 | |
96 """ | |
97 if not request.is_ajax(): | |
98 return HttpResponseRedirect(reverse('accounts-login')) | |
99 | |
100 response = { | |
101 'success': False, | |
102 'error': '', | |
103 'navbar_html': '' | |
104 } | |
105 | |
106 if request.method == "POST": | |
107 form = AuthenticationForm(data=request.POST) | |
108 if form.is_valid(): | |
109 login(request, form.get_user()) | |
110 response['success'] = True | |
111 response['navbar_html'] = render_to_string('navbar.html', | |
112 {'user': request.user}, RequestContext(request)) | |
113 else: | |
114 response['error'] = 'Invalid username or password' | |
115 | |
116 return HttpResponse(simplejson.dumps(response), | |
117 content_type='application/json') |