Merge branch 'issue-1238' into 'develop'

Add number of tracks and discs of an album to API

Closes #1238

See merge request funkwhale/funkwhale!1267
environments/review-docs-devel-1399dq/deployments/6607
Georg Krause 2021-02-20 12:54:14 +01:00
commit fac72acbd7
4 zmienionych plików z 43 dodań i 1 usunięć

Wyświetl plik

@ -20,6 +20,7 @@ from django.db.models.signals import post_save, pre_save
from django.dispatch import receiver from django.dispatch import receiver
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
from django.db.models import Prefetch, Count
from funkwhale_api import musicbrainz from funkwhale_api import musicbrainz
from funkwhale_api.common import fields from funkwhale_api.common import fields
@ -420,7 +421,13 @@ def import_album(v):
class TrackQuerySet(common_models.LocalFromFidQuerySet, models.QuerySet): class TrackQuerySet(common_models.LocalFromFidQuerySet, models.QuerySet):
def for_nested_serialization(self): def for_nested_serialization(self):
return self.prefetch_related( return self.prefetch_related(
"artist", "album__artist", "album__attachment_cover" "artist",
Prefetch(
"album",
queryset=Album.objects.select_related(
"artist", "attachment_cover"
).annotate(_prefetched_tracks_count=Count("tracks")),
),
) )
def annotate_playable_by_actor(self, actor): def annotate_playable_by_actor(self, actor):

Wyświetl plik

@ -227,6 +227,10 @@ class AlbumSerializer(OptionalDescriptionMixin, serializers.Serializer):
class TrackAlbumSerializer(serializers.ModelSerializer): class TrackAlbumSerializer(serializers.ModelSerializer):
artist = serializers.SerializerMethodField() artist = serializers.SerializerMethodField()
cover = cover_field cover = cover_field
tracks_count = serializers.SerializerMethodField()
def get_tracks_count(self, o):
return getattr(o, "_prefetched_tracks_count", len(o.tracks.all()))
class Meta: class Meta:
model = models.Album model = models.Album
@ -240,6 +244,7 @@ class TrackAlbumSerializer(serializers.ModelSerializer):
"cover", "cover",
"creation_date", "creation_date",
"is_local", "is_local",
"tracks_count",
) )
def get_artist(self, o): def get_artist(self, o):

Wyświetl plik

@ -196,6 +196,35 @@ def test_album_serializer(factories, to_api_date):
assert serializer.data == expected assert serializer.data == expected
def test_track_album_serializer(factories, to_api_date):
actor = factories["federation.Actor"]()
track1 = factories["music.Track"](
position=2, album__attributed_to=actor, album__with_cover=True
)
factories["music.Track"](position=1, album=track1.album)
album = track1.album
expected = {
"id": album.id,
"fid": album.fid,
"mbid": str(album.mbid),
"title": album.title,
"artist": serializers.serialize_artist_simple(album.artist),
"creation_date": to_api_date(album.creation_date),
"is_playable": False,
"cover": common_serializers.AttachmentSerializer(album.attachment_cover).data,
"release_date": to_api_date(album.release_date),
"tracks_count": 2,
"is_local": album.is_local,
"tags": [],
"attributed_to": federation_serializers.APIActorSerializer(actor).data,
}
serializer = serializers.AlbumSerializer(
album.__class__.objects.with_tracks_count().get(pk=album.pk)
)
assert serializer.data == expected
def test_track_serializer(factories, to_api_date): def test_track_serializer(factories, to_api_date):
actor = factories["federation.Actor"]() actor = factories["federation.Actor"]()
upload = factories["music.Upload"]( upload = factories["music.Upload"](

Wyświetl plik

@ -0,0 +1 @@
Add number of tracks and discs of an album to API (#1238)