Mercurial > public > sg101
comparison gpp/weblinks/views.py @ 165:952e05cb3d80
Implement #49; use POST for updating link hit counts. Also refactored a bit and use javascript to report broken links.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 03 Jan 2010 04:15:14 +0000 |
parents | 62eb9cbbcffc |
children | 046e6ef0ff45 |
comparison
equal
deleted
inserted
replaced
164:f7a6b8fe4556 | 165:952e05cb3d80 |
---|---|
6 import random | 6 import random |
7 from django.shortcuts import render_to_response | 7 from django.shortcuts import render_to_response |
8 from django.template import RequestContext | 8 from django.template import RequestContext |
9 from django.contrib import auth | 9 from django.contrib import auth |
10 from django.core.paginator import InvalidPage | 10 from django.core.paginator import InvalidPage |
11 from django.http import HttpResponse | |
12 from django.http import HttpResponseBadRequest | |
11 from django.http import HttpResponseRedirect | 13 from django.http import HttpResponseRedirect |
12 from django.contrib.auth.decorators import login_required | 14 from django.contrib.auth.decorators import login_required |
13 from django.shortcuts import get_object_or_404 | 15 from django.shortcuts import get_object_or_404 |
14 from django.core.urlresolvers import reverse | 16 from django.core.urlresolvers import reverse |
15 from django.db.models import Q | 17 from django.db.models import Q |
16 from django.http import Http404 | 18 from django.http import Http404 |
19 from django.views.decorators.http import require_POST | |
17 | 20 |
18 from core.paginator import DiggPaginator | 21 from core.paginator import DiggPaginator |
19 from core.functions import email_admins | 22 from core.functions import email_admins |
20 from weblinks.models import Category | 23 from weblinks.models import Category |
21 from weblinks.models import Link | 24 from weblinks.models import Link |
152 random_link = Link.public_objects.get(pk=id) | 155 random_link = Link.public_objects.get(pk=id) |
153 return _visit_link(request, random_link) | 156 return _visit_link(request, random_link) |
154 | 157 |
155 ####################################################################### | 158 ####################################################################### |
156 | 159 |
157 @login_required | 160 @require_POST |
158 def report_link(request, link_id): | 161 def report_link(request, link_id): |
159 link = get_object_or_404(Link, pk = link_id) | 162 """ |
160 if request.method == "POST": | 163 This function is the target of an AJAX POST to report a link as dead. |
161 FlaggedLink.objects.create(link, request.user) | 164 """ |
162 email_admins('A Link Has Been Flagged as Broken', """Hello, | 165 if not request.user.is_authenticated(): |
163 | 166 return HttpResponse('Please login or register to report a broken link.') |
164 A user has flagged a link as broken. | 167 |
165 """) | 168 try: |
166 return HttpResponseRedirect(reverse('weblinks.views.report_thanks', args = (link_id, ))) | 169 link = Link.objects.get(pk=link_id) |
167 | 170 except Link.DoesNotExist: |
168 return render_to_response('weblinks/report_link.html', { | 171 return HttpResponseBadRequest("That link doesn't exist.") |
169 'link': link, | 172 |
170 'report_thanks': False, | 173 FlaggedLink.objects.create(link, request.user) |
171 }, | 174 return HttpResponse("The link was reported. A moderator will review the " \ |
172 context_instance = RequestContext(request)) | 175 "link shortly. Thanks for helping to improve the content on " \ |
173 | 176 "this site.") |
174 | |
175 ####################################################################### | |
176 | |
177 @login_required | |
178 def report_thanks(request, link_id): | |
179 link = get_object_or_404(Link, pk = link_id) | |
180 return render_to_response('weblinks/report_link.html', { | |
181 'link': link, | |
182 'report_thanks': True, | |
183 }, | |
184 context_instance = RequestContext(request)) | |
185 | 177 |
186 ####################################################################### | 178 ####################################################################### |
187 | 179 |
188 def search_links(request, page=1): | 180 def search_links(request, page=1): |
189 if request.method == 'POST': | 181 if request.method == 'POST': |