paperless-ngx/src/documents/serialisers.py

97 lines
2.2 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",
2019-05-21 13:06:16 +02:00
"automatic_classification"
2019-01-27 12:52:15 +00:00
)
2016-02-16 09:28:34 +00:00
class CorrespondentField(serializers.HyperlinkedRelatedField):
def get_queryset(self):
return Correspondent.objects.all()
class TagsField(serializers.HyperlinkedRelatedField):
def get_queryset(self):
return Tag.objects.all()
2018-09-05 15:25:14 +02:00
class DocumentTypeField(serializers.HyperlinkedRelatedField):
def get_queryset(self):
return DocumentType.objects.all()
2016-02-16 09:28:34 +00:00
class DocumentSerializer(serializers.ModelSerializer):
correspondent = CorrespondentField(
view_name="drf:correspondent-detail", allow_null=True)
tags = TagsField(view_name="drf:tag-detail", many=True)
2018-09-05 15:25:14 +02:00
document_type = DocumentTypeField(
view_name="drf:documenttype-detail", allow_null=True)
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
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",
"file_type",
"tags",
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",
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"
)