Views: clean up how we use serializers and remove integrity_errors catch-alls.

The integrity errors were a bad relic from the EteSync sources and needed
to be removed.
This commit is contained in:
Tom Hacohen 2020-07-10 09:27:34 +03:00
parent 86c5d711a6
commit fae15fe420

View File

@ -197,17 +197,11 @@ class CollectionViewSet(BaseViewSet):
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
if serializer.is_valid():
try:
serializer.is_valid(raise_exception=True)
serializer.save(owner=self.request.user)
except IntegrityError:
content = {'code': 'integrity_error'}
return Response(content, status=status.HTTP_400_BAD_REQUEST)
return Response({}, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def list(self, request):
queryset = self.get_queryset()
result, new_stoken, done = self.filter_by_stoken_and_limit(request, queryset)
@ -326,7 +320,7 @@ class CollectionItemViewSet(BaseViewSet):
queryset = self.get_queryset()
serializer = CollectionItemBulkGetSerializer(data=request.data, many=True)
if serializer.is_valid():
serializer.is_valid(raise_exception=True)
# FIXME: make configurable?
item_limit = 200
@ -354,8 +348,6 @@ class CollectionItemViewSet(BaseViewSet):
}
return Response(ret)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@action_decorator(detail=False, methods=['POST'])
def batch(self, request, collection_uid=None):
return self.transaction(request, collection_uid, validate_etag=False)
@ -383,12 +375,7 @@ class CollectionItemViewSet(BaseViewSet):
ser_valid = serializer.is_valid()
deps_ser_valid = (deps is None or deps_serializer.is_valid())
if ser_valid and deps_ser_valid:
try:
items = serializer.save(collection=collection_object)
except IntegrityError:
# FIXME: return the items with a bad token (including deps) so we don't have to fetch them after
content = {'code': 'integrity_error'}
return Response(content, status=status.HTTP_400_BAD_REQUEST)
ret = {
}
@ -423,17 +410,11 @@ class CollectionItemChunkViewSet(viewsets.ViewSet):
col_it = get_object_or_404(col.items, uid=collection_item_uid)
serializer = self.get_serializer_class()(data=request.data)
if serializer.is_valid():
try:
serializer.is_valid(raise_exception=True)
serializer.save(item=col_it)
except IntegrityError:
content = {'code': 'integrity_error'}
return Response(content, status=status.HTTP_400_BAD_REQUEST)
return Response({}, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@action_decorator(detail=True, methods=['GET'])
def download(self, request, collection_uid=None, collection_item_uid=None, uid=None):
import os
@ -559,7 +540,7 @@ class InvitationOutgoingViewSet(InvitationBaseViewSet):
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
if serializer.is_valid():
serializer.is_valid(raise_exception=True)
collection_uid = serializer.validated_data.get('collection', {}).get('uid')
try:
@ -574,8 +555,6 @@ class InvitationOutgoingViewSet(InvitationBaseViewSet):
return Response({}, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@action_decorator(detail=False, allowed_methods=['GET'], methods=['GET'])
def fetch_user_profile(self, request):
username = request.GET.get('username')
@ -685,7 +664,7 @@ class AuthenticationViewSet(viewsets.ViewSet):
from datetime import datetime
serializer = AuthenticationLoginChallengeSerializer(data=request.data)
if serializer.is_valid():
serializer.is_valid(raise_exception=True)
username = serializer.validated_data.get('username')
user = self.get_login_user(username)
@ -706,8 +685,6 @@ class AuthenticationViewSet(viewsets.ViewSet):
}
return Response(ret, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@action_decorator(detail=False, methods=['POST'])
def login(self, request):
outer_serializer = AuthenticationLoginSerializer(data=request.data)