Add Store Serializer and changed related_name.

This commit is contained in:
RecursiveGreen 2019-06-04 12:22:58 -04:00
parent c305a32717
commit c428a5a3e9
6 changed files with 31 additions and 16 deletions

View file

@ -4,7 +4,7 @@ from rest_framework.serializers import (DecimalField, IntegerField, ListField,
StringRelatedField) StringRelatedField)
from core.utils import iri_to_path from core.utils import iri_to_path
from radio.models import Album, Artist, Game, Song from radio.models import Album, Artist, Game, Song, Store
class AlbumSerializer(ModelSerializer): class AlbumSerializer(ModelSerializer):
@ -38,12 +38,27 @@ class GameSerializer(ModelSerializer):
fields = ('id', 'title') fields = ('id', 'title')
class StoreSerializer(ModelSerializer):
'''A base serializer for a data store model.'''
active = SerializerMethodField()
class Meta:
model = Store
fields = ('id', 'iri', 'file_size', 'length', 'mime_type')
def get_active(self, obj):
'''Checks to see if this store is active for a song.'''
if obj.active_for:
return True
return False
class SongSerializer(ModelSerializer): class SongSerializer(ModelSerializer):
'''A base serializer for a song model.''' '''A base serializer for a song model.'''
length = DecimalField( length = DecimalField(
max_digits=10, max_digits=10,
decimal_places=2, decimal_places=2,
source='current_store.length' source='active_store.length'
) )
class Meta: class Meta:
@ -72,7 +87,7 @@ class SongListSerializer(ModelSerializer):
length = DecimalField( length = DecimalField(
max_digits=10, max_digits=10,
decimal_places=2, decimal_places=2,
source='current_store.length' source='active_store.length'
) )
class Meta: class Meta:
@ -102,7 +117,7 @@ class RadioSongSerializer(ModelSerializer):
length = DecimalField( length = DecimalField(
max_digits=10, max_digits=10,
decimal_places=2, decimal_places=2,
source='current_store.length' source='active_store.length'
) )
path = SerializerMethodField() path = SerializerMethodField()
@ -113,7 +128,7 @@ class RadioSongSerializer(ModelSerializer):
def get_path(self, obj): def get_path(self, obj):
'''Converts the IRI into a filesystem path.''' '''Converts the IRI into a filesystem path.'''
iri = str(obj.current_store.iri) iri = str(obj.active_store.iri)
if iri.startswith('file://'): if iri.startswith('file://'):
return iri_to_path(iri) return iri_to_path(iri)
return iri return iri

View file

@ -166,7 +166,7 @@ class SongAdmin(admin.ModelAdmin):
'fields': ('song_type', 'fields': ('song_type',
'title', 'title',
'published_date', 'published_date',
'current_store') 'active_store')
}), }),
('Stats', { ('Stats', {
'classes': ('collapse',), 'classes': ('collapse',),
@ -186,7 +186,7 @@ class SongAdmin(admin.ModelAdmin):
inlines = [ArtistInline, StoreInline] inlines = [ArtistInline, StoreInline]
def formfield_for_foreignkey(self, db_field, request, **kwargs): def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == 'current_store': if db_field.name == 'active_store':
kwargs['queryset'] = Store.objects.filter( kwargs['queryset'] = Store.objects.filter(
song__pk=request.resolver_match.kwargs['object_id'] song__pk=request.resolver_match.kwargs['object_id']
) )

View file

@ -97,7 +97,7 @@ class Command(BaseCommand):
length=song['store']['length'] length=song['store']['length']
) )
new_song.stores.add(new_store) new_song.stores.add(new_store)
new_song.current_store = new_store new_song.active_store = new_store
new_song.save() new_song.save()
if song['type'] == 'S': if song['type'] == 'S':
totals['songs'] += 1 totals['songs'] += 1

View file

@ -88,7 +88,7 @@ class SongManager(RadioManager):
''' '''
a_songs = self.available_songs() a_songs = self.available_songs()
length = a_songs.aggregate( length = a_songs.aggregate(
total_time=models.Sum('current_store__length') total_time=models.Sum('active_store__length')
) )
return length['total_time'] return length['total_time']

View file

@ -37,8 +37,8 @@ class Migration(migrations.Migration):
), ),
migrations.AddField( migrations.AddField(
model_name='song', model_name='song',
name='current_store', name='active_store',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='current_of', to='radio.Store'), field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='active_for', to='radio.Store'),
), ),
migrations.AddField( migrations.AddField(
model_name='song', model_name='song',

View file

@ -167,11 +167,11 @@ class Song(Disableable, Publishable, Timestampable, models.Model):
blank=True, blank=True,
editable=False) editable=False)
stores = models.ManyToManyField(Store, blank=True, related_name='song') stores = models.ManyToManyField(Store, blank=True, related_name='song')
current_store = models.ForeignKey(Store, active_store = models.ForeignKey(Store,
on_delete=models.SET_NULL, on_delete=models.SET_NULL,
null=True, null=True,
blank=True, blank=True,
related_name='current_of') related_name='active_for')
sorted_title = models.CharField(_('naturalized title'), sorted_title = models.CharField(_('naturalized title'),
db_index=True, db_index=True,
editable=False, editable=False,