Add Store Serializer and changed related_name.
This commit is contained in:
parent
c305a32717
commit
c428a5a3e9
6 changed files with 31 additions and 16 deletions
|
@ -4,7 +4,7 @@ from rest_framework.serializers import (DecimalField, IntegerField, ListField,
|
|||
StringRelatedField)
|
||||
|
||||
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):
|
||||
|
@ -38,12 +38,27 @@ class GameSerializer(ModelSerializer):
|
|||
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):
|
||||
'''A base serializer for a song model.'''
|
||||
length = DecimalField(
|
||||
max_digits=10,
|
||||
decimal_places=2,
|
||||
source='current_store.length'
|
||||
source='active_store.length'
|
||||
)
|
||||
|
||||
class Meta:
|
||||
|
@ -72,7 +87,7 @@ class SongListSerializer(ModelSerializer):
|
|||
length = DecimalField(
|
||||
max_digits=10,
|
||||
decimal_places=2,
|
||||
source='current_store.length'
|
||||
source='active_store.length'
|
||||
)
|
||||
|
||||
class Meta:
|
||||
|
@ -102,7 +117,7 @@ class RadioSongSerializer(ModelSerializer):
|
|||
length = DecimalField(
|
||||
max_digits=10,
|
||||
decimal_places=2,
|
||||
source='current_store.length'
|
||||
source='active_store.length'
|
||||
)
|
||||
path = SerializerMethodField()
|
||||
|
||||
|
@ -113,7 +128,7 @@ class RadioSongSerializer(ModelSerializer):
|
|||
|
||||
def get_path(self, obj):
|
||||
'''Converts the IRI into a filesystem path.'''
|
||||
iri = str(obj.current_store.iri)
|
||||
iri = str(obj.active_store.iri)
|
||||
if iri.startswith('file://'):
|
||||
return iri_to_path(iri)
|
||||
return iri
|
||||
|
|
|
@ -166,7 +166,7 @@ class SongAdmin(admin.ModelAdmin):
|
|||
'fields': ('song_type',
|
||||
'title',
|
||||
'published_date',
|
||||
'current_store')
|
||||
'active_store')
|
||||
}),
|
||||
('Stats', {
|
||||
'classes': ('collapse',),
|
||||
|
@ -186,7 +186,7 @@ class SongAdmin(admin.ModelAdmin):
|
|||
inlines = [ArtistInline, StoreInline]
|
||||
|
||||
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(
|
||||
song__pk=request.resolver_match.kwargs['object_id']
|
||||
)
|
||||
|
|
|
@ -97,7 +97,7 @@ class Command(BaseCommand):
|
|||
length=song['store']['length']
|
||||
)
|
||||
new_song.stores.add(new_store)
|
||||
new_song.current_store = new_store
|
||||
new_song.active_store = new_store
|
||||
new_song.save()
|
||||
if song['type'] == 'S':
|
||||
totals['songs'] += 1
|
||||
|
|
|
@ -88,7 +88,7 @@ class SongManager(RadioManager):
|
|||
'''
|
||||
a_songs = self.available_songs()
|
||||
length = a_songs.aggregate(
|
||||
total_time=models.Sum('current_store__length')
|
||||
total_time=models.Sum('active_store__length')
|
||||
)
|
||||
return length['total_time']
|
||||
|
||||
|
|
|
@ -37,8 +37,8 @@ class Migration(migrations.Migration):
|
|||
),
|
||||
migrations.AddField(
|
||||
model_name='song',
|
||||
name='current_store',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='current_of', to='radio.Store'),
|
||||
name='active_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(
|
||||
model_name='song',
|
||||
|
|
|
@ -167,11 +167,11 @@ class Song(Disableable, Publishable, Timestampable, models.Model):
|
|||
blank=True,
|
||||
editable=False)
|
||||
stores = models.ManyToManyField(Store, blank=True, related_name='song')
|
||||
current_store = models.ForeignKey(Store,
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name='current_of')
|
||||
active_store = models.ForeignKey(Store,
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name='active_for')
|
||||
sorted_title = models.CharField(_('naturalized title'),
|
||||
db_index=True,
|
||||
editable=False,
|
||||
|
|
Loading…
Reference in a new issue