comparison gpp/downloads/views.py @ 241:27bee3ac85e6

For #93: fix url scheme for downloads.
author Brian Neal <bgneal@gmail.com>
date Wed, 15 Sep 2010 01:01:40 +0000
parents 2022c0409296
children 7e8d2dda99e3
comparison
equal deleted inserted replaced
240:1246a4f1ab4f 241:27bee3ac85e6
16 from django.db.models import Q 16 from django.db.models import Q
17 from django.views.decorators.http import require_POST 17 from django.views.decorators.http import require_POST
18 18
19 from core.paginator import DiggPaginator 19 from core.paginator import DiggPaginator
20 from core.functions import email_admins 20 from core.functions import email_admins
21 from core.functions import get_page
21 from downloads.models import Category 22 from downloads.models import Category
22 from downloads.models import Download 23 from downloads.models import Download
23 from downloads.models import VoteRecord 24 from downloads.models import VoteRecord
24 from downloads.forms import AddDownloadForm 25 from downloads.forms import AddDownloadForm
25 from downloads.forms import SearchForm 26
26 27 #######################################################################
27 ####################################################################### 28
28 29 DLS_PER_PAGE = 1
29 DLS_PER_PAGE = 10
30 30
31 def create_paginator(dls): 31 def create_paginator(dls):
32 return DiggPaginator(dls, DLS_PER_PAGE, body=5, tail=3, margin=3, padding=2) 32 return DiggPaginator(dls, DLS_PER_PAGE, body=5, tail=3, margin=3, padding=2)
33 33
34 ####################################################################### 34 #######################################################################
52 'rating': '-average_score', 52 'rating': '-average_score',
53 'hits': '-hits' 53 'hits': '-hits'
54 } 54 }
55 55
56 @login_required 56 @login_required
57 def category(request, category, sort='title', page='1'): 57 def category(request, slug, sort='title'):
58 try: 58 try:
59 cat = Category.objects.get(pk=category) 59 cat = Category.objects.get(slug=slug)
60 except Category.DoesNotExist: 60 except Category.DoesNotExist:
61 raise Http404 61 raise Http404
62 62
63 if sort not in DOWNLOAD_FIELD_MAP: 63 if sort not in DOWNLOAD_FIELD_MAP:
64 sort = 'title' 64 sort = 'title'
65 order_by = DOWNLOAD_FIELD_MAP[sort] 65 order_by = DOWNLOAD_FIELD_MAP[sort]
66 66
67 downloads = Download.public_objects.filter(category=cat.pk).order_by( 67 downloads = Download.public_objects.filter(category=cat.pk).order_by(
68 order_by) 68 order_by)
69 paginator = create_paginator(downloads) 69 paginator = create_paginator(downloads)
70 page = get_page(request.GET)
70 try: 71 try:
71 the_page = paginator.page(int(page)) 72 the_page = paginator.page(page)
72 except InvalidPage: 73 except InvalidPage:
73 raise Http404 74 raise Http404
74 75
75 return render_to_response('downloads/download_list.html', { 76 return render_to_response('downloads/download_list.html', {
76 's' : sort, 77 's' : sort,
182 @login_required 183 @login_required
183 def thanks(request): 184 def thanks(request):
184 return render_to_response('downloads/thanks.html', { 185 return render_to_response('downloads/thanks.html', {
185 }, 186 },
186 context_instance=RequestContext(request)) 187 context_instance=RequestContext(request))
187
188 #######################################################################
189
190 @login_required
191 def search(request, page=1):
192 if request.method == 'POST':
193 form = SearchForm(request.POST)
194 if form.is_valid():
195 query_text = form.query()
196 page = 1
197 else:
198 return HttpResponseRedirect(reverse('downloads-index'))
199 else:
200 if 'query' in request.GET:
201 query_text = request.GET['query']
202 else:
203 return HttpResponseRedirect(reverse('downloads-index'))
204
205 dls = Download.objects.filter(
206 Q(title__icontains = query_text) |
207 Q(description__icontains = query_text)).order_by(
208 'title').select_related()
209 paginator = create_paginator(dls)
210 try:
211 the_page = paginator.page(int(page))
212 except EmptyPage:
213 dls = Download.objects.none()
214 except InvalidPage:
215 raise Http404
216
217 return render_to_response('downloads/search_results.html', {
218 'query': query_text,
219 'page': the_page,
220 },
221 context_instance = RequestContext(request))
222 188
223 ####################################################################### 189 #######################################################################
224 190
225 @require_POST 191 @require_POST
226 def rate_download(request): 192 def rate_download(request):
243 download = Download.public_objects.get(pk=id) 209 download = Download.public_objects.get(pk=id)
244 except Download.DoesNotExist: 210 except Download.DoesNotExist:
245 return HttpResponseBadRequest('Invalid download id.') 211 return HttpResponseBadRequest('Invalid download id.')
246 212
247 # prevent multiple votes from the same user 213 # prevent multiple votes from the same user
248 vote_record, created = VoteRecord.objects.get_or_create(download=download, user=request.user) 214 vote_record, created = VoteRecord.objects.get_or_create(
215 download=download, user=request.user)
249 if created: 216 if created:
250 new_score = download.vote(rating) 217 new_score = download.vote(rating)
251 download.save() 218 download.save()
252 return HttpResponse(str(new_score)) 219 return HttpResponse(str(new_score))
253 else: 220 else: