Skip to content
Snippets Groups Projects
Verified Commit 7222f7b7 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

See #186: moved PLAYLISTS_MAX_TRACKS to playlists__max_tracks

parent aa3da412
No related branches found
No related tags found
No related merge requests found
...@@ -433,6 +433,7 @@ ADMIN_URL = env('DJANGO_ADMIN_URL', default='^api/admin/') ...@@ -433,6 +433,7 @@ ADMIN_URL = env('DJANGO_ADMIN_URL', default='^api/admin/')
CSRF_USE_SESSIONS = True CSRF_USE_SESSIONS = True
# Playlist settings # Playlist settings
# XXX: Deprecated, use playlists__max_tracks instead
PLAYLISTS_MAX_TRACKS = env.int('PLAYLISTS_MAX_TRACKS', default=250) PLAYLISTS_MAX_TRACKS = env.int('PLAYLISTS_MAX_TRACKS', default=250)
ACCOUNT_USERNAME_BLACKLIST = [ ACCOUNT_USERNAME_BLACKLIST = [
......
from dynamic_preferences import types
from dynamic_preferences.registries import global_preferences_registry
from funkwhale_api.common import preferences
playlists = types.Section('playlists')
@global_preferences_registry.register
class MaxTracks(preferences.DefaultFromSettingMixin, types.IntegerPreference):
show_in_api = True
section = playlists
name = 'max_tracks'
verbose_name = 'Max tracks per playlist'
setting = 'PLAYLISTS_MAX_TRACKS'
...@@ -6,6 +6,7 @@ from django.utils import timezone ...@@ -6,6 +6,7 @@ from django.utils import timezone
from rest_framework import exceptions from rest_framework import exceptions
from funkwhale_api.common import fields from funkwhale_api.common import fields
from funkwhale_api.common import preferences
class Playlist(models.Model): class Playlist(models.Model):
...@@ -81,10 +82,11 @@ class Playlist(models.Model): ...@@ -81,10 +82,11 @@ class Playlist(models.Model):
existing = self.playlist_tracks.select_for_update() existing = self.playlist_tracks.select_for_update()
now = timezone.now() now = timezone.now()
total = existing.filter(index__isnull=False).count() total = existing.filter(index__isnull=False).count()
if existing.count() + len(tracks) > settings.PLAYLISTS_MAX_TRACKS: max_tracks = preferences.get('playlists__max_tracks')
if existing.count() + len(tracks) > max_tracks:
raise exceptions.ValidationError( raise exceptions.ValidationError(
'Playlist would reach the maximum of {} tracks'.format( 'Playlist would reach the maximum of {} tracks'.format(
settings.PLAYLISTS_MAX_TRACKS)) max_tracks))
self.save(update_fields=['modification_date']) self.save(update_fields=['modification_date'])
start = total start = total
plts = [ plts = [
......
...@@ -3,6 +3,7 @@ from django.db import transaction ...@@ -3,6 +3,7 @@ from django.db import transaction
from rest_framework import serializers from rest_framework import serializers
from taggit.models import Tag from taggit.models import Tag
from funkwhale_api.common import preferences
from funkwhale_api.music.models import Track from funkwhale_api.music.models import Track
from funkwhale_api.music.serializers import TrackSerializerNested from funkwhale_api.music.serializers import TrackSerializerNested
from funkwhale_api.users.serializers import UserBasicSerializer from funkwhale_api.users.serializers import UserBasicSerializer
...@@ -32,10 +33,11 @@ class PlaylistTrackWriteSerializer(serializers.ModelSerializer): ...@@ -32,10 +33,11 @@ class PlaylistTrackWriteSerializer(serializers.ModelSerializer):
raise serializers.ValidationError( raise serializers.ValidationError(
'You do not have the permission to edit this playlist') 'You do not have the permission to edit this playlist')
existing = value.playlist_tracks.count() existing = value.playlist_tracks.count()
if existing >= settings.PLAYLISTS_MAX_TRACKS: max_tracks = preferences.get('playlists__max_tracks')
if existing >= max_tracks:
raise serializers.ValidationError( raise serializers.ValidationError(
'Playlist has reached the maximum of {} tracks'.format( 'Playlist has reached the maximum of {} tracks'.format(
settings.PLAYLISTS_MAX_TRACKS)) max_tracks))
return value return value
@transaction.atomic @transaction.atomic
......
...@@ -116,8 +116,8 @@ def test_can_insert_many(factories): ...@@ -116,8 +116,8 @@ def test_can_insert_many(factories):
assert plt.playlist == playlist assert plt.playlist == playlist
def test_insert_many_honor_max_tracks(factories, settings): def test_insert_many_honor_max_tracks(preferences, factories):
settings.PLAYLISTS_MAX_TRACKS = 4 preferences['playlists__max_tracks'] = 4
playlist = factories['playlists.Playlist']() playlist = factories['playlists.Playlist']()
plts = factories['playlists.PlaylistTrack'].create_batch( plts = factories['playlists.PlaylistTrack'].create_batch(
size=2, playlist=playlist) size=2, playlist=playlist)
......
...@@ -2,8 +2,8 @@ from funkwhale_api.playlists import models ...@@ -2,8 +2,8 @@ from funkwhale_api.playlists import models
from funkwhale_api.playlists import serializers from funkwhale_api.playlists import serializers
def test_cannot_max_500_tracks_per_playlist(factories, settings): def test_cannot_max_500_tracks_per_playlist(factories, preferences):
settings.PLAYLISTS_MAX_TRACKS = 2 preferences['playlists__max_tracks'] = 2
playlist = factories['playlists.Playlist']() playlist = factories['playlists.Playlist']()
plts = factories['playlists.PlaylistTrack'].create_batch( plts = factories['playlists.PlaylistTrack'].create_batch(
size=2, playlist=playlist) size=2, playlist=playlist)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment