is_requestable wasn't checking already queued songs in SongRequest.

This commit is contained in:
Josh Washburne 2018-03-30 17:21:40 -04:00
parent b342fbb116
commit de74f17fb0
2 changed files with 23 additions and 3 deletions

View file

@ -64,8 +64,12 @@ class RadioProfile(Disableable, Timestampable, models.Model):
raise MakeRequestError('Users cannot request a jingle.') raise MakeRequestError('Users cannot request a jingle.')
if song.is_song and not self.user.is_staff and not song.is_requestable: if song.is_song and not self.user.is_staff and not song.is_requestable:
if not song.is_enabled or not song.is_published: if not song.is_available:
raise MakeRequestError('Song not available at this time.') raise MakeRequestError('Song not available at this time.')
if song.is_playable:
raise MakeRequestError('Song is already in request queue.')
play_again = song.get_date_when_requestable().isoformat(' ', play_again = song.get_date_when_requestable().isoformat(' ',
'seconds') 'seconds')
message = ('Song has been played recently and cannot be requested ' message = ('Song has been played recently and cannot be requested '

View file

@ -1,5 +1,6 @@
from datetime import timedelta from datetime import timedelta
from django.apps import apps
from django.db import models from django.db import models
from django.utils import timezone from django.utils import timezone
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -197,13 +198,28 @@ class Song(Disableable, Publishable, Timestampable, models.Model):
return timezone.now() return timezone.now()
return None return None
def _is_requestable(self): def _is_playable(self):
""" """
Can the song be requested or not? Is the song available and not been played within the default waiting
period (or at all)?
""" """
if self._is_song() and self._is_available(): if self._is_song() and self._is_available():
return self.get_date_when_requestable() <= timezone.now() return self.get_date_when_requestable() <= timezone.now()
return False return False
_is_playable.boolean = True
is_playable = property(_is_playable)
def _is_requestable(self):
"""
Is the song playable and has it not already been requested?
"""
if self._is_playable():
SongRequest = apps.get_model(app_label='profiles',
model_name='SongRequest')
requests = SongRequest.music.unplayed().values_list('song__id',
flat=True)
return self.pk not in requests
return False
_is_requestable.boolean = True _is_requestable.boolean = True
is_requestable = property(_is_requestable) is_requestable = property(_is_requestable)