gremmie@1
|
1 """
|
gremmie@1
|
2 Views for the weblinks application.
|
gremmie@1
|
3 """
|
gremmie@1
|
4
|
gremmie@1
|
5 import datetime
|
gremmie@1
|
6 import random
|
gremmie@1
|
7 from django.shortcuts import render_to_response
|
gremmie@1
|
8 from django.template import RequestContext
|
gremmie@1
|
9 from django.contrib import auth
|
gremmie@1
|
10 from django.http import HttpResponseRedirect
|
gremmie@1
|
11 from django.contrib.auth.decorators import login_required
|
gremmie@1
|
12 from django.shortcuts import get_object_or_404
|
gremmie@1
|
13 from django.core.urlresolvers import reverse
|
gremmie@1
|
14 from django.db.models import Q
|
gremmie@1
|
15 from django.http import Http404
|
gremmie@1
|
16
|
gremmie@1
|
17 from core.paginator import DiggPaginator
|
gremmie@1
|
18 from core.functions import email_admins
|
gremmie@1
|
19 from weblinks.models import Category
|
gremmie@1
|
20 from weblinks.models import Link
|
gremmie@1
|
21 from weblinks.models import FlaggedLink
|
gremmie@1
|
22 from weblinks.forms import SearchForm
|
gremmie@1
|
23 from weblinks.forms import AddLinkForm
|
gremmie@1
|
24
|
gremmie@1
|
25 #######################################################################
|
gremmie@1
|
26
|
gremmie@1
|
27 LINKS_PER_PAGE = 10
|
gremmie@1
|
28
|
gremmie@1
|
29 def create_paginator(links):
|
gremmie@1
|
30 return DiggPaginator(links, LINKS_PER_PAGE, body=5, tail=3, margin=3, padding=2)
|
gremmie@1
|
31
|
gremmie@1
|
32 #######################################################################
|
gremmie@1
|
33
|
gremmie@1
|
34 def link_index(request):
|
gremmie@1
|
35 categories = Category.objects.all()
|
gremmie@1
|
36 total_links = Link.public_objects.all().count()
|
gremmie@1
|
37 form = SearchForm()
|
gremmie@1
|
38 return render_to_response('weblinks/index.html', {
|
gremmie@1
|
39 'categories': categories,
|
gremmie@1
|
40 'total_links': total_links,
|
gremmie@1
|
41 'search_form': form,
|
gremmie@1
|
42 },
|
gremmie@1
|
43 context_instance = RequestContext(request))
|
gremmie@1
|
44
|
gremmie@1
|
45 #######################################################################
|
gremmie@1
|
46
|
gremmie@1
|
47 def new_links(request):
|
gremmie@1
|
48 links = Link.public_objects.order_by('-date_added')[:LINKS_PER_PAGE]
|
gremmie@1
|
49 return render_to_response('weblinks/link_summary.html', {
|
gremmie@1
|
50 'links': links,
|
gremmie@1
|
51 'title': 'Newest Links',
|
gremmie@1
|
52 'search_form': SearchForm(),
|
gremmie@1
|
53 },
|
gremmie@1
|
54 context_instance = RequestContext(request))
|
gremmie@1
|
55
|
gremmie@1
|
56 #######################################################################
|
gremmie@1
|
57
|
gremmie@1
|
58 def popular_links(request):
|
gremmie@1
|
59 links = Link.public_objects.order_by('-hits')[:LINKS_PER_PAGE]
|
gremmie@1
|
60 return render_to_response('weblinks/link_summary.html', {
|
gremmie@1
|
61 'links': links,
|
gremmie@1
|
62 'title': 'Popular Links',
|
gremmie@1
|
63 'search_form': SearchForm(),
|
gremmie@1
|
64 },
|
gremmie@1
|
65 context_instance = RequestContext(request))
|
gremmie@1
|
66
|
gremmie@1
|
67 #######################################################################
|
gremmie@1
|
68
|
gremmie@1
|
69 @login_required
|
gremmie@1
|
70 def add_link(request):
|
gremmie@1
|
71 if request.method == 'POST':
|
gremmie@1
|
72 add_form = AddLinkForm(request.POST)
|
gremmie@1
|
73 if add_form.is_valid():
|
gremmie@1
|
74 new_link = add_form.save(commit=False)
|
gremmie@1
|
75 new_link.user = request.user
|
gremmie@1
|
76 new_link.is_public = False
|
gremmie@1
|
77 new_link.save()
|
gremmie@1
|
78 email_admins('New link for approval', """Hello,
|
gremmie@1
|
79
|
gremmie@1
|
80 A user has added a new link for your approval.
|
gremmie@1
|
81 """)
|
gremmie@1
|
82 return HttpResponseRedirect(reverse('weblinks.views.add_thanks'))
|
gremmie@1
|
83 else:
|
gremmie@1
|
84 add_form = AddLinkForm()
|
gremmie@1
|
85
|
gremmie@1
|
86 return render_to_response('weblinks/add_link.html', {
|
gremmie@1
|
87 'search_form': SearchForm(),
|
gremmie@1
|
88 'add_form': add_form,
|
gremmie@1
|
89 },
|
gremmie@1
|
90 context_instance = RequestContext(request))
|
gremmie@1
|
91
|
gremmie@1
|
92 #######################################################################
|
gremmie@1
|
93
|
gremmie@1
|
94 @login_required
|
gremmie@1
|
95 def add_thanks(request):
|
gremmie@1
|
96 return render_to_response('weblinks/add_link.html', {
|
gremmie@1
|
97 'search_form': SearchForm(),
|
gremmie@1
|
98 },
|
gremmie@1
|
99 context_instance = RequestContext(request))
|
gremmie@1
|
100
|
gremmie@1
|
101 #######################################################################
|
gremmie@1
|
102
|
gremmie@1
|
103 # Maps URL component to database field name for the links table:
|
gremmie@1
|
104
|
gremmie@1
|
105 LINK_FIELD_MAP = {
|
gremmie@1
|
106 'title': 'title',
|
gremmie@1
|
107 'date': '-date_added',
|
gremmie@1
|
108 'hits': '-hits'
|
gremmie@1
|
109 }
|
gremmie@1
|
110
|
gremmie@1
|
111 def view_links(request, category, sort='title', page='1'):
|
gremmie@1
|
112 try:
|
gremmie@1
|
113 cat = Category.objects.get(pk=category)
|
gremmie@1
|
114 except Category.DoesNotExist:
|
gremmie@1
|
115 raise Http404
|
gremmie@1
|
116
|
gremmie@1
|
117 if sort in LINK_FIELD_MAP:
|
gremmie@1
|
118 order_by = LINK_FIELD_MAP[sort]
|
gremmie@1
|
119 else:
|
gremmie@1
|
120 sort = 'title'
|
gremmie@1
|
121 order_by = LINK_FIELD_MAP['title']
|
gremmie@1
|
122
|
gremmie@1
|
123 links = Link.public_objects.filter(category = category).order_by(order_by)
|
gremmie@1
|
124 paginator = create_paginator(links)
|
gremmie@1
|
125 try:
|
gremmie@1
|
126 the_page = paginator.page(int(page))
|
gremmie@1
|
127 except InvalidPage:
|
gremmie@1
|
128 raise Http404
|
gremmie@1
|
129
|
gremmie@1
|
130 return render_to_response('weblinks/view_links.html', {
|
gremmie@1
|
131 's' : sort,
|
gremmie@1
|
132 'category' : cat,
|
gremmie@1
|
133 'page' : the_page,
|
gremmie@1
|
134 'search_form': SearchForm(),
|
gremmie@1
|
135 },
|
gremmie@1
|
136 context_instance = RequestContext(request))
|
gremmie@1
|
137
|
gremmie@1
|
138 #######################################################################
|
gremmie@1
|
139
|
gremmie@1
|
140 def _visit_link(request, link):
|
gremmie@1
|
141 link.hits += 1
|
gremmie@1
|
142 link.save()
|
gremmie@1
|
143 return HttpResponseRedirect(link.url)
|
gremmie@1
|
144
|
gremmie@1
|
145 #######################################################################
|
gremmie@1
|
146
|
gremmie@1
|
147 def visit(request, link_id):
|
gremmie@1
|
148 link = get_object_or_404(Link, pk = link_id)
|
gremmie@1
|
149 return _visit_link(request, link)
|
gremmie@1
|
150
|
gremmie@1
|
151 #######################################################################
|
gremmie@1
|
152
|
gremmie@1
|
153 def random_link(request):
|
gremmie@1
|
154 ids = Link.public_objects.values_list('id', flat=True)
|
gremmie@1
|
155 if not ids:
|
gremmie@1
|
156 raise Http404
|
gremmie@1
|
157 id = random.choice(ids)
|
gremmie@1
|
158 random_link = Link.public_objects.get(pk=id)
|
gremmie@1
|
159 return _visit_link(request, random_link)
|
gremmie@1
|
160
|
gremmie@1
|
161 #######################################################################
|
gremmie@1
|
162
|
gremmie@1
|
163 @login_required
|
gremmie@1
|
164 def report_link(request, link_id):
|
gremmie@1
|
165 link = get_object_or_404(Link, pk = link_id)
|
gremmie@1
|
166 if request.method == "POST":
|
gremmie@1
|
167 FlaggedLink.objects.create(link, request.user)
|
gremmie@1
|
168 email_admins('A Link Has Been Flagged as Broken', """Hello,
|
gremmie@1
|
169
|
gremmie@1
|
170 A user has flagged a link as broken.
|
gremmie@1
|
171 """)
|
gremmie@1
|
172 return HttpResponseRedirect(reverse('weblinks.views.report_thanks', args = (link_id, )))
|
gremmie@1
|
173
|
gremmie@1
|
174 return render_to_response('weblinks/report_link.html', {
|
gremmie@1
|
175 'link': link,
|
gremmie@1
|
176 'search_form': SearchForm(),
|
gremmie@1
|
177 'report_thanks': False,
|
gremmie@1
|
178 },
|
gremmie@1
|
179 context_instance = RequestContext(request))
|
gremmie@1
|
180
|
gremmie@1
|
181
|
gremmie@1
|
182 #######################################################################
|
gremmie@1
|
183
|
gremmie@1
|
184 @login_required
|
gremmie@1
|
185 def report_thanks(request, link_id):
|
gremmie@1
|
186 link = get_object_or_404(Link, pk = link_id)
|
gremmie@1
|
187 return render_to_response('weblinks/report_link.html', {
|
gremmie@1
|
188 'link': link,
|
gremmie@1
|
189 'search_form': SearchForm(),
|
gremmie@1
|
190 'report_thanks': True,
|
gremmie@1
|
191 },
|
gremmie@1
|
192 context_instance = RequestContext(request))
|
gremmie@1
|
193
|
gremmie@1
|
194 #######################################################################
|
gremmie@1
|
195
|
gremmie@1
|
196 def search_links(request, page=1):
|
gremmie@1
|
197 if request.method == 'POST':
|
gremmie@1
|
198 form = SearchForm(request.POST)
|
gremmie@1
|
199 if form.is_valid():
|
gremmie@1
|
200 query_text = form.query()
|
gremmie@1
|
201 page = 1
|
gremmie@1
|
202 else:
|
gremmie@1
|
203 return HttpResponseRedirect(reverse('weblinks.views.link_index'))
|
gremmie@1
|
204 else:
|
gremmie@1
|
205 if 'query' in request.GET:
|
gremmie@1
|
206 query_text = request.GET['query']
|
gremmie@1
|
207 else:
|
gremmie@1
|
208 return HttpResponseRedirect(reverse('weblinks.views.link_index'))
|
gremmie@1
|
209
|
gremmie@1
|
210 links = Link.public_objects.filter(
|
gremmie@1
|
211 Q(title__icontains = query_text) |
|
gremmie@1
|
212 Q(description__icontains = query_text)).order_by('title')
|
gremmie@1
|
213 paginator = create_paginator(links)
|
gremmie@1
|
214 try:
|
gremmie@1
|
215 the_page = paginator.page(int(page))
|
gremmie@1
|
216 except EmptyPage:
|
gremmie@1
|
217 links = Link.public_objects.none()
|
gremmie@1
|
218 except InvalidPage:
|
gremmie@1
|
219 raise Http404
|
gremmie@1
|
220
|
gremmie@1
|
221 return render_to_response('weblinks/search_results.html', {
|
gremmie@1
|
222 'query': query_text,
|
gremmie@1
|
223 'page': the_page,
|
gremmie@1
|
224 'search_form': SearchForm(),
|
gremmie@1
|
225 },
|
gremmie@1
|
226 context_instance = RequestContext(request))
|
bgneal@20
|
227
|
bgneal@20
|
228 #######################################################################
|
bgneal@20
|
229
|
bgneal@20
|
230 def link_detail(request, id):
|
bgneal@20
|
231 link = get_object_or_404(Link, pk=id)
|
bgneal@20
|
232 return render_to_response('weblinks/link_detail.html', {
|
bgneal@20
|
233 'link': link,
|
bgneal@20
|
234 'search_form': SearchForm(),
|
bgneal@20
|
235 },
|
bgneal@20
|
236 context_instance = RequestContext(request))
|