annotate views.py @ 168:c494e6ecff58

Fix bug in logging in with incorrect credentials
author dellsystem <ilostwaldo@gmail.com>
date Sat, 20 Oct 2012 00:16:38 -0400
parents a5547f079190
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
107
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
1 from django.contrib.auth import login, authenticate
160
a5547f079190 Fix bug in registration
dellsystem <ilostwaldo@gmail.com>
parents: 151
diff changeset
2 from django.contrib.auth.models import User
107
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
3 from django.contrib.auth.forms import AuthenticationForm
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
4 from django.shortcuts import render, redirect
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
5 from django.core.urlresolvers import reverse
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
6 from registration.forms import RegistrationForm
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
7
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
8 from agora.apps.snippet.models import Snippet
151
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 139
diff changeset
9 from agora.apps.bundle.models import Bundle
107
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
10
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
11
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
12 def code(request):
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
13 context = {
139
b8e0bdc37e32 Hide snippets created by anonymous users
dellsystem <ilostwaldo@gmail.com>
parents: 107
diff changeset
14 'snippets': Snippet.objects.public()[:5],
151
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 139
diff changeset
15 'bundles': Bundle.objects.all()[:5],
107
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
16 'forge': None, # temp
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
17 }
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
18
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
19 return render(request, 'code.djhtml', context)
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
20
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
21
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
22 def login_register(request):
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
23 form = None
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
24 next_url = None
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
25
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
26 if request.method == 'POST':
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
27 action = request.POST.get('action')
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
28 next_url = request.GET.get('next') or reverse('login')
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
29
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
30 if action == 'login':
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
31 username = request.POST.get('username', '')
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
32 password = request.POST.get('password1', '')
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
33
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
34 if username and password:
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
35 user = authenticate(username=username, password=password)
168
c494e6ecff58 Fix bug in logging in with incorrect credentials
dellsystem <ilostwaldo@gmail.com>
parents: 160
diff changeset
36
c494e6ecff58 Fix bug in logging in with incorrect credentials
dellsystem <ilostwaldo@gmail.com>
parents: 160
diff changeset
37 if user is not None:
c494e6ecff58 Fix bug in logging in with incorrect credentials
dellsystem <ilostwaldo@gmail.com>
parents: 160
diff changeset
38 login(request, user)
c494e6ecff58 Fix bug in logging in with incorrect credentials
dellsystem <ilostwaldo@gmail.com>
parents: 160
diff changeset
39 return redirect(next_url)
107
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
40
168
c494e6ecff58 Fix bug in logging in with incorrect credentials
dellsystem <ilostwaldo@gmail.com>
parents: 160
diff changeset
41 # Could not authenticate
c494e6ecff58 Fix bug in logging in with incorrect credentials
dellsystem <ilostwaldo@gmail.com>
parents: 160
diff changeset
42 form = {
c494e6ecff58 Fix bug in logging in with incorrect credentials
dellsystem <ilostwaldo@gmail.com>
parents: 160
diff changeset
43 'username': {
c494e6ecff58 Fix bug in logging in with incorrect credentials
dellsystem <ilostwaldo@gmail.com>
parents: 160
diff changeset
44 'errors': 'Your username and password did not match.',
c494e6ecff58 Fix bug in logging in with incorrect credentials
dellsystem <ilostwaldo@gmail.com>
parents: 160
diff changeset
45 }
c494e6ecff58 Fix bug in logging in with incorrect credentials
dellsystem <ilostwaldo@gmail.com>
parents: 160
diff changeset
46 }
107
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
47 else:
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
48 form = {
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
49 'password1': {
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
50 'errors': 'Please enter a username and password.',
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
51 },
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
52 }
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
53 elif action == 'register':
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
54 form = RegistrationForm(request.POST)
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
55
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
56 if form.is_valid():
160
a5547f079190 Fix bug in registration
dellsystem <ilostwaldo@gmail.com>
parents: 151
diff changeset
57 username = form.cleaned_data['username']
a5547f079190 Fix bug in registration
dellsystem <ilostwaldo@gmail.com>
parents: 151
diff changeset
58 email = form.cleaned_data['email']
a5547f079190 Fix bug in registration
dellsystem <ilostwaldo@gmail.com>
parents: 151
diff changeset
59 password = form.cleaned_data['password1']
a5547f079190 Fix bug in registration
dellsystem <ilostwaldo@gmail.com>
parents: 151
diff changeset
60
a5547f079190 Fix bug in registration
dellsystem <ilostwaldo@gmail.com>
parents: 151
diff changeset
61 User.objects.create_user(username, email, password)
a5547f079190 Fix bug in registration
dellsystem <ilostwaldo@gmail.com>
parents: 151
diff changeset
62 user = authenticate(username=username, password=password)
107
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
63 login(request, user)
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
64 return redirect(next_url)
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
65 else:
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
66 # The action is not set. Malicious submission?
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
67 pass
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
68
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
69 context = {
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
70 'next_url': next_url,
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
71 'form': form,
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
72 }
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
73
2bca07be6e51 Add login popup box
dellsystem <ilostwaldo@gmail.com>
parents:
diff changeset
74 return render(request, 'login.djhtml', context)