Skip to content
Snippets Groups Projects
Commit 7f850b7d authored by Samuel GAIST's avatar Samuel GAIST Committed by Samuel GAIST
Browse files

[code][api] Cleanup diff entry point

parent 95c29663
No related branches found
No related tags found
No related merge requests found
This commit is part of merge request !327. Comments created here will be created in the context of that merge request.
......@@ -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()
)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment