paperless-ngx/src/documents/serialisers.py

116 lines
2.6 KiB
Python
Raw Normal View History

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
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
)