Rearrange Song Serializers to clean up code.
This commit is contained in:
parent
62a4b8db40
commit
a94b75a663
3 changed files with 40 additions and 29 deletions
|
@ -4,7 +4,7 @@ from rest_framework.serializers import (IntegerField,
|
||||||
StringRelatedField)
|
StringRelatedField)
|
||||||
|
|
||||||
from profiles.models import SongRequest
|
from profiles.models import SongRequest
|
||||||
from radio.models import Song
|
from .radio import RadioSongSerializer
|
||||||
|
|
||||||
|
|
||||||
class JustPlayedSerializer(Serializer):
|
class JustPlayedSerializer(Serializer):
|
||||||
|
@ -15,19 +15,8 @@ class MakeRequestSerializer(Serializer):
|
||||||
song = IntegerField()
|
song = IntegerField()
|
||||||
|
|
||||||
|
|
||||||
class NextSongSerializer(ModelSerializer):
|
|
||||||
album = StringRelatedField(many=False)
|
|
||||||
artists = StringRelatedField(many=True)
|
|
||||||
game = StringRelatedField(many=False)
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = Song
|
|
||||||
fields = ('id', 'album', 'artists', 'game',
|
|
||||||
'song_type', 'title', 'length', 'path')
|
|
||||||
|
|
||||||
|
|
||||||
class GetRequestSerializer(ModelSerializer):
|
class GetRequestSerializer(ModelSerializer):
|
||||||
song = NextSongSerializer(many=False)
|
song = RadioSongSerializer()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = SongRequest
|
model = SongRequest
|
||||||
|
|
|
@ -1,46 +1,69 @@
|
||||||
from rest_framework import serializers
|
from rest_framework.serializers import (IntegerField, ListField,
|
||||||
|
ModelSerializer, Serializer,
|
||||||
|
StringRelatedField)
|
||||||
|
|
||||||
from radio.models import Album, Artist, Game, Song
|
from radio.models import Album, Artist, Game, Song
|
||||||
|
|
||||||
|
|
||||||
class AlbumSerializer(serializers.ModelSerializer):
|
class AlbumSerializer(ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Album
|
model = Album
|
||||||
fields = ('id', 'title')
|
fields = ('id', 'title')
|
||||||
|
|
||||||
|
|
||||||
class ArtistSerializer(serializers.ModelSerializer):
|
class ArtistSerializer(ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Artist
|
model = Artist
|
||||||
fields = ('id', 'alias', 'first_name', 'last_name')
|
fields = ('id', 'alias', 'first_name', 'last_name')
|
||||||
|
|
||||||
|
|
||||||
class ArtistFullnameSerializer(serializers.ModelSerializer):
|
class ArtistFullnameSerializer(ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Artist
|
model = Artist
|
||||||
fields = ('id', 'full_name')
|
fields = ('id', 'full_name')
|
||||||
|
|
||||||
|
|
||||||
class GameSerializer(serializers.ModelSerializer):
|
class GameSerializer(ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Game
|
model = Game
|
||||||
fields = ('id', 'title')
|
fields = ('id', 'title')
|
||||||
|
|
||||||
|
|
||||||
class SongSerializer(serializers.ModelSerializer):
|
class BasicSongSerializer(ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = Song
|
||||||
|
fields = ('id', 'album', 'artists', 'game', 'title')
|
||||||
|
|
||||||
|
|
||||||
|
class FullSongSerializer(ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Song
|
model = Song
|
||||||
fields = ('id', 'album', 'artists', 'published_date', 'game',
|
fields = ('id', 'album', 'artists', 'published_date', 'game',
|
||||||
'num_played', 'last_played', 'length', 'song_type', 'title')
|
'num_played', 'last_played', 'length', 'song_type', 'title')
|
||||||
|
|
||||||
|
|
||||||
class SongRetrieveSerializer(SongSerializer):
|
class BasicSongRetrieveSerializer(BasicSongSerializer):
|
||||||
album = AlbumSerializer()
|
album = AlbumSerializer()
|
||||||
artists = ArtistFullnameSerializer(many=True)
|
artists = ArtistFullnameSerializer(many=True)
|
||||||
game = GameSerializer()
|
game = GameSerializer()
|
||||||
|
|
||||||
|
|
||||||
class SongArtistsListSerializer(serializers.Serializer):
|
class FullSongRetrieveSerializer(FullSongSerializer):
|
||||||
artists = serializers.ListField(child=serializers.IntegerField(),
|
album = AlbumSerializer()
|
||||||
min_length=1,
|
artists = ArtistSerializer(many=True)
|
||||||
max_length=10)
|
game = GameSerializer()
|
||||||
|
|
||||||
|
|
||||||
|
class RadioSongSerializer(ModelSerializer):
|
||||||
|
album = StringRelatedField()
|
||||||
|
artists = StringRelatedField(many=True)
|
||||||
|
game = StringRelatedField()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = Song
|
||||||
|
fields = ('album', 'artists', 'game', 'song_type', 'title', 'length',
|
||||||
|
'path')
|
||||||
|
|
||||||
|
|
||||||
|
class SongArtistsListSerializer(Serializer):
|
||||||
|
artists = ListField(child=IntegerField(), min_length=1, max_length=10)
|
||||||
|
|
|
@ -6,9 +6,9 @@ from rest_framework.response import Response
|
||||||
from radio.models import Album, Artist, Game, Song
|
from radio.models import Album, Artist, Game, Song
|
||||||
from ..permissions import IsAdminOrReadOnly
|
from ..permissions import IsAdminOrReadOnly
|
||||||
from ..serializers.radio import (AlbumSerializer, ArtistSerializer,
|
from ..serializers.radio import (AlbumSerializer, ArtistSerializer,
|
||||||
GameSerializer, SongSerializer,
|
GameSerializer, FullSongSerializer,
|
||||||
SongArtistsListSerializer,
|
SongArtistsListSerializer,
|
||||||
SongRetrieveSerializer)
|
FullSongRetrieveSerializer)
|
||||||
|
|
||||||
|
|
||||||
class AlbumViewSet(viewsets.ModelViewSet):
|
class AlbumViewSet(viewsets.ModelViewSet):
|
||||||
|
@ -32,7 +32,6 @@ class GameViewSet(viewsets.ModelViewSet):
|
||||||
class SongViewSet(viewsets.ModelViewSet):
|
class SongViewSet(viewsets.ModelViewSet):
|
||||||
permission_classes = [IsAdminOrReadOnly]
|
permission_classes = [IsAdminOrReadOnly]
|
||||||
queryset = Song.objects.all()
|
queryset = Song.objects.all()
|
||||||
serializer_class = SongSerializer
|
|
||||||
|
|
||||||
def get_serializer_class(self):
|
def get_serializer_class(self):
|
||||||
'''
|
'''
|
||||||
|
@ -41,8 +40,8 @@ class SongViewSet(viewsets.ModelViewSet):
|
||||||
(Thanks to https://stackoverflow.com/questions/22616973/)
|
(Thanks to https://stackoverflow.com/questions/22616973/)
|
||||||
'''
|
'''
|
||||||
if self.action in ['list', 'retrieve']:
|
if self.action in ['list', 'retrieve']:
|
||||||
return SongRetrieveSerializer
|
return FullSongRetrieveSerializer
|
||||||
return SongSerializer
|
return FullSongSerializer
|
||||||
|
|
||||||
def _change_artists(self, request, remove=False):
|
def _change_artists(self, request, remove=False):
|
||||||
song = self.get_object()
|
song = self.get_object()
|
||||||
|
|
Loading…
Reference in a new issue