Resolve "Subsonic API: expose more metadata in getAlbumList endpoint" (#623)
MR !1403 closes #623
This commit is contained in:
parent
6d20b18bcb
commit
d5d7de8590
|
@ -1,6 +1,8 @@
|
||||||
import collections
|
import collections
|
||||||
|
|
||||||
from django.db.models import Count, functions
|
from django.db.models import Count, functions
|
||||||
|
from django.db.models import Sum
|
||||||
|
from django.db.models.expressions import OuterRef, Subquery
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
from funkwhale_api.history import models as history_models
|
from funkwhale_api.history import models as history_models
|
||||||
|
@ -141,16 +143,32 @@ def get_track_data(album, track, upload):
|
||||||
|
|
||||||
|
|
||||||
def get_album2_data(album):
|
def get_album2_data(album):
|
||||||
|
# takes one upload per track
|
||||||
|
subquery = Subquery(
|
||||||
|
music_models.Upload.objects.filter(track_id=OuterRef("id"))
|
||||||
|
.order_by("id")
|
||||||
|
.values("id")[:1]
|
||||||
|
)
|
||||||
payload = {
|
payload = {
|
||||||
"id": album.id,
|
"id": album.id,
|
||||||
"artistId": album.artist.id,
|
"artistId": album.artist.id,
|
||||||
"name": album.title,
|
"name": album.title,
|
||||||
"artist": album.artist.name,
|
"artist": album.artist.name,
|
||||||
"created": to_subsonic_date(album.creation_date),
|
"created": to_subsonic_date(album.creation_date),
|
||||||
|
"duration": album.tracks.filter(uploads__in=subquery).aggregate(
|
||||||
|
d=Sum("uploads__duration")
|
||||||
|
)["d"]
|
||||||
|
or 0,
|
||||||
|
"playCount": album.tracks.aggregate(l=Count("listenings"))["l"] or 0,
|
||||||
}
|
}
|
||||||
if album.attachment_cover_id:
|
if album.attachment_cover_id:
|
||||||
payload["coverArt"] = "al-{}".format(album.id)
|
payload["coverArt"] = "al-{}".format(album.id)
|
||||||
|
if album.tagged_items:
|
||||||
|
# exposes only first genre since the specification uses singular noun
|
||||||
|
first_genre = album.tagged_items.first()
|
||||||
|
payload["genre"] = first_genre.tag.name if first_genre else ""
|
||||||
|
if album.release_date:
|
||||||
|
payload["year"] = album.release_date.year
|
||||||
try:
|
try:
|
||||||
payload["songCount"] = album._tracks_count
|
payload["songCount"] = album._tracks_count
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
|
from django.db.models.aggregates import Count, Sum
|
||||||
|
from django.db.models.expressions import OuterRef, Subquery
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from funkwhale_api.music import models as music_models
|
from funkwhale_api.music import models as music_models
|
||||||
|
@ -171,7 +174,13 @@ def test_get_album_serializer(factories):
|
||||||
album = factories["music.Album"](artist=artist, with_cover=True)
|
album = factories["music.Album"](artist=artist, with_cover=True)
|
||||||
track = factories["music.Track"](album=album, disc_number=42)
|
track = factories["music.Track"](album=album, disc_number=42)
|
||||||
upload = factories["music.Upload"](track=track, bitrate=42000, duration=43, size=44)
|
upload = factories["music.Upload"](track=track, bitrate=42000, duration=43, size=44)
|
||||||
|
tagged_item = factories["tags.TaggedItem"](content_object=album, tag__name="foo")
|
||||||
|
# takes one upload per track
|
||||||
|
subquery = Subquery(
|
||||||
|
music_models.Upload.objects.filter(track_id=OuterRef("id"))
|
||||||
|
.order_by("id")
|
||||||
|
.values("id")[:1]
|
||||||
|
)
|
||||||
expected = {
|
expected = {
|
||||||
"id": album.pk,
|
"id": album.pk,
|
||||||
"artistId": artist.pk,
|
"artistId": artist.pk,
|
||||||
|
@ -181,6 +190,12 @@ def test_get_album_serializer(factories):
|
||||||
"created": serializers.to_subsonic_date(album.creation_date),
|
"created": serializers.to_subsonic_date(album.creation_date),
|
||||||
"year": album.release_date.year,
|
"year": album.release_date.year,
|
||||||
"coverArt": "al-{}".format(album.id),
|
"coverArt": "al-{}".format(album.id),
|
||||||
|
"genre": tagged_item.tag.name,
|
||||||
|
"duration": album.tracks.filter(uploads__in=subquery).aggregate(
|
||||||
|
d=Sum("uploads__duration")
|
||||||
|
)["d"]
|
||||||
|
or 0,
|
||||||
|
"playCount": album.tracks.aggregate(l=Count("listenings"))["l"] or 0,
|
||||||
"song": [
|
"song": [
|
||||||
{
|
{
|
||||||
"id": track.pk,
|
"id": track.pk,
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Exposes more metadata in Subsonic's getAlbumList endpoint (#623)
|
Loading…
Reference in New Issue