Verified Commit 944135e7 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Capped number of tracks in playlists

parent d8486bee
......@@ -383,3 +383,6 @@ CACHEOPS = {
# Custom Admin URL, use {% url 'admin:index' %}
ADMIN_URL = env('DJANGO_ADMIN_URL', default='^api/admin/')
CSRF_USE_SESSIONS = True
# Playlist settings
PLAYLISTS_MAX_TRACKS = env.int('PLAYLISTS_MAX_TRACKS', default=500)
from django.conf import settings
from rest_framework import serializers
from taggit.models import Tag
......@@ -20,6 +21,14 @@ class PlaylistTrackCreateSerializer(serializers.ModelSerializer):
model = models.PlaylistTrack
fields = ('id', 'track', 'playlist', 'position')
def validate_playlist(self, value):
existing = value.playlist_tracks.count()
if existing >= settings.PLAYLISTS_MAX_TRACKS:
raise serializers.ValidationError(
'Playlist has reached the maximum of {} tracks'.format(
settings.PLAYLISTS_MAX_TRACKS))
return value
class PlaylistSerializer(serializers.ModelSerializer):
......
from funkwhale_api.playlists import serializers
def test_cannot_max_500_tracks_per_playlist(mocker, factories, settings):
settings.PLAYLISTS_MAX_TRACKS = 2
playlist = factories['playlists.Playlist']()
plts = factories['playlists.PlaylistTrack'].create_batch(
size=2, playlist=playlist)
track = factories['music.Track']()
serializer = serializers.PlaylistTrackCreateSerializer(data={
'playlist': playlist.pk,
'track': track.pk,
})
assert serializer.is_valid() is False
assert 'playlist' in serializer.errors
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