From e2ab4ce845171e38f2ef292f0db104b839607def Mon Sep 17 00:00:00 2001 From: Eliot Berriot Date: Wed, 22 Jan 2020 11:40:34 +0100 Subject: [PATCH] See #170: added content_category on artist --- api/funkwhale_api/audio/serializers.py | 25 ++++++++++++++++--- .../migrations/0049_auto_20200122_1020.py | 18 +++++++++++++ api/funkwhale_api/music/models.py | 14 +++++++++++ api/funkwhale_api/music/serializers.py | 2 ++ api/tests/audio/test_serializers.py | 4 +++ api/tests/audio/test_views.py | 1 + api/tests/music/test_serializers.py | 1 + 7 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 api/funkwhale_api/music/migrations/0049_auto_20200122_1020.py diff --git a/api/funkwhale_api/audio/serializers.py b/api/funkwhale_api/audio/serializers.py index 205ec383c..3971a070e 100644 --- a/api/funkwhale_api/audio/serializers.py +++ b/api/funkwhale_api/audio/serializers.py @@ -18,12 +18,17 @@ class ChannelCreateSerializer(serializers.Serializer): username = serializers.CharField(max_length=music_models.MAX_LENGTHS["ARTIST_NAME"]) description = common_serializers.ContentSerializer(allow_null=True) tags = tags_serializers.TagsListField() + content_category = serializers.ChoiceField( + choices=music_models.ARTIST_CONTENT_CATEGORY_CHOICES + ) @transaction.atomic def create(self, validated_data): description = validated_data.get("description") artist = music_models.Artist.objects.create( - attributed_to=validated_data["attributed_to"], name=validated_data["name"] + attributed_to=validated_data["attributed_to"], + name=validated_data["name"], + content_category=validated_data["content_category"], ) description_obj = common_utils.attach_content( artist, "description", description @@ -56,12 +61,16 @@ class ChannelUpdateSerializer(serializers.Serializer): name = serializers.CharField(max_length=music_models.MAX_LENGTHS["ARTIST_NAME"]) description = common_serializers.ContentSerializer(allow_null=True) tags = tags_serializers.TagsListField() + content_category = serializers.ChoiceField( + choices=music_models.ARTIST_CONTENT_CATEGORY_CHOICES + ) @transaction.atomic def update(self, obj, validated_data): if validated_data.get("tags") is not None: tags_models.set_tags(obj.artist, *validated_data["tags"]) actor_update_fields = [] + artist_update_fields = [] if "description" in validated_data: description_obj = common_utils.attach_content( @@ -71,14 +80,24 @@ class ChannelUpdateSerializer(serializers.Serializer): actor_update_fields.append(("summary", description_obj.rendered)) if "name" in validated_data: - obj.artist.name = validated_data["name"] - obj.artist.save(update_fields=["name"]) actor_update_fields.append(("name", validated_data["name"])) + artist_update_fields.append(("name", validated_data["name"])) + + if "content_category" in validated_data: + artist_update_fields.append( + ("content_category", validated_data["content_category"]) + ) if actor_update_fields: for field, value in actor_update_fields: setattr(obj.actor, field, value) obj.actor.save(update_fields=[f for f, _ in actor_update_fields]) + + if artist_update_fields: + for field, value in artist_update_fields: + setattr(obj.artist, field, value) + obj.artist.save(update_fields=[f for f, _ in artist_update_fields]) + return obj def to_representation(self, obj): diff --git a/api/funkwhale_api/music/migrations/0049_auto_20200122_1020.py b/api/funkwhale_api/music/migrations/0049_auto_20200122_1020.py new file mode 100644 index 000000000..e0ea4ec8d --- /dev/null +++ b/api/funkwhale_api/music/migrations/0049_auto_20200122_1020.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.9 on 2020-01-22 10:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('music', '0048_auto_20200120_0900'), + ] + + operations = [ + migrations.AddField( + model_name='artist', + name='content_category', + field=models.CharField(choices=[('music', 'music'), ('podcast', 'podcast'), ('other', 'other')], db_index=True, default='music', max_length=30, null=True), + ), + ] diff --git a/api/funkwhale_api/music/models.py b/api/funkwhale_api/music/models.py index 57379453a..e1852870e 100644 --- a/api/funkwhale_api/music/models.py +++ b/api/funkwhale_api/music/models.py @@ -43,6 +43,13 @@ MAX_LENGTHS = { } +ARTIST_CONTENT_CATEGORY_CHOICES = [ + ("music", "music"), + ("podcast", "podcast"), + ("other", "other"), +] + + def empty_dict(): return {} @@ -237,6 +244,13 @@ class Artist(APIModelMixin): on_delete=models.SET_NULL, related_name="covered_artist", ) + content_category = models.CharField( + max_length=30, + db_index=True, + default="music", + choices=ARTIST_CONTENT_CATEGORY_CHOICES, + null=True, + ) api = musicbrainz.api.artists objects = ArtistQuerySet.as_manager() diff --git a/api/funkwhale_api/music/serializers.py b/api/funkwhale_api/music/serializers.py index 34025f297..27cca51da 100644 --- a/api/funkwhale_api/music/serializers.py +++ b/api/funkwhale_api/music/serializers.py @@ -119,6 +119,7 @@ class ArtistWithAlbumsSerializer(OptionalDescriptionMixin, serializers.Serialize fid = serializers.URLField() mbid = serializers.UUIDField() name = serializers.CharField() + content_category = serializers.CharField() creation_date = serializers.DateTimeField() is_local = serializers.BooleanField() cover = cover_field @@ -142,6 +143,7 @@ def serialize_artist_simple(artist): "name": artist.name, "creation_date": DATETIME_FIELD.to_representation(artist.creation_date), "is_local": artist.is_local, + "content_category": artist.content_category, } if "description" in artist._state.fields_cache: data["description"] = ( diff --git a/api/tests/audio/test_serializers.py b/api/tests/audio/test_serializers.py index 7f39bb338..243f52372 100644 --- a/api/tests/audio/test_serializers.py +++ b/api/tests/audio/test_serializers.py @@ -14,6 +14,7 @@ def test_channel_serializer_create(factories): "username": "mychannel", "description": {"text": "This is my channel", "content_type": "text/markdown"}, "tags": ["hello", "world"], + "content_category": "other", } serializer = serializers.ChannelCreateSerializer(data=data) @@ -28,6 +29,7 @@ def test_channel_serializer_create(factories): == data["tags"] ) assert channel.artist.description.text == data["description"]["text"] + assert channel.artist.content_category == data["content_category"] assert ( channel.artist.description.content_type == data["description"]["content_type"] ) @@ -49,6 +51,7 @@ def test_channel_serializer_update(factories): "name": "My channel", "description": {"text": "This is my channel", "content_type": "text/markdown"}, "tags": ["hello", "world"], + "content_category": "other", } serializer = serializers.ChannelUpdateSerializer(channel, data=data) @@ -58,6 +61,7 @@ def test_channel_serializer_update(factories): channel.refresh_from_db() assert channel.artist.name == data["name"] + assert channel.artist.content_category == data["content_category"] assert ( sorted(channel.artist.tagged_items.values_list("tag__name", flat=True)) == data["tags"] diff --git a/api/tests/audio/test_views.py b/api/tests/audio/test_views.py index 935ee4342..b40fa77bf 100644 --- a/api/tests/audio/test_views.py +++ b/api/tests/audio/test_views.py @@ -14,6 +14,7 @@ def test_channel_create(logged_in_api_client): "username": "mychannel", "description": {"text": "This is my channel", "content_type": "text/markdown"}, "tags": ["hello", "world"], + "content_category": "podcast", } url = reverse("api:v1:channels-list") diff --git a/api/tests/music/test_serializers.py b/api/tests/music/test_serializers.py index 156abe33b..6325ff54b 100644 --- a/api/tests/music/test_serializers.py +++ b/api/tests/music/test_serializers.py @@ -66,6 +66,7 @@ def test_artist_with_albums_serializer(factories, to_api_date): "mbid": str(artist.mbid), "name": artist.name, "is_local": artist.is_local, + "content_category": artist.content_category, "creation_date": to_api_date(artist.creation_date), "albums": [serializers.ArtistAlbumSerializer(album).data], "tags": [],