2018-03-19 14:28:33 +00:00
|
|
|
from django.db import transaction
|
2017-06-23 21:00:42 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
2018-04-28 03:30:23 +00:00
|
|
|
from funkwhale_api.common import preferences
|
2018-03-20 18:56:42 +00:00
|
|
|
from funkwhale_api.music.models import Track
|
2018-05-22 19:10:48 +00:00
|
|
|
from funkwhale_api.music.serializers import TrackSerializer
|
2018-03-20 18:56:42 +00:00
|
|
|
from funkwhale_api.users.serializers import UserBasicSerializer
|
2018-06-10 08:55:16 +00:00
|
|
|
|
2017-06-23 21:00:42 +00:00
|
|
|
from . import models
|
|
|
|
|
|
|
|
|
|
|
|
class PlaylistTrackSerializer(serializers.ModelSerializer):
|
2018-05-22 19:10:48 +00:00
|
|
|
track = TrackSerializer()
|
2017-06-23 21:00:42 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = models.PlaylistTrack
|
2018-06-09 13:36:16 +00:00
|
|
|
fields = ("id", "track", "playlist", "index", "creation_date")
|
2017-06-23 21:00:42 +00:00
|
|
|
|
|
|
|
|
2018-03-19 14:28:33 +00:00
|
|
|
class PlaylistTrackWriteSerializer(serializers.ModelSerializer):
|
2018-06-09 13:36:16 +00:00
|
|
|
index = serializers.IntegerField(required=False, min_value=0, allow_null=True)
|
2017-06-23 21:00:42 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = models.PlaylistTrack
|
2018-06-09 13:36:16 +00:00
|
|
|
fields = ("id", "track", "playlist", "index")
|
2017-06-23 21:00:42 +00:00
|
|
|
|
2018-03-19 11:36:15 +00:00
|
|
|
def validate_playlist(self, value):
|
2018-06-09 13:36:16 +00:00
|
|
|
if self.context.get("request"):
|
2018-03-19 14:28:33 +00:00
|
|
|
# validate proper ownership on the playlist
|
2018-06-09 13:36:16 +00:00
|
|
|
if self.context["request"].user != value.user:
|
2018-03-19 14:28:33 +00:00
|
|
|
raise serializers.ValidationError(
|
2018-06-09 13:36:16 +00:00
|
|
|
"You do not have the permission to edit this playlist"
|
|
|
|
)
|
2018-03-19 11:36:15 +00:00
|
|
|
existing = value.playlist_tracks.count()
|
2018-06-09 13:36:16 +00:00
|
|
|
max_tracks = preferences.get("playlists__max_tracks")
|
2018-04-28 03:30:23 +00:00
|
|
|
if existing >= max_tracks:
|
2018-03-19 11:36:15 +00:00
|
|
|
raise serializers.ValidationError(
|
2018-06-09 13:36:16 +00:00
|
|
|
"Playlist has reached the maximum of {} tracks".format(max_tracks)
|
|
|
|
)
|
2018-03-19 11:36:15 +00:00
|
|
|
return value
|
|
|
|
|
2018-03-19 14:28:33 +00:00
|
|
|
@transaction.atomic
|
|
|
|
def create(self, validated_data):
|
2018-06-09 13:36:16 +00:00
|
|
|
index = validated_data.pop("index", None)
|
2018-03-19 14:28:33 +00:00
|
|
|
instance = super().create(validated_data)
|
|
|
|
instance.playlist.insert(instance, index)
|
|
|
|
return instance
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def update(self, instance, validated_data):
|
2018-06-09 13:36:16 +00:00
|
|
|
update_index = "index" in validated_data
|
|
|
|
index = validated_data.pop("index", None)
|
2018-03-19 14:28:33 +00:00
|
|
|
super().update(instance, validated_data)
|
|
|
|
if update_index:
|
|
|
|
instance.playlist.insert(instance, index)
|
|
|
|
return instance
|
|
|
|
|
|
|
|
def get_unique_together_validators(self):
|
|
|
|
"""
|
|
|
|
We explicitely disable unique together validation here
|
|
|
|
because it collides with our internal logic
|
|
|
|
"""
|
|
|
|
return []
|
|
|
|
|
2017-06-23 21:00:42 +00:00
|
|
|
|
|
|
|
class PlaylistSerializer(serializers.ModelSerializer):
|
2018-03-21 18:03:57 +00:00
|
|
|
tracks_count = serializers.SerializerMethodField(read_only=True)
|
|
|
|
user = UserBasicSerializer(read_only=True)
|
2017-06-23 21:00:42 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = models.Playlist
|
2018-03-19 18:07:45 +00:00
|
|
|
fields = (
|
2018-06-09 13:36:16 +00:00
|
|
|
"id",
|
|
|
|
"name",
|
|
|
|
"tracks_count",
|
|
|
|
"user",
|
|
|
|
"modification_date",
|
|
|
|
"creation_date",
|
|
|
|
"privacy_level",
|
|
|
|
)
|
|
|
|
read_only_fields = ["id", "modification_date", "creation_date"]
|
2018-03-19 18:07:45 +00:00
|
|
|
|
|
|
|
def get_tracks_count(self, obj):
|
|
|
|
try:
|
|
|
|
return obj.tracks_count
|
|
|
|
except AttributeError:
|
|
|
|
# no annotation?
|
|
|
|
return obj.playlist_tracks.count()
|
2018-03-20 18:56:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PlaylistAddManySerializer(serializers.Serializer):
|
|
|
|
tracks = serializers.PrimaryKeyRelatedField(
|
2018-06-09 13:36:16 +00:00
|
|
|
many=True, queryset=Track.objects.for_nested_serialization()
|
|
|
|
)
|