2020-12-03 18:36:23 +01:00
|
|
|
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
|
2020-12-03 18:36:23 +01:00
|
|
|
from .parsers import is_mime_type_supported
|
2016-02-16 09:28:34 +00:00
|
|
|
|
|
|
|
|
|
2016-03-03 20:52:42 +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:
|
2016-03-03 20:52:42 +00:00
|
|
|
model = Correspondent
|
2019-01-27 12:52:15 +00:00
|
|
|
fields = (
|
|
|
|
|
"id",
|
|
|
|
|
"slug",
|
|
|
|
|
"name",
|
2020-10-28 11:45:11 +01:00
|
|
|
"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",
|
2020-10-28 11:45:11 +01:00
|
|
|
"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",
|
2020-10-28 11:45:11 +01:00
|
|
|
"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
|
|
|
|
|
|
|
|
|
2020-10-20 00:35:27 +02:00
|
|
|
class CorrespondentField(serializers.PrimaryKeyRelatedField):
|
2017-03-25 15:01:01 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
|
return Correspondent.objects.all()
|
|
|
|
|
|
|
|
|
|
|
2020-10-20 00:35:27 +02:00
|
|
|
class TagsField(serializers.PrimaryKeyRelatedField):
|
2017-03-25 15:01:01 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
|
return Tag.objects.all()
|
|
|
|
|
|
|
|
|
|
|
2020-10-20 00:35:27 +02:00
|
|
|
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-12-03 19:56:52 +01:00
|
|
|
correspondent = CorrespondentField(allow_null=True)
|
|
|
|
|
tags = TagsField(many=True)
|
|
|
|
|
document_type = DocumentTypeField(allow_null=True)
|
|
|
|
|
|
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",
|
2018-09-05 15:25:14 +02:00
|
|
|
"document_type",
|
2016-02-16 09:28:34 +00:00
|
|
|
"title",
|
|
|
|
|
"content",
|
|
|
|
|
"tags",
|
|
|
|
|
"created",
|
|
|
|
|
"modified",
|
2019-01-03 16:54:32 +00:00
|
|
|
"added",
|
2020-10-20 00:35:27 +02:00
|
|
|
"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
|
|
|
)
|
2020-12-03 18:36:23 +01: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
|