From de74f17fb0dc54ac589561ba5c78ae2667304c35 Mon Sep 17 00:00:00 2001 From: Josh Washburne Date: Fri, 30 Mar 2018 17:21:40 -0400 Subject: [PATCH] is_requestable wasn't checking already queued songs in SongRequest. --- savepointradio/profiles/models.py | 6 +++++- savepointradio/radio/models.py | 20 ++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/savepointradio/profiles/models.py b/savepointradio/profiles/models.py index fc7dd65..ac6ee66 100644 --- a/savepointradio/profiles/models.py +++ b/savepointradio/profiles/models.py @@ -64,8 +64,12 @@ class RadioProfile(Disableable, Timestampable, models.Model): raise MakeRequestError('Users cannot request a jingle.') 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.') + + if song.is_playable: + raise MakeRequestError('Song is already in request queue.') + play_again = song.get_date_when_requestable().isoformat(' ', 'seconds') message = ('Song has been played recently and cannot be requested ' diff --git a/savepointradio/radio/models.py b/savepointradio/radio/models.py index f88d52f..46fbb34 100644 --- a/savepointradio/radio/models.py +++ b/savepointradio/radio/models.py @@ -1,5 +1,6 @@ from datetime import timedelta +from django.apps import apps from django.db import models from django.utils import timezone from django.utils.translation import ugettext_lazy as _ @@ -197,13 +198,28 @@ class Song(Disableable, Publishable, Timestampable, models.Model): return timezone.now() 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(): return self.get_date_when_requestable() <= timezone.now() 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 = property(_is_requestable)