diff --git a/beat/web/code/api.py b/beat/web/code/api.py index 1a1a978c1435b1bf738b1ec72d0213f4eea0ddd2..13ec6332210d435a3c555db39b618efa32d7daaf 100755 --- a/beat/web/code/api.py +++ b/beat/web/code/api.py @@ -25,11 +25,12 @@ # # ############################################################################### +from django.shortcuts import get_object_or_404 from rest_framework import generics from rest_framework.response import Response +from rest_framework import exceptions as drf_exceptions -from ..common.responses import ForbiddenResponse from ..common.api import ShareView, RetrieveUpdateDestroyContributionView from ..common.serializers import DiffSerializer @@ -54,34 +55,31 @@ class DiffView(generics.RetrieveAPIView): def get(self, request, author1, name1, version1, author2, name2, version2): # Retrieve the objects - try: - object1 = self.model.objects.get( - author__username__iexact=author1, - name__iexact=name1, - version=int(version1), - ) - except Exception: - return Response("%s/%s/%s" % (author1, name1, version1), status=404) - - try: - object2 = self.model.objects.get( - author__username__iexact=author2, - name__iexact=name2, - version=int(version2), - ) - except Exception: - return Response("%s/%s/%s" % (author2, name2, version2), status=404) + + object1 = get_object_or_404( + self.model, + author__username__iexact=author1, + name__iexact=name1, + version=int(version1), + ) + + object2 = get_object_or_404( + self.model, + author__username__iexact=author2, + name__iexact=name2, + version=int(version2), + ) # Check that the user can access them has_access, open_source, _ = object1.accessibility_for(request.user) if not ((request.user == object1.author) or (has_access and open_source)): - return ForbiddenResponse( + raise drf_exceptions.PermissionDenied( 'You cannot access the source-code of "%s"' % object1.fullname() ) has_access, open_source, _ = object2.accessibility_for(request.user) if not ((request.user == object2.author) or (has_access and open_source)): - return ForbiddenResponse( + raise drf_exceptions.PermissionDenied( 'You cannot access the source-code of "%s"' % object2.fullname() )