diff --git a/savepointradio/core/dynamic_preferences_registry.py b/savepointradio/core/dynamic_preferences_registry.py index 8577209..f329abf 100644 --- a/savepointradio/core/dynamic_preferences_registry.py +++ b/savepointradio/core/dynamic_preferences_registry.py @@ -96,6 +96,15 @@ class ReplayRatio(FloatPreference): raise ValidationError('Must be between 0.0 and 1.0, inclusive.') +@global_preferences_registry.register +class VarianceEnabled(BooleanPreference): + section = replay + name = 'variance_enabled' + help_text = 'Enable variance to replay time based on user ratings.' + default = False + required = False + + @global_preferences_registry.register class MinRatingsForVariance(IntegerPreference): section = replay diff --git a/savepointradio/radio/models.py b/savepointradio/radio/models.py index 63d7f9b..267963a 100644 --- a/savepointradio/radio/models.py +++ b/savepointradio/radio/models.py @@ -246,6 +246,20 @@ class Song(Disableable, Publishable, Timestampable, models.Model): return None average_rating = property(_average_rating) + def get_adjusted_ratio(self): + ''' + Return a change to the replay ratio based on various factors. + ''' + if radio_settings['replay__variance_enabled']: + min_ratings = radio_settings['replay__min_ratings_for_variance'] + if self.rating_set.count() >= min_ratings: + ratio = radio_settings['replay__rating_variance_ratio'] + + # -((average - 1)/(highest_rating - 1)) * ratio + base = -((self._average_rating() - 1) / 4) * ratio + return float(base + (ratio * 0.5)) + return float(0.0) + def get_time_until_requestable(self): ''' Length of time before a song can be requested again. @@ -267,17 +281,7 @@ class Song(Disableable, Publishable, Timestampable, models.Model): if self._is_song() and self._is_available(): if last: - # Check if we have enough ratings to change ratio - min_rate = radio_settings['replay__min_ratings_for_variance'] - if self.rating_set.count() >= min_rate: - ratio = radio_settings['replay__rating_variance_ratio'] - - # -((average - 1)/(highest_rating - 1)) * ratio - base = -((self._average_rating() - 1) / 4) * ratio - adjusted_ratio = float(base + (ratio * 0.5)) - else: - adjusted_ratio = float(0.0) - + adjusted_ratio = self.get_adjusted_ratio() return last + Song.music.wait_total(adjusted_ratio) return timezone.now() return None