comparison gpp/weblinks/views.py @ 1:dbd703f7d63a

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