Mercurial > agora
annotate views.py @ 160:a5547f079190
Fix bug in registration
django-registration's RegistrationForm isn't a ModelForm, it's just a
Form, so there's no save method. Have to create the account manually.
author | dellsystem <ilostwaldo@gmail.com> |
---|---|
date | Mon, 15 Oct 2012 02:24:01 -0400 |
parents | c7be7def8b57 |
children | c494e6ecff58 |
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) | |
36 login(request, user) | |
37 | |
38 return redirect(next_url) | |
39 else: | |
40 form = { | |
41 'password1': { | |
42 'errors': 'Please enter a username and password.', | |
43 }, | |
44 } | |
45 elif action == 'register': | |
46 form = RegistrationForm(request.POST) | |
47 | |
48 if form.is_valid(): | |
160 | 49 username = form.cleaned_data['username'] |
50 email = form.cleaned_data['email'] | |
51 password = form.cleaned_data['password1'] | |
52 | |
53 User.objects.create_user(username, email, password) | |
54 user = authenticate(username=username, password=password) | |
107 | 55 login(request, user) |
56 return redirect(next_url) | |
57 else: | |
58 # The action is not set. Malicious submission? | |
59 pass | |
60 | |
61 context = { | |
62 'next_url': next_url, | |
63 'form': form, | |
64 } | |
65 | |
66 return render(request, 'login.djhtml', context) |