See #170: added content_category on artist
This commit is contained in:
parent
9c8d2ef53a
commit
e2ab4ce845
|
@ -18,12 +18,17 @@ class ChannelCreateSerializer(serializers.Serializer):
|
||||||
username = serializers.CharField(max_length=music_models.MAX_LENGTHS["ARTIST_NAME"])
|
username = serializers.CharField(max_length=music_models.MAX_LENGTHS["ARTIST_NAME"])
|
||||||
description = common_serializers.ContentSerializer(allow_null=True)
|
description = common_serializers.ContentSerializer(allow_null=True)
|
||||||
tags = tags_serializers.TagsListField()
|
tags = tags_serializers.TagsListField()
|
||||||
|
content_category = serializers.ChoiceField(
|
||||||
|
choices=music_models.ARTIST_CONTENT_CATEGORY_CHOICES
|
||||||
|
)
|
||||||
|
|
||||||
@transaction.atomic
|
@transaction.atomic
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
description = validated_data.get("description")
|
description = validated_data.get("description")
|
||||||
artist = music_models.Artist.objects.create(
|
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(
|
description_obj = common_utils.attach_content(
|
||||||
artist, "description", description
|
artist, "description", description
|
||||||
|
@ -56,12 +61,16 @@ class ChannelUpdateSerializer(serializers.Serializer):
|
||||||
name = serializers.CharField(max_length=music_models.MAX_LENGTHS["ARTIST_NAME"])
|
name = serializers.CharField(max_length=music_models.MAX_LENGTHS["ARTIST_NAME"])
|
||||||
description = common_serializers.ContentSerializer(allow_null=True)
|
description = common_serializers.ContentSerializer(allow_null=True)
|
||||||
tags = tags_serializers.TagsListField()
|
tags = tags_serializers.TagsListField()
|
||||||
|
content_category = serializers.ChoiceField(
|
||||||
|
choices=music_models.ARTIST_CONTENT_CATEGORY_CHOICES
|
||||||
|
)
|
||||||
|
|
||||||
@transaction.atomic
|
@transaction.atomic
|
||||||
def update(self, obj, validated_data):
|
def update(self, obj, validated_data):
|
||||||
if validated_data.get("tags") is not None:
|
if validated_data.get("tags") is not None:
|
||||||
tags_models.set_tags(obj.artist, *validated_data["tags"])
|
tags_models.set_tags(obj.artist, *validated_data["tags"])
|
||||||
actor_update_fields = []
|
actor_update_fields = []
|
||||||
|
artist_update_fields = []
|
||||||
|
|
||||||
if "description" in validated_data:
|
if "description" in validated_data:
|
||||||
description_obj = common_utils.attach_content(
|
description_obj = common_utils.attach_content(
|
||||||
|
@ -71,14 +80,24 @@ class ChannelUpdateSerializer(serializers.Serializer):
|
||||||
actor_update_fields.append(("summary", description_obj.rendered))
|
actor_update_fields.append(("summary", description_obj.rendered))
|
||||||
|
|
||||||
if "name" in validated_data:
|
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"]))
|
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:
|
if actor_update_fields:
|
||||||
for field, value in actor_update_fields:
|
for field, value in actor_update_fields:
|
||||||
setattr(obj.actor, field, value)
|
setattr(obj.actor, field, value)
|
||||||
obj.actor.save(update_fields=[f for f, _ in actor_update_fields])
|
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
|
return obj
|
||||||
|
|
||||||
def to_representation(self, obj):
|
def to_representation(self, obj):
|
||||||
|
|
|
@ -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),
|
||||||
|
),
|
||||||
|
]
|
|
@ -43,6 +43,13 @@ MAX_LENGTHS = {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ARTIST_CONTENT_CATEGORY_CHOICES = [
|
||||||
|
("music", "music"),
|
||||||
|
("podcast", "podcast"),
|
||||||
|
("other", "other"),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def empty_dict():
|
def empty_dict():
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
@ -237,6 +244,13 @@ class Artist(APIModelMixin):
|
||||||
on_delete=models.SET_NULL,
|
on_delete=models.SET_NULL,
|
||||||
related_name="covered_artist",
|
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
|
api = musicbrainz.api.artists
|
||||||
objects = ArtistQuerySet.as_manager()
|
objects = ArtistQuerySet.as_manager()
|
||||||
|
|
|
@ -119,6 +119,7 @@ class ArtistWithAlbumsSerializer(OptionalDescriptionMixin, serializers.Serialize
|
||||||
fid = serializers.URLField()
|
fid = serializers.URLField()
|
||||||
mbid = serializers.UUIDField()
|
mbid = serializers.UUIDField()
|
||||||
name = serializers.CharField()
|
name = serializers.CharField()
|
||||||
|
content_category = serializers.CharField()
|
||||||
creation_date = serializers.DateTimeField()
|
creation_date = serializers.DateTimeField()
|
||||||
is_local = serializers.BooleanField()
|
is_local = serializers.BooleanField()
|
||||||
cover = cover_field
|
cover = cover_field
|
||||||
|
@ -142,6 +143,7 @@ def serialize_artist_simple(artist):
|
||||||
"name": artist.name,
|
"name": artist.name,
|
||||||
"creation_date": DATETIME_FIELD.to_representation(artist.creation_date),
|
"creation_date": DATETIME_FIELD.to_representation(artist.creation_date),
|
||||||
"is_local": artist.is_local,
|
"is_local": artist.is_local,
|
||||||
|
"content_category": artist.content_category,
|
||||||
}
|
}
|
||||||
if "description" in artist._state.fields_cache:
|
if "description" in artist._state.fields_cache:
|
||||||
data["description"] = (
|
data["description"] = (
|
||||||
|
|
|
@ -14,6 +14,7 @@ def test_channel_serializer_create(factories):
|
||||||
"username": "mychannel",
|
"username": "mychannel",
|
||||||
"description": {"text": "This is my channel", "content_type": "text/markdown"},
|
"description": {"text": "This is my channel", "content_type": "text/markdown"},
|
||||||
"tags": ["hello", "world"],
|
"tags": ["hello", "world"],
|
||||||
|
"content_category": "other",
|
||||||
}
|
}
|
||||||
|
|
||||||
serializer = serializers.ChannelCreateSerializer(data=data)
|
serializer = serializers.ChannelCreateSerializer(data=data)
|
||||||
|
@ -28,6 +29,7 @@ def test_channel_serializer_create(factories):
|
||||||
== data["tags"]
|
== data["tags"]
|
||||||
)
|
)
|
||||||
assert channel.artist.description.text == data["description"]["text"]
|
assert channel.artist.description.text == data["description"]["text"]
|
||||||
|
assert channel.artist.content_category == data["content_category"]
|
||||||
assert (
|
assert (
|
||||||
channel.artist.description.content_type == data["description"]["content_type"]
|
channel.artist.description.content_type == data["description"]["content_type"]
|
||||||
)
|
)
|
||||||
|
@ -49,6 +51,7 @@ def test_channel_serializer_update(factories):
|
||||||
"name": "My channel",
|
"name": "My channel",
|
||||||
"description": {"text": "This is my channel", "content_type": "text/markdown"},
|
"description": {"text": "This is my channel", "content_type": "text/markdown"},
|
||||||
"tags": ["hello", "world"],
|
"tags": ["hello", "world"],
|
||||||
|
"content_category": "other",
|
||||||
}
|
}
|
||||||
|
|
||||||
serializer = serializers.ChannelUpdateSerializer(channel, data=data)
|
serializer = serializers.ChannelUpdateSerializer(channel, data=data)
|
||||||
|
@ -58,6 +61,7 @@ def test_channel_serializer_update(factories):
|
||||||
channel.refresh_from_db()
|
channel.refresh_from_db()
|
||||||
|
|
||||||
assert channel.artist.name == data["name"]
|
assert channel.artist.name == data["name"]
|
||||||
|
assert channel.artist.content_category == data["content_category"]
|
||||||
assert (
|
assert (
|
||||||
sorted(channel.artist.tagged_items.values_list("tag__name", flat=True))
|
sorted(channel.artist.tagged_items.values_list("tag__name", flat=True))
|
||||||
== data["tags"]
|
== data["tags"]
|
||||||
|
|
|
@ -14,6 +14,7 @@ def test_channel_create(logged_in_api_client):
|
||||||
"username": "mychannel",
|
"username": "mychannel",
|
||||||
"description": {"text": "This is my channel", "content_type": "text/markdown"},
|
"description": {"text": "This is my channel", "content_type": "text/markdown"},
|
||||||
"tags": ["hello", "world"],
|
"tags": ["hello", "world"],
|
||||||
|
"content_category": "podcast",
|
||||||
}
|
}
|
||||||
|
|
||||||
url = reverse("api:v1:channels-list")
|
url = reverse("api:v1:channels-list")
|
||||||
|
|
|
@ -66,6 +66,7 @@ def test_artist_with_albums_serializer(factories, to_api_date):
|
||||||
"mbid": str(artist.mbid),
|
"mbid": str(artist.mbid),
|
||||||
"name": artist.name,
|
"name": artist.name,
|
||||||
"is_local": artist.is_local,
|
"is_local": artist.is_local,
|
||||||
|
"content_category": artist.content_category,
|
||||||
"creation_date": to_api_date(artist.creation_date),
|
"creation_date": to_api_date(artist.creation_date),
|
||||||
"albums": [serializers.ArtistAlbumSerializer(album).data],
|
"albums": [serializers.ArtistAlbumSerializer(album).data],
|
||||||
"tags": [],
|
"tags": [],
|
||||||
|
|
Loading…
Reference in New Issue