2019-07-03 15:57:46 +00:00
|
|
|
from rest_framework.serializers import (BooleanField, CharField, DecimalField,
|
2019-06-04 18:58:52 +00:00
|
|
|
IntegerField, ListField,
|
2018-04-05 20:22:15 +00:00
|
|
|
ModelSerializer, Serializer,
|
2019-06-04 16:03:12 +00:00
|
|
|
SerializerMethodField,
|
2018-04-05 20:22:15 +00:00
|
|
|
StringRelatedField)
|
2018-03-28 20:12:55 +00:00
|
|
|
|
2019-06-04 16:03:12 +00:00
|
|
|
from core.utils import iri_to_path
|
2019-06-04 16:22:58 +00:00
|
|
|
from radio.models import Album, Artist, Game, Song, Store
|
2018-03-28 20:12:55 +00:00
|
|
|
|
|
|
|
|
2018-04-05 20:22:15 +00:00
|
|
|
class AlbumSerializer(ModelSerializer):
|
2019-06-04 16:03:12 +00:00
|
|
|
'''A base serializer for an album model.'''
|
2018-03-28 20:12:55 +00:00
|
|
|
class Meta:
|
|
|
|
model = Album
|
|
|
|
fields = ('id', 'title')
|
|
|
|
|
|
|
|
|
2018-04-05 20:22:15 +00:00
|
|
|
class ArtistSerializer(ModelSerializer):
|
2019-06-04 16:03:12 +00:00
|
|
|
'''A base serializer for an artist model.'''
|
2018-03-28 20:12:55 +00:00
|
|
|
class Meta:
|
|
|
|
model = Artist
|
|
|
|
fields = ('id', 'alias', 'first_name', 'last_name')
|
|
|
|
|
|
|
|
|
2018-04-05 20:22:15 +00:00
|
|
|
class ArtistFullnameSerializer(ModelSerializer):
|
2019-06-04 16:03:12 +00:00
|
|
|
'''
|
|
|
|
A base serializer for an artist model, but combining all name
|
|
|
|
attributes into one field.
|
|
|
|
'''
|
2018-03-28 20:12:55 +00:00
|
|
|
class Meta:
|
|
|
|
model = Artist
|
|
|
|
fields = ('id', 'full_name')
|
|
|
|
|
|
|
|
|
2018-04-05 20:22:15 +00:00
|
|
|
class GameSerializer(ModelSerializer):
|
2019-06-04 16:03:12 +00:00
|
|
|
'''A base serializer for a game model.'''
|
2018-03-28 20:12:55 +00:00
|
|
|
class Meta:
|
|
|
|
model = Game
|
|
|
|
fields = ('id', 'title')
|
|
|
|
|
|
|
|
|
2019-06-04 16:22:58 +00:00
|
|
|
class StoreSerializer(ModelSerializer):
|
|
|
|
'''A base serializer for a data store model.'''
|
|
|
|
active = SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Store
|
2019-06-04 16:43:04 +00:00
|
|
|
fields = ('id', 'active', 'iri', 'file_size', 'length', 'mime_type')
|
2019-06-04 16:22:58 +00:00
|
|
|
|
|
|
|
def get_active(self, obj):
|
|
|
|
'''Checks to see if this store is active for a song.'''
|
2019-06-04 18:58:52 +00:00
|
|
|
if obj.active_for.all():
|
2019-06-04 16:22:58 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2019-06-04 16:03:12 +00:00
|
|
|
class SongSerializer(ModelSerializer):
|
|
|
|
'''A base serializer for a song model.'''
|
|
|
|
length = DecimalField(
|
|
|
|
max_digits=10,
|
|
|
|
decimal_places=2,
|
2019-06-04 16:22:58 +00:00
|
|
|
source='active_store.length'
|
2019-06-04 16:03:12 +00:00
|
|
|
)
|
|
|
|
|
2018-04-05 20:22:15 +00:00
|
|
|
class Meta:
|
|
|
|
model = Song
|
2019-06-04 16:03:12 +00:00
|
|
|
fields = ('id', 'album', 'artists', 'published_date', 'game',
|
|
|
|
'num_played', 'last_played', 'length', 'next_play',
|
|
|
|
'song_type', 'title', 'average_rating', 'is_requestable')
|
|
|
|
|
2018-04-05 20:22:15 +00:00
|
|
|
|
2019-06-04 16:03:12 +00:00
|
|
|
class SongMinimalSerializer(ModelSerializer):
|
|
|
|
'''Minimal song information, usually appended to favorites/ratings.'''
|
|
|
|
album = AlbumSerializer()
|
|
|
|
artists = ArtistFullnameSerializer(many=True)
|
|
|
|
game = GameSerializer()
|
2018-04-05 20:22:15 +00:00
|
|
|
|
2018-03-28 20:12:55 +00:00
|
|
|
class Meta:
|
|
|
|
model = Song
|
2019-06-04 16:03:12 +00:00
|
|
|
fields = ('id', 'album', 'artists', 'game', 'title')
|
2018-04-02 20:54:53 +00:00
|
|
|
|
2018-04-03 20:24:54 +00:00
|
|
|
|
2019-06-04 16:03:12 +00:00
|
|
|
class SongListSerializer(ModelSerializer):
|
|
|
|
'''Song information used in large listings.'''
|
2018-04-05 14:58:27 +00:00
|
|
|
album = AlbumSerializer()
|
2018-04-02 20:54:53 +00:00
|
|
|
artists = ArtistFullnameSerializer(many=True)
|
2018-04-05 14:58:27 +00:00
|
|
|
game = GameSerializer()
|
2019-06-04 16:03:12 +00:00
|
|
|
length = DecimalField(
|
|
|
|
max_digits=10,
|
|
|
|
decimal_places=2,
|
2019-06-04 16:22:58 +00:00
|
|
|
source='active_store.length'
|
2019-06-04 16:03:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Song
|
|
|
|
fields = ('id', 'album', 'artists', 'game', 'title', 'average_rating',
|
|
|
|
'length', 'is_requestable')
|
2018-04-03 20:24:54 +00:00
|
|
|
|
|
|
|
|
2019-06-04 16:03:12 +00:00
|
|
|
class SongRetrieveSerializer(SongSerializer):
|
|
|
|
'''
|
|
|
|
An almost complete listing of a song's information, based on a single
|
|
|
|
object retrieval.
|
|
|
|
'''
|
2018-04-05 20:22:15 +00:00
|
|
|
album = AlbumSerializer()
|
|
|
|
artists = ArtistSerializer(many=True)
|
|
|
|
game = GameSerializer()
|
|
|
|
|
|
|
|
|
|
|
|
class RadioSongSerializer(ModelSerializer):
|
2019-06-04 16:03:12 +00:00
|
|
|
'''
|
|
|
|
A song serializer that is specific to the radio DJ and the underlying
|
|
|
|
audio manipulation application.
|
|
|
|
'''
|
2018-04-05 20:22:15 +00:00
|
|
|
album = StringRelatedField()
|
|
|
|
artists = StringRelatedField(many=True)
|
|
|
|
game = StringRelatedField()
|
2019-06-04 16:03:12 +00:00
|
|
|
length = DecimalField(
|
|
|
|
max_digits=10,
|
|
|
|
decimal_places=2,
|
2019-06-04 16:22:58 +00:00
|
|
|
source='active_store.length'
|
2019-06-04 16:03:12 +00:00
|
|
|
)
|
2019-07-03 15:57:46 +00:00
|
|
|
replaygain = CharField(source='active_store.replaygain')
|
2019-06-04 16:03:12 +00:00
|
|
|
path = SerializerMethodField()
|
2018-04-05 20:22:15 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Song
|
|
|
|
fields = ('album', 'artists', 'game', 'song_type', 'title', 'length',
|
2019-07-03 15:57:46 +00:00
|
|
|
'replaygain', 'path')
|
2018-04-05 20:22:15 +00:00
|
|
|
|
2019-06-04 16:03:12 +00:00
|
|
|
def get_path(self, obj):
|
|
|
|
'''Converts the IRI into a filesystem path.'''
|
2019-06-04 16:22:58 +00:00
|
|
|
iri = str(obj.active_store.iri)
|
2019-06-04 16:03:12 +00:00
|
|
|
if iri.startswith('file://'):
|
|
|
|
return iri_to_path(iri)
|
|
|
|
return iri
|
|
|
|
|
2018-04-05 20:22:15 +00:00
|
|
|
|
|
|
|
class SongArtistsListSerializer(Serializer):
|
2019-06-04 16:03:12 +00:00
|
|
|
'''
|
|
|
|
A serializer for adding or removing artists from a song based on
|
|
|
|
the song's id number.
|
|
|
|
'''
|
2019-06-04 18:58:52 +00:00
|
|
|
# TODO: Probably should move to PrimaryKeyRelatedField.
|
2018-04-05 20:22:15 +00:00
|
|
|
artists = ListField(child=IntegerField(), min_length=1, max_length=10)
|
2019-06-04 18:58:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SongStoresSerializer(Serializer):
|
|
|
|
'''
|
|
|
|
A serializer for adding or removing a data store from a song based on
|
|
|
|
the song's id number.
|
|
|
|
'''
|
|
|
|
# TODO: Probably should move to PrimaryKeyRelatedField.
|
|
|
|
store = IntegerField()
|
|
|
|
set_active = BooleanField(default=False)
|