Rearrange Song Serializers to clean up code.

This commit is contained in:
Josh Washburne 2018-04-05 16:22:15 -04:00
parent 62a4b8db40
commit a94b75a663
3 changed files with 40 additions and 29 deletions

View file

@ -4,7 +4,7 @@ from rest_framework.serializers import (IntegerField,
StringRelatedField)
from profiles.models import SongRequest
from radio.models import Song
from .radio import RadioSongSerializer
class JustPlayedSerializer(Serializer):
@ -15,19 +15,8 @@ class MakeRequestSerializer(Serializer):
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):
song = NextSongSerializer(many=False)
song = RadioSongSerializer()
class Meta:
model = SongRequest

View file

@ -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
class AlbumSerializer(serializers.ModelSerializer):
class AlbumSerializer(ModelSerializer):
class Meta:
model = Album
fields = ('id', 'title')
class ArtistSerializer(serializers.ModelSerializer):
class ArtistSerializer(ModelSerializer):
class Meta:
model = Artist
fields = ('id', 'alias', 'first_name', 'last_name')
class ArtistFullnameSerializer(serializers.ModelSerializer):
class ArtistFullnameSerializer(ModelSerializer):
class Meta:
model = Artist
fields = ('id', 'full_name')
class GameSerializer(serializers.ModelSerializer):
class GameSerializer(ModelSerializer):
class Meta:
model = Game
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:
model = Song
fields = ('id', 'album', 'artists', 'published_date', 'game',
'num_played', 'last_played', 'length', 'song_type', 'title')
class SongRetrieveSerializer(SongSerializer):
class BasicSongRetrieveSerializer(BasicSongSerializer):
album = AlbumSerializer()
artists = ArtistFullnameSerializer(many=True)
game = GameSerializer()
class SongArtistsListSerializer(serializers.Serializer):
artists = serializers.ListField(child=serializers.IntegerField(),
min_length=1,
max_length=10)
class FullSongRetrieveSerializer(FullSongSerializer):
album = AlbumSerializer()
artists = ArtistSerializer(many=True)
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)

View file

@ -6,9 +6,9 @@ from rest_framework.response import Response
from radio.models import Album, Artist, Game, Song
from ..permissions import IsAdminOrReadOnly
from ..serializers.radio import (AlbumSerializer, ArtistSerializer,
GameSerializer, SongSerializer,
GameSerializer, FullSongSerializer,
SongArtistsListSerializer,
SongRetrieveSerializer)
FullSongRetrieveSerializer)
class AlbumViewSet(viewsets.ModelViewSet):
@ -32,7 +32,6 @@ class GameViewSet(viewsets.ModelViewSet):
class SongViewSet(viewsets.ModelViewSet):
permission_classes = [IsAdminOrReadOnly]
queryset = Song.objects.all()
serializer_class = SongSerializer
def get_serializer_class(self):
'''
@ -41,8 +40,8 @@ class SongViewSet(viewsets.ModelViewSet):
(Thanks to https://stackoverflow.com/questions/22616973/)
'''
if self.action in ['list', 'retrieve']:
return SongRetrieveSerializer
return SongSerializer
return FullSongRetrieveSerializer
return FullSongSerializer
def _change_artists(self, request, remove=False):
song = self.get_object()