60 lines
1.9 KiB
Python
60 lines
1.9 KiB
Python
from rest_framework import serializers
|
|
from .models import Song, FavoriteSong, PlayList, Podkast
|
|
|
|
class SongSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = Song
|
|
fields = ('id', 'unique_id', 'azura_id', 'title', 'artist', 'album', 'genre', 'art')
|
|
|
|
class PlayListSerializer(serializers.ModelSerializer):
|
|
def validate(self, attrs):
|
|
|
|
if PlayList.objects.filter(
|
|
name=attrs.get('name'),
|
|
user=attrs.get('user')
|
|
).exists():
|
|
raise serializers.ValidationError(
|
|
{
|
|
'playlist': ['Данный плейлист уже существует']
|
|
})
|
|
return super().validate(attrs)
|
|
|
|
class Meta:
|
|
model = PlayList
|
|
fields = ('id', 'name', 'song', 'user', 'playlist_art')
|
|
|
|
def to_representation(self, instance):
|
|
rep = super().to_representation(instance)
|
|
rep["song"] = SongSerializer(
|
|
instance.song.all(), many=True).data
|
|
return rep
|
|
|
|
class FavoriteSongSerializer(serializers.ModelSerializer):
|
|
|
|
def validate(self, attrs):
|
|
if FavoriteSong.objects.filter(
|
|
song=attrs.get('song'),
|
|
user=attrs.get('user')
|
|
).exists():
|
|
raise serializers.ValidationError(
|
|
{
|
|
'song': ['Данный трек уже добавлен в избранное']
|
|
})
|
|
return super().validate(attrs)
|
|
|
|
class Meta:
|
|
model = FavoriteSong
|
|
fields = ('id', 'song', 'user')
|
|
|
|
def to_representation(self, instance):
|
|
rep = super().to_representation(instance)
|
|
rep["song"] = SongSerializer(
|
|
instance.song).data
|
|
return rep
|
|
|
|
class PodkastSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = Podkast
|
|
fields = '__all__'
|
|
|
|
|