Mercurial > agora
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 |
rev | line source |
---|---|
107 | 1 from django.contrib.auth import login, authenticate |
160 | 2 from django.contrib.auth.models import User |
107 | 3 from django.contrib.auth.forms import AuthenticationForm |
4 from django.shortcuts import render, redirect | |
5 from django.core.urlresolvers import reverse | |
6 from registration.forms import RegistrationForm | |
7 | |
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 | 10 |
11 | |
12 def code(request): | |
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 | 16 'forge': None, # temp |
17 } | |
18 | |
19 return render(request, 'code.djhtml', context) | |
20 | |
21 | |
22 def login_register(request): | |
23 form = None | |
24 next_url = None | |
25 | |
26 if request.method == 'POST': | |
27 action = request.POST.get('action') | |
28 next_url = request.GET.get('next') or reverse('login') | |
29 | |
30 if action == 'login': | |
31 username = request.POST.get('username', '') | |
32 password = request.POST.get('password1', '') | |
33 | |
34 if username and password: | |
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 | 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 | 47 else: |
48 form = { | |
49 'password1': { | |
50 'errors': 'Please enter a username and password.', | |
51 }, | |
52 } | |
53 elif action == 'register': | |
54 form = RegistrationForm(request.POST) | |
55 | |
56 if form.is_valid(): | |
160 | 57 username = form.cleaned_data['username'] |
58 email = form.cleaned_data['email'] | |
59 password = form.cleaned_data['password1'] | |
60 | |
61 User.objects.create_user(username, email, password) | |
62 user = authenticate(username=username, password=password) | |
107 | 63 login(request, user) |
64 return redirect(next_url) | |
65 else: | |
66 # The action is not set. Malicious submission? | |
67 pass | |
68 | |
69 context = { | |
70 'next_url': next_url, | |
71 'form': form, | |
72 } | |
73 | |
74 return render(request, 'login.djhtml', context) |