Mercurial > agora-palash
annotate views.py @ 139:b8e0bdc37e32
Hide snippets created by anonymous users
Created a custom manager for Snippet, with a public() method for easily
retrieving all the snippets that were created by registered users.
This change makes the code a bit neater, as it's no longer to necessary
to have to do the {% if snippet.author %} check every time.
author | dellsystem <ilostwaldo@gmail.com> |
---|---|
date | Sat, 29 Sep 2012 21:31:17 -0400 |
parents | 2bca07be6e51 |
children | c7be7def8b57 |
rev | line source |
---|---|
107 | 1 from django.contrib.auth import login, authenticate |
2 from django.contrib.auth.forms import AuthenticationForm | |
3 from django.shortcuts import render, redirect | |
4 from django.core.urlresolvers import reverse | |
5 from registration.forms import RegistrationForm | |
6 | |
7 from agora.apps.snippet.models import Snippet | |
8 | |
9 | |
10 def code(request): | |
11 context = { | |
139
b8e0bdc37e32
Hide snippets created by anonymous users
dellsystem <ilostwaldo@gmail.com>
parents:
107
diff
changeset
|
12 'snippets': Snippet.objects.public()[:5], |
107 | 13 'modules': None, # temp |
14 'forge': None, # temp | |
15 } | |
16 | |
17 return render(request, 'code.djhtml', context) | |
18 | |
19 | |
20 def login_register(request): | |
21 form = None | |
22 next_url = None | |
23 | |
24 if request.method == 'POST': | |
25 action = request.POST.get('action') | |
26 next_url = request.GET.get('next') or reverse('login') | |
27 | |
28 if action == 'login': | |
29 username = request.POST.get('username', '') | |
30 password = request.POST.get('password1', '') | |
31 | |
32 if username and password: | |
33 user = authenticate(username=username, password=password) | |
34 login(request, user) | |
35 | |
36 return redirect(next_url) | |
37 else: | |
38 form = { | |
39 'password1': { | |
40 'errors': 'Please enter a username and password.', | |
41 }, | |
42 } | |
43 elif action == 'register': | |
44 form = RegistrationForm(request.POST) | |
45 | |
46 if form.is_valid(): | |
47 user = form.save() | |
48 login(request, user) | |
49 return redirect(next_url) | |
50 else: | |
51 # The action is not set. Malicious submission? | |
52 pass | |
53 | |
54 context = { | |
55 'next_url': next_url, | |
56 'form': form, | |
57 } | |
58 | |
59 return render(request, 'login.djhtml', context) |