Added better jingle functionality.

This commit is contained in:
Josh Washburne 2020-04-11 18:17:39 -04:00
parent efcb00c0ec
commit 3a3881f661
3 changed files with 68 additions and 9 deletions

View file

@ -64,19 +64,17 @@ class NextRequest(RetrieveAPIView):
def retrieve(self, request): def retrieve(self, request):
dj_profile = RadioProfile.objects.get(user__is_dj=True) dj_profile = RadioProfile.objects.get(user__is_dj=True)
limit = radio_settings['general__songs_per_jingle'] if SongRequest.music.should_play_jingle():
queued_songs = SongRequest.music.get_queued_requests(limit) random_jingle = Song.music.get_random_jingle()
if [j for j in queued_songs if j.song.is_jingle]: next_request = SongRequest.objects.create(profile=dj_profile,
song=random_jingle)
else:
if not SongRequest.music.new_requests().exists(): if not SongRequest.music.new_requests().exists():
random_song = Song.music.get_random_requestable_song() random_song = Song.music.get_random_requestable_song()
next_request = SongRequest.objects.create(profile=dj_profile, next_request = SongRequest.objects.create(profile=dj_profile,
song=random_song) song=random_song)
else: else:
next_request = SongRequest.music.next_request() next_request = SongRequest.music.next_request()
else:
random_jingle = Song.music.get_random_jingle()
next_request = SongRequest.objects.create(profile=dj_profile,
song=random_jingle)
next_request.queued_at = timezone.now() next_request.queued_at = timezone.now()
next_request.save(update_fields=['queued_at']) next_request.save(update_fields=['queued_at'])

View file

@ -1,11 +1,16 @@
from django.forms import ValidationError from django.forms import ValidationError
from django.utils import timezone
from dynamic_preferences.types import FloatPreference, IntegerPreference from dynamic_preferences.types import (
BooleanPreference, ChoicePreference, DurationPreference,
FloatPreference, IntegerPreference
)
from dynamic_preferences.preferences import Section from dynamic_preferences.preferences import Section
from dynamic_preferences.registries import global_preferences_registry from dynamic_preferences.registries import global_preferences_registry
general = Section('general') general = Section('general')
jingles = Section('jingles')
replay = Section('replay') replay = Section('replay')
@ -27,8 +32,30 @@ class MaxSongRequests(IntegerPreference):
@global_preferences_registry.register @global_preferences_registry.register
class SongsPerJingle(IntegerPreference): class JinglesEnabled(BooleanPreference):
section = general section = general
name = 'jingles_enabled'
help_text = 'Enable jingle functionality for the radio.'
default = False
required = False
@global_preferences_registry.register
class JinglesFunction(ChoicePreference):
section = jingles
name = 'jingles_function'
help_text = 'The function to determine when jingles are played.'
choices = [
('after_songs', 'After so many songs are played'),
('after_time', 'After so much time has passed'),
]
default = 'after_songs'
required = True
@global_preferences_registry.register
class SongsPerJingle(IntegerPreference):
section = jingles
name = 'songs_per_jingle' name = 'songs_per_jingle'
help_text = 'The amount of songs that will be played between jingles.' help_text = 'The amount of songs that will be played between jingles.'
default = 30 default = 30
@ -39,6 +66,15 @@ class SongsPerJingle(IntegerPreference):
raise ValidationError('Must be greater than 0.') raise ValidationError('Must be greater than 0.')
@global_preferences_registry.register
class TimeBeforeJingle(DurationPreference):
section = jingles
name = 'time_before_jingle'
help_text = 'The amount of time before a jingle is played.'
default = timezone.timedelta(hours=1)
required = True
@global_preferences_registry.register @global_preferences_registry.register
class ReplayRatio(FloatPreference): class ReplayRatio(FloatPreference):
section = replay section = replay

View file

@ -1,4 +1,10 @@
from django.db import models from django.db import models
from django.utils import timezone
from dynamic_preferences.registries import global_preferences_registry
radio_settings = global_preferences_registry.manager()
class RequestManager(models.Manager): class RequestManager(models.Manager):
@ -34,3 +40,22 @@ class RequestManager(models.Manager):
def next_request(self): def next_request(self):
return self.new_requests().earliest('created_date') return self.new_requests().earliest('created_date')
def should_play_jingle(self):
if radio_settings['general__jingles_enabled']:
function = radio_settings['jingles__jingles_function']
if function == 'after_songs':
limit = radio_settings['jingles__songs_per_jingle']
queued_songs = self.get_queued_requests(limit)
if not [j for j in queued_songs if j.song.is_jingle]:
return True
elif function == 'after_time':
now = timezone.now()
delta = radio_settings['jingles__time_before_jingle']
earlier = now - delta
queued_songs = self.get_queued_requests().filter(
queued_at__range=(earlier, now)
)
if not [j for j in queued_songs if j.song.is_jingle]:
return True
return False