paperless-ngx/src/documents/serialisers.py

101 lines
2.3 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
2018-09-02 21:26:06 +01:00
class Meta:
model = Correspondent
2019-01-27 12:52:15 +00:00
fields = (
"id",
"slug",
"name",
2019-05-21 13:06:16 +02:00
"automatic_classification"
)
2016-02-16 09:28:34 +00:00
2018-09-05 15:25:14 +02:00
class DocumentTypeSerializer(serializers.HyperlinkedModelSerializer):
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",
"automatic_classification"
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
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",
"automatic_classification",
"is_inbox_tag"
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-10-21 12:16:25 +02:00
correspondent_id = CorrespondentField(allow_null=True, source='correspondent')
tags_id = TagsField(many=True, source='tags')
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",
"file_type",
"tags",
2020-10-21 12:16:25 +02:00
"tags_id",
2016-04-04 22:09:32 +01:00
"checksum",
2016-02-16 09:28:34 +00:00
"created",
"modified",
"added",
2016-02-16 09:28:34 +00:00
"file_name",
2016-03-05 12:34:26 +00:00
"download_url",
"thumbnail_url",
"archive_serial_number"
2016-02-16 09:28:34 +00:00
)
2016-03-01 18:57:12 +00:00
class LogSerializer(serializers.ModelSerializer):
time = serializers.DateTimeField()
messages = serializers.CharField()
2018-09-02 21:26:06 +01:00
class Meta:
2016-03-01 18:57:12 +00:00
model = Log
fields = (
"time",
"messages"
)