Authors in collections
authorRadek Czajka <rczajka@rczajka.pl>
Thu, 27 Oct 2022 09:19:26 +0000 (11:19 +0200)
committerRadek Czajka <rczajka@rczajka.pl>
Thu, 27 Oct 2022 09:19:26 +0000 (11:19 +0200)
src/catalogue/api/serializers.py
src/catalogue/migrations/0039_collection_authors.py [new file with mode: 0644]
src/catalogue/models/collection.py

index 06832e7..02a98a1 100644 (file)
@@ -148,12 +148,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):
diff --git a/src/catalogue/migrations/0039_collection_authors.py b/src/catalogue/migrations/0039_collection_authors.py
new file mode 100644 (file)
index 0000000..3e33a9f
--- /dev/null
@@ -0,0 +1,18 @@
+# Generated by Django 4.0.8 on 2022-10-27 09:01
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('catalogue', '0038_auto_20220930_1050'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='collection',
+            name='authors',
+            field=models.ManyToManyField(limit_choices_to={'category': 'author'}, to='catalogue.tag'),
+        ),
+    ]
index 2f64b79..75c56ea 100644 (file)
@@ -15,6 +15,10 @@ class Collection(models.Model):
     slug = models.SlugField(_('slug'), max_length=120, primary_key=True)
     description = models.TextField(_('description'), null=True, blank=True)
     book_slugs = models.TextField(_('book slugs'))
+    authors = models.ManyToManyField(
+        'Tag',
+        limit_choices_to={'category': 'author'}
+    )
     kind = models.CharField(_('kind'), max_length=10, blank=False, default='book', db_index=True,
                             choices=(('book', _('book')), ('picture', _('picture'))))
     listed = models.BooleanField(_('listed'), default=True, db_index=True)