Verified Commit 71d46e43 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Simplified playlist serializer and ensure update return same payload as detail

parent 6a9a34d2
...@@ -62,34 +62,20 @@ class PlaylistTrackWriteSerializer(serializers.ModelSerializer): ...@@ -62,34 +62,20 @@ class PlaylistTrackWriteSerializer(serializers.ModelSerializer):
return [] return []
class PlaylistWriteSerializer(serializers.ModelSerializer):
class Meta:
model = models.Playlist
fields = [
'id',
'name',
'privacy_level',
]
class PlaylistSerializer(serializers.ModelSerializer): class PlaylistSerializer(serializers.ModelSerializer):
tracks_count = serializers.SerializerMethodField() tracks_count = serializers.SerializerMethodField(read_only=True)
user = UserBasicSerializer() user = UserBasicSerializer(read_only=True)
class Meta: class Meta:
model = models.Playlist model = models.Playlist
fields = ( fields = (
'id', 'id',
'name', 'name',
'user',
'tracks_count', 'tracks_count',
'privacy_level', 'user',
'creation_date', 'privacy_level',)
'modification_date')
read_only_fields = [ read_only_fields = [
'id', 'id',
'user',
'modification_date', 'modification_date',
'creation_date',] 'creation_date',]
......
...@@ -38,11 +38,6 @@ class PlaylistViewSet( ...@@ -38,11 +38,6 @@ class PlaylistViewSet(
filter_class = filters.PlaylistFilter filter_class = filters.PlaylistFilter
ordering_fields = ('id', 'name', 'creation_date', 'modification_date') ordering_fields = ('id', 'name', 'creation_date', 'modification_date')
def get_serializer_class(self):
if self.request.method in ['PUT', 'PATCH', 'DELETE', 'POST']:
return serializers.PlaylistWriteSerializer
return self.serializer_class
@detail_route(methods=['get']) @detail_route(methods=['get'])
def tracks(self, request, *args, **kwargs): def tracks(self, request, *args, **kwargs):
playlist = self.get_object() playlist = self.get_object()
......
...@@ -182,3 +182,16 @@ def test_can_clear_playlist_from_api( ...@@ -182,3 +182,16 @@ def test_can_clear_playlist_from_api(
assert response.status_code == 204 assert response.status_code == 204
assert playlist.playlist_tracks.count() == 0 assert playlist.playlist_tracks.count() == 0
def test_update_playlist_from_api(
factories, mocker, logged_in_api_client):
playlist = factories['playlists.Playlist'](user=logged_in_api_client.user)
plts = factories['playlists.PlaylistTrack'].create_batch(
size=5, playlist=playlist)
url = reverse('api:v1:playlists-detail', kwargs={'pk': playlist.pk})
response = logged_in_api_client.patch(url, {'name': 'test'})
playlist.refresh_from_db()
assert response.status_code == 200
assert response.data['user']['username'] == playlist.user.username
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