mirror of
https://code.eliotberriot.com/funkwhale/funkwhale.git
synced 2025-10-06 02:49:56 +02:00
See #224: less clutered music API, better serializers
This commit is contained in:
parent
28236ef7a7
commit
857fab526d
14 changed files with 328 additions and 132 deletions
|
@ -68,13 +68,8 @@ class TagViewSetMixin(object):
|
|||
|
||||
|
||||
class ArtistViewSet(SearchMixin, viewsets.ReadOnlyModelViewSet):
|
||||
queryset = (
|
||||
models.Artist.objects.all()
|
||||
.prefetch_related(
|
||||
'albums__tracks__files',
|
||||
'albums__tracks__artist',
|
||||
'albums__tracks__tags'))
|
||||
serializer_class = serializers.ArtistSerializerNested
|
||||
queryset = models.Artist.objects.with_albums()
|
||||
serializer_class = serializers.ArtistWithAlbumsSerializer
|
||||
permission_classes = [ConditionalAuthentication]
|
||||
search_fields = ['name__unaccent']
|
||||
filter_class = filters.ArtistFilter
|
||||
|
@ -88,7 +83,7 @@ class AlbumViewSet(SearchMixin, viewsets.ReadOnlyModelViewSet):
|
|||
.select_related()
|
||||
.prefetch_related('tracks__tags',
|
||||
'tracks__files'))
|
||||
serializer_class = serializers.AlbumSerializerNested
|
||||
serializer_class = serializers.AlbumSerializer
|
||||
permission_classes = [ConditionalAuthentication]
|
||||
search_fields = ['title__unaccent']
|
||||
ordering_fields = ('creation_date',)
|
||||
|
@ -166,7 +161,7 @@ class TrackViewSet(
|
|||
A simple ViewSet for viewing and editing accounts.
|
||||
"""
|
||||
queryset = (models.Track.objects.all().for_nested_serialization())
|
||||
serializer_class = serializers.TrackSerializerNested
|
||||
serializer_class = serializers.TrackSerializer
|
||||
permission_classes = [ConditionalAuthentication]
|
||||
search_fields = ['title', 'artist__name']
|
||||
ordering_fields = (
|
||||
|
@ -370,10 +365,10 @@ class Search(views.APIView):
|
|||
def get(self, request, *args, **kwargs):
|
||||
query = request.GET['query']
|
||||
results = {
|
||||
'tags': serializers.TagSerializer(self.get_tags(query), many=True).data,
|
||||
'artists': serializers.ArtistSerializerNested(self.get_artists(query), many=True).data,
|
||||
'tracks': serializers.TrackSerializerNested(self.get_tracks(query), many=True).data,
|
||||
'albums': serializers.AlbumSerializerNested(self.get_albums(query), many=True).data,
|
||||
# 'tags': serializers.TagSerializer(self.get_tags(query), many=True).data,
|
||||
'artists': serializers.ArtistWithAlbumsSerializer(self.get_artists(query), many=True).data,
|
||||
'tracks': serializers.TrackSerializer(self.get_tracks(query), many=True).data,
|
||||
'albums': serializers.AlbumSerializer(self.get_albums(query), many=True).data,
|
||||
}
|
||||
return Response(results, status=200)
|
||||
|
||||
|
@ -387,14 +382,10 @@ class Search(views.APIView):
|
|||
return (
|
||||
models.Track.objects.all()
|
||||
.filter(query_obj)
|
||||
.select_related('album__artist')
|
||||
.prefetch_related(
|
||||
'tags',
|
||||
'artist__albums__tracks__tags',
|
||||
'files')
|
||||
.select_related('artist', 'album__artist')
|
||||
.prefetch_related('files')
|
||||
)[:self.max_results]
|
||||
|
||||
|
||||
def get_albums(self, query):
|
||||
search_fields = [
|
||||
'mbid',
|
||||
|
@ -406,27 +397,19 @@ class Search(views.APIView):
|
|||
.filter(query_obj)
|
||||
.select_related()
|
||||
.prefetch_related(
|
||||
'tracks__tags',
|
||||
'tracks__files',
|
||||
)
|
||||
)
|
||||
)[:self.max_results]
|
||||
|
||||
|
||||
def get_artists(self, query):
|
||||
search_fields = ['mbid', 'name__unaccent']
|
||||
query_obj = utils.get_query(query, search_fields)
|
||||
return (
|
||||
models.Artist.objects.all()
|
||||
.filter(query_obj)
|
||||
.select_related()
|
||||
.prefetch_related(
|
||||
'albums__tracks__tags',
|
||||
'albums__tracks__files',
|
||||
)
|
||||
|
||||
.with_albums()
|
||||
)[:self.max_results]
|
||||
|
||||
|
||||
def get_tags(self, query):
|
||||
search_fields = ['slug', 'name__unaccent']
|
||||
query_obj = utils.get_query(query, search_fields)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue