mirror of
https://code.eliotberriot.com/funkwhale/funkwhale.git
synced 2025-10-04 23:29:17 +02:00
Fix #551: Added a library widget to display libraries associated with a track, album and artist
This commit is contained in:
parent
f2812c67ce
commit
a865fcdcf1
9 changed files with 210 additions and 10 deletions
|
@ -3,7 +3,7 @@ import urllib
|
|||
|
||||
from django.conf import settings
|
||||
from django.db import transaction
|
||||
from django.db.models import Count, Prefetch, Sum, F
|
||||
from django.db.models import Count, Prefetch, Sum, F, Q
|
||||
from django.db.models.functions import Length
|
||||
from django.utils import timezone
|
||||
|
||||
|
@ -26,6 +26,28 @@ from . import filters, models, serializers, tasks, utils
|
|||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_libraries(filter_uploads):
|
||||
def view(self, request, *args, **kwargs):
|
||||
obj = self.get_object()
|
||||
actor = utils.get_actor_from_request(request)
|
||||
uploads = models.Upload.objects.all()
|
||||
uploads = filter_uploads(obj, uploads)
|
||||
uploads = uploads.playable_by(actor)
|
||||
libraries = models.Library.objects.filter(
|
||||
pk__in=uploads.values_list("library", flat=True)
|
||||
)
|
||||
libraries = libraries.select_related("actor")
|
||||
page = self.paginate_queryset(libraries)
|
||||
if page is not None:
|
||||
serializer = federation_api_serializers.LibrarySerializer(page, many=True)
|
||||
return self.get_paginated_response(serializer.data)
|
||||
|
||||
serializer = federation_api_serializers.LibrarySerializer(libraries, many=True)
|
||||
return Response(serializer.data)
|
||||
|
||||
return view
|
||||
|
||||
|
||||
class TagViewSetMixin(object):
|
||||
def get_queryset(self):
|
||||
queryset = super().get_queryset()
|
||||
|
@ -50,6 +72,14 @@ class ArtistViewSet(viewsets.ReadOnlyModelViewSet):
|
|||
)
|
||||
return queryset.prefetch_related(Prefetch("albums", queryset=albums)).distinct()
|
||||
|
||||
libraries = detail_route(methods=["get"])(
|
||||
get_libraries(
|
||||
filter_uploads=lambda o, uploads: uploads.filter(
|
||||
Q(track__artist=o) | Q(track__album__artist=o)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class AlbumViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
queryset = (
|
||||
|
@ -76,6 +106,10 @@ class AlbumViewSet(viewsets.ReadOnlyModelViewSet):
|
|||
qs = queryset.prefetch_related(Prefetch("tracks", queryset=tracks))
|
||||
return qs.distinct()
|
||||
|
||||
libraries = detail_route(methods=["get"])(
|
||||
get_libraries(filter_uploads=lambda o, uploads: uploads.filter(track__album=o))
|
||||
)
|
||||
|
||||
|
||||
class LibraryViewSet(
|
||||
mixins.CreateModelMixin,
|
||||
|
@ -197,6 +231,10 @@ class TrackViewSet(TagViewSetMixin, viewsets.ReadOnlyModelViewSet):
|
|||
serializer = serializers.LyricsSerializer(lyrics)
|
||||
return Response(serializer.data)
|
||||
|
||||
libraries = detail_route(methods=["get"])(
|
||||
get_libraries(filter_uploads=lambda o, uploads: uploads.filter(track=o))
|
||||
)
|
||||
|
||||
|
||||
def get_file_path(audio_file):
|
||||
serve_path = settings.MUSIC_DIRECTORY_SERVE_PATH
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue