From d84ca511d4642ec336e7f3272217d9ef36d35ae6 Mon Sep 17 00:00:00 2001 From: jonaswinkler <17569239+jonaswinkler@users.noreply.github.com> Date: Wed, 24 Feb 2021 23:54:19 +0100 Subject: [PATCH] versioning for the tags API --- src/documents/serialisers.py | 52 +++++++++++++++++++++++++++++++++++- src/documents/views.py | 8 +++++- src/paperless/settings.py | 2 +- 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index adda7117e..e6aef3627 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -88,7 +88,40 @@ class DocumentTypeSerializer(MatchingModelSerializer): ) -class TagSerializer(MatchingModelSerializer): +class ColorField(serializers.Field): + + COLOURS = ( + (1, "#a6cee3"), + (2, "#1f78b4"), + (3, "#b2df8a"), + (4, "#33a02c"), + (5, "#fb9a99"), + (6, "#e31a1c"), + (7, "#fdbf6f"), + (8, "#ff7f00"), + (9, "#cab2d6"), + (10, "#6a3d9a"), + (11, "#b15928"), + (12, "#000000"), + (13, "#cccccc") + ) + + def to_internal_value(self, data): + for id, color in self.COLOURS: + if id == data: + return color + return "#a6cee3" + + def to_representation(self, value): + for id, color in self.COLOURS: + if color == value: + return id + return 1 + + +class TagSerializerVersion1(MatchingModelSerializer): + + colour = ColorField(source='color') class Meta: model = Tag @@ -105,6 +138,23 @@ class TagSerializer(MatchingModelSerializer): ) +class TagSerializer(MatchingModelSerializer): + + class Meta: + model = Tag + fields = ( + "id", + "slug", + "name", + "color", + "match", + "matching_algorithm", + "is_insensitive", + "is_inbox_tag", + "document_count" + ) + + class CorrespondentField(serializers.PrimaryKeyRelatedField): def get_queryset(self): return Correspondent.objects.all() diff --git a/src/documents/views.py b/src/documents/views.py index f5ddd88ee..bed44633d 100755 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -50,6 +50,7 @@ from .parsers import get_parser_class_for_mime_type from .serialisers import ( CorrespondentSerializer, DocumentSerializer, + TagSerializerVersion1, TagSerializer, DocumentTypeSerializer, PostDocumentSerializer, @@ -118,7 +119,12 @@ class TagViewSet(ModelViewSet): queryset = Tag.objects.annotate( document_count=Count('documents')).order_by(Lower('name')) - serializer_class = TagSerializer + def get_serializer_class(self): + if int(self.request.version) == 1: + return TagSerializerVersion1 + else: + return TagSerializer + pagination_class = StandardPagination permission_classes = (IsAuthenticated,) filter_backends = (DjangoFilterBackend, OrderingFilter) diff --git a/src/paperless/settings.py b/src/paperless/settings.py index 48712954c..3dcff4a89 100644 --- a/src/paperless/settings.py +++ b/src/paperless/settings.py @@ -114,7 +114,7 @@ REST_FRAMEWORK = { 'rest_framework.authentication.TokenAuthentication' ], 'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.AcceptHeaderVersioning', - 'DEFAULT_VERSION': 'v1', + 'DEFAULT_VERSION': '1', 'ALLOWED_VERSIONS': ['1', '2'] }