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.
master
Tom Hacohen 4 years ago
parent 86c5d711a6
commit fae15fe420

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

Loading…
Cancel
Save