From 528a34731579d8b81df43879f2b63c2754005a86 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Thu, 27 Oct 2022 11:19:26 +0200 Subject: [PATCH] Authors in collections --- src/catalogue/api/serializers.py | 22 +++++++++++++++++-- .../migrations/0039_collection_authors.py | 18 +++++++++++++++ src/catalogue/models/collection.py | 4 ++++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 src/catalogue/migrations/0039_collection_authors.py diff --git a/src/catalogue/api/serializers.py b/src/catalogue/api/serializers.py index 06832e794..02a98a154 100644 --- a/src/catalogue/api/serializers.py +++ b/src/catalogue/api/serializers.py @@ -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 index 000000000..3e33a9fd4 --- /dev/null +++ b/src/catalogue/migrations/0039_collection_authors.py @@ -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'), + ), + ] diff --git a/src/catalogue/models/collection.py b/src/catalogue/models/collection.py index 2f64b79de..75c56ea99 100644 --- a/src/catalogue/models/collection.py +++ b/src/catalogue/models/collection.py @@ -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) -- 2.20.1