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):
return []
class PlaylistWriteSerializer(serializers.ModelSerializer):
class Meta:
model = models.Playlist
fields = [
'id',
'name',
'privacy_level',
]
class PlaylistSerializer(serializers.ModelSerializer):
tracks_count = serializers.SerializerMethodField()
user = UserBasicSerializer()
tracks_count = serializers.SerializerMethodField(read_only=True)
user = UserBasicSerializer(read_only=True)
class Meta:
model = models.Playlist
fields = (
'id',
'name',
'user',
'tracks_count',
'privacy_level',
'creation_date',
'modification_date')
'user',
'privacy_level',)
read_only_fields = [
'id',
'user',
'modification_date',
'creation_date',]
......
......@@ -38,11 +38,6 @@ class PlaylistViewSet(
filter_class = filters.PlaylistFilter
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'])
def tracks(self, request, *args, **kwargs):
playlist = self.get_object()
......
......@@ -182,3 +182,16 @@ def test_can_clear_playlist_from_api(
assert response.status_code == 204
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