paperless-ngx/src/documents/serialisers.py

201 lines
4.9 KiB
Python
Raw Normal View History

import magic
from pathvalidate import validate_filename, ValidationError
2016-02-16 09:28:34 +00:00
from rest_framework import serializers
2018-09-05 15:25:14 +02:00
from .models import Correspondent, Tag, Document, Log, DocumentType
from .parsers import is_mime_type_supported
2016-02-16 09:28:34 +00:00
class CorrespondentSerializer(serializers.HyperlinkedModelSerializer):
2016-02-16 09:28:34 +00:00
2020-10-21 12:53:14 +02:00
document_count = serializers.IntegerField(read_only=True)
last_correspondence = serializers.DateTimeField(read_only=True)
2018-09-02 21:26:06 +01:00
class Meta:
model = Correspondent
2019-01-27 12:52:15 +00:00
fields = (
"id",
"slug",
"name",
"match",
"matching_algorithm",
"is_insensitive",
2020-10-21 12:53:14 +02:00
"document_count",
"last_correspondence"
2019-05-21 13:06:16 +02:00
)
2016-02-16 09:28:34 +00:00
2018-09-05 15:25:14 +02:00
class DocumentTypeSerializer(serializers.HyperlinkedModelSerializer):
2020-10-21 12:53:14 +02:00
document_count = serializers.IntegerField(read_only=True)
2018-12-11 12:26:44 +01:00
class Meta:
2018-09-05 15:25:14 +02:00
model = DocumentType
2019-05-21 13:06:16 +02:00
fields = (
"id",
"slug",
"name",
"match",
"matching_algorithm",
"is_insensitive",
2020-10-21 12:53:14 +02:00
"document_count"
2019-01-27 12:52:15 +00:00
)
2018-09-05 15:25:14 +02:00
2016-02-21 00:55:38 +00:00
class TagSerializer(serializers.HyperlinkedModelSerializer):
2016-02-16 09:28:34 +00:00
2020-10-21 12:53:14 +02:00
document_count = serializers.IntegerField(read_only=True)
2018-09-02 21:26:06 +01:00
class Meta:
2016-02-16 09:28:34 +00:00
model = Tag
2016-02-21 00:14:50 +00:00
fields = (
2019-01-27 12:52:15 +00:00
"id",
"slug",
"name",
"colour",
"match",
"matching_algorithm",
"is_insensitive",
2020-10-21 12:53:14 +02:00
"is_inbox_tag",
"document_count"
2019-01-27 12:52:15 +00:00
)
2016-02-16 09:28:34 +00:00
class CorrespondentField(serializers.PrimaryKeyRelatedField):
def get_queryset(self):
return Correspondent.objects.all()
class TagsField(serializers.PrimaryKeyRelatedField):
def get_queryset(self):
return Tag.objects.all()
class DocumentTypeField(serializers.PrimaryKeyRelatedField):
2018-09-05 15:25:14 +02:00
def get_queryset(self):
return DocumentType.objects.all()
2016-02-16 09:28:34 +00:00
class DocumentSerializer(serializers.ModelSerializer):
2020-11-21 14:03:45 +01:00
correspondent_id = CorrespondentField(
allow_null=True, source='correspondent')
2020-10-21 12:16:25 +02:00
tags_id = TagsField(many=True, source='tags')
2020-11-21 14:03:45 +01:00
document_type_id = DocumentTypeField(
allow_null=True, source='document_type')
2016-02-16 09:28:34 +00:00
2018-09-02 21:26:06 +01:00
class Meta:
2016-02-16 09:28:34 +00:00
model = Document
2020-10-21 12:16:25 +02:00
depth = 1
2016-02-16 09:28:34 +00:00
fields = (
"id",
2016-03-04 09:14:50 +00:00
"correspondent",
2020-10-21 12:16:25 +02:00
"correspondent_id",
2018-09-05 15:25:14 +02:00
"document_type",
2020-10-21 12:16:25 +02:00
"document_type_id",
2016-02-16 09:28:34 +00:00
"title",
"content",
"tags",
2020-10-21 12:16:25 +02:00
"tags_id",
2016-02-16 09:28:34 +00:00
"created",
"modified",
"added",
"archive_serial_number"
2016-02-16 09:28:34 +00:00
)
2016-03-01 18:57:12 +00:00
class LogSerializer(serializers.ModelSerializer):
2018-09-02 21:26:06 +01:00
class Meta:
2016-03-01 18:57:12 +00:00
model = Log
fields = (
2020-11-02 01:24:56 +01:00
"id",
"created",
"message",
"group",
"level"
2016-03-01 18:57:12 +00:00
)
class PostDocumentSerializer(serializers.Serializer):
document = serializers.FileField(
label="Document",
write_only=True,
)
title = serializers.CharField(
label="Title",
write_only=True,
required=False,
)
correspondent = serializers.CharField(
label="Correspondent",
write_only=True,
required=False,
)
document_type = serializers.CharField(
label="Document type",
write_only=True,
required=False,
)
tags = serializers.ListField(
child=serializers.CharField(),
label="Tags",
source="tag",
write_only=True,
required=False,
)
def validate(self, attrs):
document = attrs.get('document')
try:
validate_filename(document.name)
except ValidationError:
raise serializers.ValidationError("Invalid filename.")
document_data = document.file.read()
mime_type = magic.from_buffer(document_data, mime=True)
if not is_mime_type_supported(mime_type):
raise serializers.ValidationError(
"This mime type is not supported.")
attrs['document_data'] = document_data
title = attrs.get('title')
if not title:
attrs['title'] = None
correspondent = attrs.get('correspondent')
if correspondent:
c, _ = Correspondent.objects.get_or_create(name=correspondent)
attrs['correspondent_id'] = c.id
else:
attrs['correspondent_id'] = None
document_type = attrs.get('document_type')
if document_type:
dt, _ = DocumentType.objects.get_or_create(name=document_type)
attrs['document_type_id'] = dt.id
else:
attrs['document_type_id'] = None
tags = attrs.get('tag')
if tags:
tag_ids = []
for tag in tags:
tag, _ = Tag.objects.get_or_create(name=tag)
tag_ids.append(tag.id)
attrs['tag_ids'] = tag_ids
else:
attrs['tag_ids'] = None
return attrs