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