Only formalities.
[wolnelektury.git] / src / catalogue / api / serializers.py
index 06832e7..daa86fe 100644 (file)
@@ -1,5 +1,5 @@
-# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
-# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+# This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
 #
 from rest_framework import serializers
 from api.fields import AbsoluteURLField, LegacyMixin, ThumbnailField
@@ -24,7 +24,20 @@ class TagDetailSerializer(serializers.ModelSerializer):
 
     class Meta:
         model = Tag
-        fields = ['name', 'url', 'sort_key', 'description']
+        fields = [
+            'name', 'url', 'sort_key',
+            'description',
+            'description_pl',
+            'plural', 'genre_epoch_specific',
+            'adjective_feminine_singular', 'adjective_nonmasculine_plural',
+            'genitive', 'collective_noun',
+        ]
+        write_only_fields = [
+            'description_pl',
+            'plural', 'genre_epoch_specific',
+            'adjective_feminine_singular', 'adjective_nonmasculine_plural',
+            'genitive', 'collective_noun',
+        ]
 
 
 class TranslatorSerializer(serializers.Serializer):
@@ -148,12 +161,30 @@ class CollectionListSerializer(serializers.ModelSerializer):
 
 class CollectionSerializer(serializers.ModelSerializer):
     books = BookSerializer(many=True, source='get_books', read_only=True)
-    book_slugs = serializers.CharField(write_only=True)
+    authors = TagSerializer(many=True, read_only=True)
+    book_slugs = serializers.CharField(write_only=True, required=False)
+    author_slugs = serializers.CharField(write_only=True, required=False)
     url = AbsoluteURLField()
 
     class Meta:
         model = Collection
-        fields = ['url', 'books', 'description', 'title', 'book_slugs']
+        fields = [
+            'url', 'books', 'description', 'title',
+            'book_slugs', 'authors', 'author_slugs'
+        ]
+
+    def update(self, instance, validated_data):
+        instance = super().update(instance, validated_data)
+        author_slugs = validated_data.get('author_slugs', '').strip().split()
+        if author_slugs:
+            authors = Tag.objects.filter(
+                category='author',
+                slug__in=author_slugs
+            )
+        else:
+            authors = []
+        instance.authors.set(authors)
+        return instance
 
 
 class FragmentSerializer(serializers.ModelSerializer):