Browse Source

Improve error handling for label upload

pull/1222/head
Hironsan 4 years ago
parent
commit
b190be0115
2 changed files with 13 additions and 7 deletions
  1. 5
      app/api/exceptions.py
  2. 15
      app/api/views/label.py

5
app/api/exceptions.py

@ -34,3 +34,8 @@ class AWSTokenError(ValidationError):
class SampleDataException(ValidationError):
default_detail = 'The response is empty. Maybe the sample data is not appropriate.' \
'Please specify another sample data which returns at least one label.'
class LabelValidationError(APIException):
status_code = status.HTTP_400_BAD_REQUEST
default_detail = 'You cannot create a label with same name or shortcut key.'

15
app/api/views/label.py

@ -9,6 +9,7 @@ from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from ..exceptions import LabelValidationError
from ..models import Label, Project
from ..permissions import IsInProjectReadOnlyOrAdmin, IsProjectAdmin
from ..serializers import LabelSerializer
@ -48,14 +49,14 @@ class LabelUploadAPI(APIView):
def post(self, request, *args, **kwargs):
if 'file' not in request.data:
raise ParseError('Empty content')
labels = json.load(request.data['file'])
project = get_object_or_404(Project, pk=kwargs['project_id'])
try:
for label in labels:
serializer = LabelSerializer(data=label)
serializer.is_valid(raise_exception=True)
serializer.save(project=project)
labels = json.load(request.data['file'])
serializer = LabelSerializer(data=labels, many=True)
serializer.is_valid(raise_exception=True)
serializer.save(project=project)
return Response(status=status.HTTP_201_CREATED)
except json.decoder.JSONDecodeError:
raise ParseError('The file format is invalid.')
except IntegrityError:
content = {'error': 'IntegrityError: you cannot create a label with same name or shortkey.'}
return Response(content, status=status.HTTP_400_BAD_REQUEST)
raise LabelValidationError
Loading…
Cancel
Save