kopia lustrzana https://dev.funkwhale.audio/funkwhale/funkwhale
Merge branch '170-artist-include-channel-data' into 'develop'
See #170: include channel info in artist representations See merge request funkwhale/funkwhale!1013merge-requests/1042/head
commit
5a855b36a1
|
@ -21,7 +21,10 @@ class ChannelFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory):
|
|||
privacy_level="everyone",
|
||||
)
|
||||
actor = factory.LazyAttribute(set_actor)
|
||||
artist = factory.SubFactory(music_factories.ArtistFactory)
|
||||
artist = factory.SubFactory(
|
||||
music_factories.ArtistFactory,
|
||||
attributed_to=factory.SelfAttribute("..attributed_to"),
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = "audio.Channel"
|
||||
|
|
|
@ -273,6 +273,12 @@ class Artist(APIModelMixin):
|
|||
def cover(self):
|
||||
return self.attachment_cover
|
||||
|
||||
def get_channel(self):
|
||||
try:
|
||||
return self.channel
|
||||
except ObjectDoesNotExist:
|
||||
return None
|
||||
|
||||
|
||||
def import_artist(v):
|
||||
a = Artist.get_or_create_from_api(mbid=v[0]["artist"]["id"])[0]
|
||||
|
|
|
@ -103,6 +103,7 @@ class ArtistWithAlbumsSerializer(OptionalDescriptionMixin, serializers.Serialize
|
|||
albums = ArtistAlbumSerializer(many=True)
|
||||
tags = serializers.SerializerMethodField()
|
||||
attributed_to = serializers.SerializerMethodField()
|
||||
channel = serializers.SerializerMethodField()
|
||||
tracks_count = serializers.SerializerMethodField()
|
||||
id = serializers.IntegerField()
|
||||
fid = serializers.URLField()
|
||||
|
@ -123,6 +124,20 @@ class ArtistWithAlbumsSerializer(OptionalDescriptionMixin, serializers.Serialize
|
|||
tracks = getattr(o, "_prefetched_tracks", None)
|
||||
return len(tracks) if tracks else None
|
||||
|
||||
def get_channel(self, o):
|
||||
channel = o.get_channel()
|
||||
if not channel:
|
||||
return
|
||||
|
||||
return {
|
||||
"uuid": str(channel.uuid),
|
||||
"actor": {
|
||||
"full_username": channel.actor.full_username,
|
||||
"preferred_username": channel.actor.preferred_username,
|
||||
"domain": channel.actor.domain_id,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def serialize_artist_simple(artist):
|
||||
data = {
|
||||
|
|
|
@ -115,11 +115,12 @@ class ArtistViewSet(
|
|||
models.Artist.objects.all()
|
||||
.prefetch_related("attributed_to", "attachment_cover")
|
||||
.prefetch_related(
|
||||
"channel__actor",
|
||||
Prefetch(
|
||||
"tracks",
|
||||
queryset=models.Track.objects.all(),
|
||||
to_attr="_prefetched_tracks",
|
||||
)
|
||||
),
|
||||
)
|
||||
.order_by("-id")
|
||||
)
|
||||
|
@ -752,6 +753,7 @@ class Search(views.APIView):
|
|||
models.Artist.objects.all()
|
||||
.filter(query_obj)
|
||||
.with_albums()
|
||||
.prefetch_related("channel__actor")
|
||||
.select_related("attributed_to")
|
||||
)
|
||||
return common_utils.order_for_search(qs, "name")[: self.max_results]
|
||||
|
|
|
@ -73,6 +73,41 @@ def test_artist_with_albums_serializer(factories, to_api_date):
|
|||
"attributed_to": federation_serializers.APIActorSerializer(actor).data,
|
||||
"tracks_count": 42,
|
||||
"cover": common_serializers.AttachmentSerializer(artist.attachment_cover).data,
|
||||
"channel": None,
|
||||
}
|
||||
serializer = serializers.ArtistWithAlbumsSerializer(artist)
|
||||
assert serializer.data == expected
|
||||
|
||||
|
||||
def test_artist_with_albums_serializer_channel(factories, to_api_date):
|
||||
actor = factories["federation.Actor"]()
|
||||
channel = factories["audio.Channel"](attributed_to=actor)
|
||||
track = factories["music.Track"](album__artist=channel.artist)
|
||||
artist = track.artist
|
||||
artist = artist.__class__.objects.with_albums().get(pk=artist.pk)
|
||||
album = list(artist.albums.all())[0]
|
||||
setattr(artist, "_prefetched_tracks", range(42))
|
||||
expected = {
|
||||
"id": artist.id,
|
||||
"fid": artist.fid,
|
||||
"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": [],
|
||||
"attributed_to": federation_serializers.APIActorSerializer(actor).data,
|
||||
"tracks_count": 42,
|
||||
"cover": common_serializers.AttachmentSerializer(artist.attachment_cover).data,
|
||||
"channel": {
|
||||
"uuid": str(channel.uuid),
|
||||
"actor": {
|
||||
"full_username": channel.actor.full_username,
|
||||
"preferred_username": channel.actor.preferred_username,
|
||||
"domain": channel.actor.domain_id,
|
||||
},
|
||||
},
|
||||
}
|
||||
serializer = serializers.ArtistWithAlbumsSerializer(artist)
|
||||
assert serializer.data == expected
|
||||
|
|
Ładowanie…
Reference in New Issue