versioning for the tags API

This commit is contained in:
jonaswinkler 2021-02-24 23:54:19 +01:00
parent 95fe803bf6
commit d84ca511d4
3 changed files with 59 additions and 3 deletions

View File

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

View File

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

View File

@ -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']
}