comparison gpp/downloads/views.py @ 480:1a7ca5fa494f

Fixing #229: Download details view with a bogus download ID produces internal server error isntead of a 404.
author Brian Neal <bgneal@gmail.com>
date Wed, 05 Oct 2011 00:09:30 +0000
parents 41411066b16d
children
comparison
equal deleted inserted replaced
479:32cec6cd8808 480:1a7ca5fa494f
1 """ 1 """
2 Views for the downloads application. 2 Views for the downloads application.
3 """ 3 """
4 import random 4 import random
5 5
6 from django.shortcuts import render_to_response 6 from django.shortcuts import render_to_response, get_object_or_404
7 from django.template import RequestContext 7 from django.template import RequestContext
8 from django.contrib.auth.decorators import login_required 8 from django.contrib.auth.decorators import login_required
9 from django.http import Http404 9 from django.http import Http404
10 from django.http import HttpResponse 10 from django.http import HttpResponse
11 from django.http import HttpResponseRedirect 11 from django.http import HttpResponseRedirect
55 'hits': '-hits' 55 'hits': '-hits'
56 } 56 }
57 57
58 @login_required 58 @login_required
59 def category(request, slug, sort='title'): 59 def category(request, slug, sort='title'):
60 try: 60
61 cat = Category.objects.get(slug=slug) 61 cat = get_object_or_404(Category, slug=slug)
62 except Category.DoesNotExist:
63 raise Http404
64 62
65 if sort not in DOWNLOAD_FIELD_MAP: 63 if sort not in DOWNLOAD_FIELD_MAP:
66 sort = 'title' 64 sort = 'title'
67 order_by = DOWNLOAD_FIELD_MAP[sort] 65 order_by = DOWNLOAD_FIELD_MAP[sort]
68 66
146 144
147 ####################################################################### 145 #######################################################################
148 146
149 @login_required 147 @login_required
150 def details(request, id): 148 def details(request, id):
151 download = Download.public_objects.get(pk=id) 149 download = get_object_or_404(Download.public_objects, pk=id)
152 if download is None:
153 raise Http404
154 return render_to_response('downloads/download_detail.html', { 150 return render_to_response('downloads/download_detail.html', {
155 'download' : download, 151 'download' : download,
156 }, 152 },
157 context_instance = RequestContext(request)) 153 context_instance = RequestContext(request))
158 154
205 return HttpResponseBadRequest('Invalid rating.') 201 return HttpResponseBadRequest('Invalid rating.')
206 202
207 # rating will be from 0-4 203 # rating will be from 0-4
208 rating = min(5, max(1, rating)) 204 rating = min(5, max(1, rating))
209 205
210 try: 206 download = get_object_or_404(Download.public_objects, pk=id)
211 download = Download.public_objects.get(pk=id)
212 except Download.DoesNotExist:
213 return HttpResponseBadRequest('Invalid download id.')
214 207
215 # prevent multiple votes from the same user 208 # prevent multiple votes from the same user
216 vote_record, created = VoteRecord.objects.get_or_create( 209 vote_record, created = VoteRecord.objects.get_or_create(
217 download=download, user=request.user) 210 download=download, user=request.user)
218 if created: 211 if created: