Mercurial > public > sg101
comparison weblinks/views.py @ 675:6e6492468bb8
Add admin action to acknowledge flagged links.
Fix bug where weblinks js wasn't being included on detail page.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 03 Aug 2013 22:12:03 -0500 |
parents | ee87ea74d46b |
children | 41d0389fc85a |
comparison
equal
deleted
inserted
replaced
673:92101013d5ac | 675:6e6492468bb8 |
---|---|
1 """ | 1 """ |
2 Views for the weblinks application. | 2 Views for the weblinks application. |
3 | |
3 """ | 4 """ |
4 | |
5 import datetime | |
6 import random | 5 import random |
7 from django.shortcuts import render_to_response | 6 from django.shortcuts import render_to_response |
8 from django.template import RequestContext | 7 from django.template import RequestContext |
9 from django.core.paginator import InvalidPage | 8 from django.core.paginator import InvalidPage |
10 from django.http import HttpResponse | 9 from django.http import HttpResponse |
11 from django.http import HttpResponseBadRequest | 10 from django.http import HttpResponseBadRequest |
12 from django.http import HttpResponseRedirect | 11 from django.http import HttpResponseRedirect |
13 from django.contrib.auth.decorators import login_required | 12 from django.contrib.auth.decorators import login_required |
14 from django.shortcuts import get_object_or_404 | 13 from django.shortcuts import get_object_or_404 |
15 from django.core.urlresolvers import reverse | 14 from django.core.urlresolvers import reverse |
16 from django.db.models import Q | |
17 from django.http import Http404 | 15 from django.http import Http404 |
18 from django.views.decorators.http import require_POST | 16 from django.views.decorators.http import require_POST |
19 | 17 |
20 from core.paginator import DiggPaginator | 18 from core.paginator import DiggPaginator |
21 from core.functions import email_admins | 19 from core.functions import email_admins |
36 | 34 |
37 def link_index(request): | 35 def link_index(request): |
38 categories = Category.objects.all() | 36 categories = Category.objects.all() |
39 total_links = Link.public_objects.all().count() | 37 total_links = Link.public_objects.all().count() |
40 return render_to_response('weblinks/index.html', { | 38 return render_to_response('weblinks/index.html', { |
41 'categories': categories, | 39 'categories': categories, |
42 'total_links': total_links, | 40 'total_links': total_links, |
43 }, | 41 }, |
44 context_instance = RequestContext(request)) | 42 context_instance = RequestContext(request)) |
45 | 43 |
46 ####################################################################### | 44 ####################################################################### |
53 the_page = paginator.page(page) | 51 the_page = paginator.page(page) |
54 except InvalidPage: | 52 except InvalidPage: |
55 raise Http404 | 53 raise Http404 |
56 | 54 |
57 return render_to_response('weblinks/link_summary.html', { | 55 return render_to_response('weblinks/link_summary.html', { |
58 'page': the_page, | 56 'page': the_page, |
59 'title': 'Newest Links', | 57 'title': 'Newest Links', |
60 }, | 58 }, |
61 context_instance = RequestContext(request)) | 59 context_instance = RequestContext(request)) |
62 | 60 |
63 ####################################################################### | 61 ####################################################################### |
69 try: | 67 try: |
70 the_page = paginator.page(page) | 68 the_page = paginator.page(page) |
71 except InvalidPage: | 69 except InvalidPage: |
72 raise Http404 | 70 raise Http404 |
73 return render_to_response('weblinks/link_summary.html', { | 71 return render_to_response('weblinks/link_summary.html', { |
74 'page': the_page, | 72 'page': the_page, |
75 'title': 'Popular Links', | 73 'title': 'Popular Links', |
76 }, | 74 }, |
77 context_instance = RequestContext(request)) | 75 context_instance = RequestContext(request)) |
78 | 76 |
79 ####################################################################### | 77 ####################################################################### |
110 ####################################################################### | 108 ####################################################################### |
111 | 109 |
112 # Maps URL component to database field name for the links table: | 110 # Maps URL component to database field name for the links table: |
113 | 111 |
114 LINK_FIELD_MAP = { | 112 LINK_FIELD_MAP = { |
115 'title': 'title', | 113 'title': 'title', |
116 'date': '-date_added', | 114 'date': '-date_added', |
117 'hits': '-hits' | 115 'hits': '-hits' |
118 } | 116 } |
119 | 117 |
120 def view_links(request, slug, sort='title'): | 118 def view_links(request, slug, sort='title'): |
138 raise Http404 | 136 raise Http404 |
139 | 137 |
140 return render_to_response('weblinks/view_links.html', { | 138 return render_to_response('weblinks/view_links.html', { |
141 's' : sort, | 139 's' : sort, |
142 'category' : cat, | 140 'category' : cat, |
143 'page' : the_page, | 141 'page' : the_page, |
144 }, | 142 }, |
145 context_instance = RequestContext(request)) | 143 context_instance = RequestContext(request)) |
146 | 144 |
147 ####################################################################### | 145 ####################################################################### |
148 | 146 |
149 def _visit_link(request, link): | 147 def _visit_link(request, link): |
183 link = Link.objects.get(pk=link_id) | 181 link = Link.objects.get(pk=link_id) |
184 except Link.DoesNotExist: | 182 except Link.DoesNotExist: |
185 return HttpResponseBadRequest("That link doesn't exist.") | 183 return HttpResponseBadRequest("That link doesn't exist.") |
186 | 184 |
187 FlaggedLink.objects.create(link, request.user) | 185 FlaggedLink.objects.create(link, request.user) |
188 return HttpResponse("The link was reported. A moderator will review the " \ | 186 return HttpResponse("The link was reported. A moderator will review the " |
189 "link shortly. Thanks for helping to improve the content on " \ | 187 "link shortly. Thanks for helping to improve the content on " |
190 "this site.") | 188 "this site.") |
191 | 189 |
192 ####################################################################### | 190 ####################################################################### |
193 | 191 |
194 def link_detail(request, id): | 192 def link_detail(request, id): |
195 link = get_object_or_404(Link, pk=id) | 193 link = get_object_or_404(Link, pk=id) |
196 return render_to_response('weblinks/link_detail.html', { | 194 return render_to_response('weblinks/link_detail.html', { |
197 'link': link, | 195 'link': link, |
198 }, | 196 }, |
199 context_instance = RequestContext(request)) | 197 context_instance = RequestContext(request)) |