Move Subsonic getArtistInfo2 serialization to serializer
Also fixed JSON serialization by not using lists for the single value fields. Part-of: <https://dev.funkwhale.audio/funkwhale/funkwhale/-/merge_requests/2695>
This commit is contained in:
parent
a5ee48818e
commit
136f24a917
|
@ -7,6 +7,8 @@ from funkwhale_api.history import models as history_models
|
|||
from funkwhale_api.music import models as music_models
|
||||
from funkwhale_api.music import utils as music_utils
|
||||
|
||||
from .renderers import TagValue
|
||||
|
||||
|
||||
def to_subsonic_date(date):
|
||||
"""
|
||||
|
@ -103,6 +105,23 @@ class GetArtistSerializer(serializers.Serializer):
|
|||
return payload
|
||||
|
||||
|
||||
class GetArtistInfo2Serializer(serializers.Serializer):
|
||||
def to_representation(self, artist):
|
||||
payload = {}
|
||||
if artist.mbid:
|
||||
payload["musicBrainzId"] = TagValue(artist.mbid)
|
||||
if artist.attachment_cover:
|
||||
payload["mediumImageUrl"] = TagValue(
|
||||
artist.attachment_cover.download_url_medium_square_crop
|
||||
)
|
||||
payload["largeImageUrl"] = TagValue(
|
||||
artist.attachment_cover.download_url_large_square_crop
|
||||
)
|
||||
if artist.description:
|
||||
payload["biography"] = TagValue(artist.description.rendered)
|
||||
return payload
|
||||
|
||||
|
||||
def get_track_data(album, track, upload):
|
||||
data = {
|
||||
"id": track.pk,
|
||||
|
|
|
@ -269,19 +269,8 @@ class SubsonicViewSet(viewsets.GenericViewSet):
|
|||
@find_object(music_models.Artist.objects.all(), filter_playable=True)
|
||||
def get_artist_info2(self, request, *args, **kwargs):
|
||||
artist = kwargs.pop("obj")
|
||||
artist_info = {}
|
||||
if artist.mbid:
|
||||
artist_info["musicBrainzId"] = [str(artist.mbid)]
|
||||
if artist.attachment_cover:
|
||||
artist_info["mediumImageUrl"] = [
|
||||
artist.attachment_cover.download_url_medium_square_crop
|
||||
]
|
||||
artist_info["largeImageUrl"] = [
|
||||
artist.attachment_cover.download_url_large_square_crop
|
||||
]
|
||||
if artist.description:
|
||||
artist_info["biography"] = [artist.description.rendered]
|
||||
payload = {"artistInfo2": artist_info}
|
||||
data = serializers.GetArtistInfo2Serializer(artist).data
|
||||
payload = {"artistInfo2": data}
|
||||
|
||||
return response.Response(payload, status=200)
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ import pytest
|
|||
from django.db.models.aggregates import Count
|
||||
|
||||
from funkwhale_api.music import models as music_models
|
||||
from funkwhale_api.subsonic import serializers
|
||||
from funkwhale_api.subsonic import renderers, serializers
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
@ -150,6 +150,24 @@ def test_get_artist_serializer(factories):
|
|||
assert serializers.GetArtistSerializer(artist).data == expected
|
||||
|
||||
|
||||
def test_get_artist_info_2_serializer(factories):
|
||||
content = factories["common.Content"]()
|
||||
artist = factories["music.Artist"](with_cover=True, description=content)
|
||||
|
||||
expected = {
|
||||
"musicBrainzId": artist.mbid,
|
||||
"mediumImageUrl": renderers.TagValue(
|
||||
artist.attachment_cover.download_url_medium_square_crop
|
||||
),
|
||||
"largeImageUrl": renderers.TagValue(
|
||||
artist.attachment_cover.download_url_large_square_crop
|
||||
),
|
||||
"biography": renderers.TagValue(artist.description.rendered),
|
||||
}
|
||||
|
||||
assert serializers.GetArtistInfo2Serializer(artist).data == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"mimetype, extension, expected",
|
||||
[
|
||||
|
|
Loading…
Reference in New Issue