Mercurial > public > sg101
comparison weblinks/views.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/weblinks/views.py@75ea1a8be7f2 |
children | 6e6492468bb8 |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """ | |
2 Views for the weblinks application. | |
3 """ | |
4 | |
5 import datetime | |
6 import random | |
7 from django.shortcuts import render_to_response | |
8 from django.template import RequestContext | |
9 from django.core.paginator import InvalidPage | |
10 from django.http import HttpResponse | |
11 from django.http import HttpResponseBadRequest | |
12 from django.http import HttpResponseRedirect | |
13 from django.contrib.auth.decorators import login_required | |
14 from django.shortcuts import get_object_or_404 | |
15 from django.core.urlresolvers import reverse | |
16 from django.db.models import Q | |
17 from django.http import Http404 | |
18 from django.views.decorators.http import require_POST | |
19 | |
20 from core.paginator import DiggPaginator | |
21 from core.functions import email_admins | |
22 from core.functions import get_page | |
23 from weblinks.models import Category | |
24 from weblinks.models import Link | |
25 from weblinks.models import FlaggedLink | |
26 from weblinks.forms import AddLinkForm | |
27 | |
28 ####################################################################### | |
29 | |
30 LINKS_PER_PAGE = 10 | |
31 | |
32 def create_paginator(links): | |
33 return DiggPaginator(links, LINKS_PER_PAGE, body=5, tail=3, margin=3, padding=2) | |
34 | |
35 ####################################################################### | |
36 | |
37 def link_index(request): | |
38 categories = Category.objects.all() | |
39 total_links = Link.public_objects.all().count() | |
40 return render_to_response('weblinks/index.html', { | |
41 'categories': categories, | |
42 'total_links': total_links, | |
43 }, | |
44 context_instance = RequestContext(request)) | |
45 | |
46 ####################################################################### | |
47 | |
48 def new_links(request): | |
49 links = Link.public_objects.order_by('-date_added') | |
50 paginator = create_paginator(links) | |
51 page = get_page(request.GET) | |
52 try: | |
53 the_page = paginator.page(page) | |
54 except InvalidPage: | |
55 raise Http404 | |
56 | |
57 return render_to_response('weblinks/link_summary.html', { | |
58 'page': the_page, | |
59 'title': 'Newest Links', | |
60 }, | |
61 context_instance = RequestContext(request)) | |
62 | |
63 ####################################################################### | |
64 | |
65 def popular_links(request): | |
66 links = Link.public_objects.order_by('-hits') | |
67 paginator = create_paginator(links) | |
68 page = get_page(request.GET) | |
69 try: | |
70 the_page = paginator.page(page) | |
71 except InvalidPage: | |
72 raise Http404 | |
73 return render_to_response('weblinks/link_summary.html', { | |
74 'page': the_page, | |
75 'title': 'Popular Links', | |
76 }, | |
77 context_instance = RequestContext(request)) | |
78 | |
79 ####################################################################### | |
80 | |
81 @login_required | |
82 def add_link(request): | |
83 if request.method == 'POST': | |
84 add_form = AddLinkForm(request.POST) | |
85 if add_form.is_valid(): | |
86 new_link = add_form.save(commit=False) | |
87 new_link.user = request.user | |
88 new_link.save() | |
89 email_admins('New link for approval', """Hello, | |
90 | |
91 A user has added a new link for your approval. | |
92 """) | |
93 return HttpResponseRedirect(reverse('weblinks-add_thanks')) | |
94 else: | |
95 add_form = AddLinkForm() | |
96 | |
97 return render_to_response('weblinks/add_link.html', { | |
98 'add_form': add_form, | |
99 }, | |
100 context_instance = RequestContext(request)) | |
101 | |
102 ####################################################################### | |
103 | |
104 @login_required | |
105 def add_thanks(request): | |
106 return render_to_response('weblinks/add_link.html', { | |
107 }, | |
108 context_instance = RequestContext(request)) | |
109 | |
110 ####################################################################### | |
111 | |
112 # Maps URL component to database field name for the links table: | |
113 | |
114 LINK_FIELD_MAP = { | |
115 'title': 'title', | |
116 'date': '-date_added', | |
117 'hits': '-hits' | |
118 } | |
119 | |
120 def view_links(request, slug, sort='title'): | |
121 try: | |
122 cat = Category.objects.get(slug=slug) | |
123 except Category.DoesNotExist: | |
124 raise Http404 | |
125 | |
126 if sort in LINK_FIELD_MAP: | |
127 order_by = LINK_FIELD_MAP[sort] | |
128 else: | |
129 sort = 'title' | |
130 order_by = LINK_FIELD_MAP['title'] | |
131 | |
132 links = Link.public_objects.filter(category=cat).order_by(order_by) | |
133 paginator = create_paginator(links) | |
134 page = get_page(request.GET) | |
135 try: | |
136 the_page = paginator.page(page) | |
137 except InvalidPage: | |
138 raise Http404 | |
139 | |
140 return render_to_response('weblinks/view_links.html', { | |
141 's' : sort, | |
142 'category' : cat, | |
143 'page' : the_page, | |
144 }, | |
145 context_instance = RequestContext(request)) | |
146 | |
147 ####################################################################### | |
148 | |
149 def _visit_link(request, link): | |
150 link.hits += 1 | |
151 link.save() | |
152 return HttpResponseRedirect(link.url) | |
153 | |
154 ####################################################################### | |
155 | |
156 @require_POST | |
157 def visit(request, link_id): | |
158 link = get_object_or_404(Link, pk = link_id) | |
159 return _visit_link(request, link) | |
160 | |
161 ####################################################################### | |
162 | |
163 @require_POST | |
164 def random_link(request): | |
165 ids = Link.public_objects.values_list('id', flat=True) | |
166 if not ids: | |
167 raise Http404 | |
168 id = random.choice(ids) | |
169 random_link = Link.public_objects.get(pk=id) | |
170 return _visit_link(request, random_link) | |
171 | |
172 ####################################################################### | |
173 | |
174 @require_POST | |
175 def report_link(request, link_id): | |
176 """ | |
177 This function is the target of an AJAX POST to report a link as dead. | |
178 """ | |
179 if not request.user.is_authenticated(): | |
180 return HttpResponse('Please login or register to report a broken link.') | |
181 | |
182 try: | |
183 link = Link.objects.get(pk=link_id) | |
184 except Link.DoesNotExist: | |
185 return HttpResponseBadRequest("That link doesn't exist.") | |
186 | |
187 FlaggedLink.objects.create(link, request.user) | |
188 return HttpResponse("The link was reported. A moderator will review the " \ | |
189 "link shortly. Thanks for helping to improve the content on " \ | |
190 "this site.") | |
191 | |
192 ####################################################################### | |
193 | |
194 def link_detail(request, id): | |
195 link = get_object_or_404(Link, pk=id) | |
196 return render_to_response('weblinks/link_detail.html', { | |
197 'link': link, | |
198 }, | |
199 context_instance = RequestContext(request)) |