See #170: added content_category on artist

merge-requests/1042/head
Eliot Berriot 2020-01-22 11:40:34 +01:00
rodzic 9c8d2ef53a
commit e2ab4ce845
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6B501DFD73514E14
7 zmienionych plików z 62 dodań i 3 usunięć

Wyświetl plik

@ -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):

Wyświetl plik

@ -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),
),
]

Wyświetl plik

@ -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()

Wyświetl plik

@ -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"] = (

Wyświetl plik

@ -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"]

Wyświetl plik

@ -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")

Wyświetl plik

@ -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": [],