Verified Commit d8486bee authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Can now list tracks from within playlist endpoint

parent 9a909798
......@@ -22,9 +22,8 @@ class PlaylistTrackCreateSerializer(serializers.ModelSerializer):
class PlaylistSerializer(serializers.ModelSerializer):
playlist_tracks = PlaylistTrackSerializer(many=True, read_only=True)
class Meta:
model = models.Playlist
fields = ('id', 'name', 'privacy_level', 'creation_date', 'playlist_tracks')
read_only_fields = ['id', 'playlist_tracks', 'creation_date']
fields = ('id', 'name', 'privacy_level', 'creation_date')
read_only_fields = ['id', 'creation_date']
from rest_framework import generics, mixins, viewsets
from rest_framework import status
from rest_framework.decorators import detail_route
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticatedOrReadOnly
......@@ -27,6 +28,17 @@ class PlaylistViewSet(
IsAuthenticatedOrReadOnly,
]
@detail_route(methods=['get'])
def tracks(self, request, *args, **kwargs):
playlist = self.get_object()
plts = playlist.playlist_tracks.all()
serializer = serializers.PlaylistTrackSerializer(plts, many=True)
data = {
'count': len(plts),
'result': serializer.data
}
return Response(data, status=200)
def get_queryset(self):
return self.queryset.filter(
fields.privacy_level_query(self.request.user))
......@@ -36,7 +48,6 @@ class PlaylistViewSet(
user=self.request.user,
privacy_level=serializer.validated_data.get(
'privacy_level', self.request.user.privacy_level)
)
......
......@@ -30,7 +30,6 @@ def test_owner_permission_owner_field_not_ok(nodb_factories, api_request):
permission.has_object_permission(request, view, playlist)
def test_owner_permission_read_only(nodb_factories, api_request):
playlist = nodb_factories['playlists.Playlist']()
view = APIView.as_view()
......
......@@ -6,7 +6,7 @@ from django.core.exceptions import ValidationError
from django.utils import timezone
from funkwhale_api.playlists import models
from funkwhale_api.playlists.serializers import PlaylistSerializer
from funkwhale_api.playlists import serializers
def test_can_create_playlist_via_api(logged_in_api_client):
......@@ -48,6 +48,7 @@ def test_can_add_playlist_track_via_api(factories, logged_in_api_client):
}
response = logged_in_api_client.post(url, data)
assert response.status_code == 201
plts = logged_in_api_client.user.playlists.latest('id').playlist_tracks.all()
assert plts.first().track == tracks[0]
......@@ -114,3 +115,16 @@ def test_playlist_track_privacy_respected_in_list_anon(
response = api_client.get(url)
assert response.data['count'] == 0
@pytest.mark.parametrize('level', ['instance', 'me', 'followers'])
def test_can_list_tracks_from_playlist(
level, factories, logged_in_api_client):
plt = factories['playlists.PlaylistTrack'](
playlist__user=logged_in_api_client.user)
url = reverse('api:v1:playlists-tracks', kwargs={'pk': plt.playlist.pk})
response = logged_in_api_client.get(url)
serialized_plt = serializers.PlaylistTrackSerializer(plt).data
assert response.data['count'] == 1
assert response.data['result'][0] == serialized_plt
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment