better error messages for file uploads. adresses #91

This commit is contained in:
jonaswinkler
2020-12-06 22:30:04 +01:00
parent 41755c509e
commit 18c9c22941
3 changed files with 38 additions and 36 deletions

View File

@@ -150,8 +150,7 @@ class PostDocumentSerializer(serializers.Serializer):
required=False,
)
def validate(self, attrs):
document = attrs.get('document')
def validate_document(self, document):
try:
validate_filename(document.name)
@@ -163,32 +162,31 @@ class PostDocumentSerializer(serializers.Serializer):
if not is_mime_type_supported(mime_type):
raise serializers.ValidationError(
"This mime type is not supported.")
"This file type is not supported.")
attrs['document_data'] = document_data
return document.name, document_data
title = attrs.get('title')
def validate_title(self, title):
if title:
return title
else:
# do not return empty strings.
return None
if not title:
attrs['title'] = None
correspondent = attrs.get('correspondent')
def validate_correspondent(self, correspondent):
if correspondent:
attrs['correspondent_id'] = correspondent.id
return correspondent.id
else:
attrs['correspondent_id'] = None
return None
document_type = attrs.get('document_type')
def validate_document_type(self, document_type):
if document_type:
attrs['document_type_id'] = document_type.id
return document_type.id
else:
attrs['document_type_id'] = None
return None
tags = attrs.get('tags')
def validate_tags(self, tags):
if tags:
tag_ids = [tag.id for tag in tags]
attrs['tag_ids'] = tag_ids
return [tag.id for tag in tags]
else:
attrs['tag_ids'] = None
return attrs
return None

View File

@@ -235,12 +235,11 @@ class PostDocumentView(APIView):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
document = serializer.validated_data['document']
document_data = serializer.validated_data['document_data']
correspondent_id = serializer.validated_data['correspondent_id']
document_type_id = serializer.validated_data['document_type_id']
tag_ids = serializer.validated_data['tag_ids']
title = serializer.validated_data['title']
doc_name, doc_data = serializer.validated_data.get('document')
correspondent_id = serializer.validated_data.get('correspondent')
document_type_id = serializer.validated_data.get('document_type')
tag_ids = serializer.validated_data.get('tags')
title = serializer.validated_data.get('title')
t = int(mktime(datetime.now().timetuple()))
@@ -249,17 +248,17 @@ class PostDocumentView(APIView):
with tempfile.NamedTemporaryFile(prefix="paperless-upload-",
dir=settings.SCRATCH_DIR,
delete=False) as f:
f.write(document_data)
f.write(doc_data)
os.utime(f.name, times=(t, t))
async_task("documents.tasks.consume_file",
f.name,
override_filename=document.name,
override_filename=doc_name,
override_title=title,
override_correspondent_id=correspondent_id,
override_document_type_id=document_type_id,
override_tag_ids=tag_ids,
task_name=os.path.basename(document.name)[:100])
task_name=os.path.basename(doc_name)[:100])
return Response("OK")