# HG changeset patch # User Brian Neal # Date 1317773370 0 # Node ID 1a7ca5fa494f9b8dba7c85a75887629980c81f01 # Parent 32cec6cd8808e216161f9d35e6041966d67d4986 Fixing #229: Download details view with a bogus download ID produces internal server error isntead of a 404. diff -r 32cec6cd8808 -r 1a7ca5fa494f gpp/downloads/views.py --- a/gpp/downloads/views.py Sun Sep 25 00:49:05 2011 +0000 +++ b/gpp/downloads/views.py Wed Oct 05 00:09:30 2011 +0000 @@ -3,7 +3,7 @@ """ import random -from django.shortcuts import render_to_response +from django.shortcuts import render_to_response, get_object_or_404 from django.template import RequestContext from django.contrib.auth.decorators import login_required from django.http import Http404 @@ -57,10 +57,8 @@ @login_required def category(request, slug, sort='title'): - try: - cat = Category.objects.get(slug=slug) - except Category.DoesNotExist: - raise Http404 + + cat = get_object_or_404(Category, slug=slug) if sort not in DOWNLOAD_FIELD_MAP: sort = 'title' @@ -148,9 +146,7 @@ @login_required def details(request, id): - download = Download.public_objects.get(pk=id) - if download is None: - raise Http404 + download = get_object_or_404(Download.public_objects, pk=id) return render_to_response('downloads/download_detail.html', { 'download' : download, }, @@ -207,10 +203,7 @@ # rating will be from 0-4 rating = min(5, max(1, rating)) - try: - download = Download.public_objects.get(pk=id) - except Download.DoesNotExist: - return HttpResponseBadRequest('Invalid download id.') + download = get_object_or_404(Download.public_objects, pk=id) # prevent multiple votes from the same user vote_record, created = VoteRecord.objects.get_or_create(