RadioProfile now has song request functionality.

This commit is contained in:
Josh Washburne 2018-03-30 12:53:41 -04:00
parent b599aa5d1e
commit f6e0365e6b
3 changed files with 63 additions and 4 deletions

View file

@ -0,0 +1,5 @@
class MakeRequestError(Exception):
"""
Exception raised when there is a problem making a song request.
"""
pass

View file

@ -1,4 +1,4 @@
# Generated by Django 2.0 on 2018-01-19 16:46 # Generated by Django 2.0.3 on 2018-03-30 15:24
from django.conf import settings from django.conf import settings
import django.core.validators import django.core.validators
@ -11,8 +11,8 @@ class Migration(migrations.Migration):
initial = True initial = True
dependencies = [ dependencies = [
('radio', '0002_naming_and_sorting'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL), migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('radio', '0002_naming_and_sorting'),
] ]
operations = [ operations = [
@ -20,6 +20,9 @@ class Migration(migrations.Migration):
name='RadioProfile', name='RadioProfile',
fields=[ fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('disabled', models.BooleanField(default=False, verbose_name='disabled state')),
('disabled_date', models.DateTimeField(blank=True, default=None, null=True, verbose_name='disabled on')),
('disabled_reason', models.TextField(blank=True, verbose_name='reason for disabling')),
('created_date', models.DateTimeField(auto_now_add=True, verbose_name='added on')), ('created_date', models.DateTimeField(auto_now_add=True, verbose_name='added on')),
('modified_date', models.DateTimeField(auto_now=True, verbose_name='last modified')), ('modified_date', models.DateTimeField(auto_now=True, verbose_name='last modified')),
('favorites', models.ManyToManyField(related_name='song_favorites', to='radio.Song')), ('favorites', models.ManyToManyField(related_name='song_favorites', to='radio.Song')),

View file

@ -4,12 +4,14 @@ from django.core.validators import (MaxLengthValidator, MinValueValidator,
from django.db import models from django.db import models
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from core.behaviors import Timestampable from core.behaviors import Disableable, Timestampable
from core.utils import get_setting
from radio.models import Song from radio.models import Song
from .exceptions import MakeRequestError
from .managers import RequestManager from .managers import RequestManager
class RadioProfile(Timestampable, models.Model): class RadioProfile(Disableable, Timestampable, models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, user = models.OneToOneField(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE, on_delete=models.CASCADE,
null=True, null=True,
@ -22,6 +24,55 @@ class RadioProfile(Timestampable, models.Model):
related_name='song_requests', related_name='song_requests',
through='SongRequest') through='SongRequest')
def disable(self, reason=''):
super().disable(reason)
user = self.user
user.is_active = False
user.save(update_fields=['is_active'])
def enable(self):
super().enable()
user = self.user
user.is_active = True
user.save(update_fields=['is_active'])
def has_reached_request_max(self):
self_requests = SongRequest.music.unplayed().filter(profile=self)
max_requests = get_setting('max_song_requests')
return self_requests.count() >= max_requests
def can_request(self):
if not self.disabled:
return self.user.is_staff or not self.has_reached_request_max()
return False
def make_request(self, song_requested):
if isinstance(song_requested, int):
song = Song.objects.get(pk=song_requested)
else:
song = song_requested
if self.disabled:
raise MakeRequestError('User is currently disabled.')
if self.has_reached_request_max():
max_requests = get_setting('max_song_requests')
message = 'User has reached the maximum request limit ({}).'
raise MakeRequestError(message.format(max_requests))
if song.is_jingle and not self.user.is_staff:
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:
raise MakeRequestError('Song not available at this time.')
play_again = song.get_date_when_requestable()
message = ('Song has been played recently and cannot be requested '
'again until {}')
raise MakeRequestError(message.format(play_again))
SongRequest.objects.create(profile=self, song=song)
def __str__(self): def __str__(self):
return "{}'s profile".format(self.user.get_username()) return "{}'s profile".format(self.user.get_username())