X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/7fac6e7f9c46a423bb3581856c42c8c7ded7d3cf..d10c63f27d1494a04d57a6c805d093089526430a:/src/catalogue/api/serializers.py

diff --git a/src/catalogue/api/serializers.py b/src/catalogue/api/serializers.py
index 45252a8f0..02a98a154 100644
--- a/src/catalogue/api/serializers.py
+++ b/src/catalogue/api/serializers.py
@@ -147,12 +147,31 @@ class CollectionListSerializer(serializers.ModelSerializer):
 
 
 class CollectionSerializer(serializers.ModelSerializer):
-    books = BookSerializer(many=True, source='get_books')
+    books = BookSerializer(many=True, source='get_books', read_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']
+        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):