Skip to content
Snippets Groups Projects

Refactor update creation api

Merged Samuel GAIST requested to merge refactor_update_creation_api into master
All threads resolved!
1 file
+ 18
20
Compare changes
  • Side-by-side
  • Inline
+ 18
20
@@ -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()
)
Loading