2018-01-18 17:45:00 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.validators import (MaxLengthValidator, MinValueValidator,
|
|
|
|
MaxValueValidator)
|
|
|
|
from django.db import models
|
2019-06-06 19:44:56 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2018-01-18 17:45:00 +00:00
|
|
|
|
2020-02-16 02:12:30 +00:00
|
|
|
from dynamic_preferences.registries import global_preferences_registry
|
|
|
|
|
2018-03-30 16:53:41 +00:00
|
|
|
from core.behaviors import Disableable, Timestampable
|
2018-01-18 17:45:00 +00:00
|
|
|
from radio.models import Song
|
2018-03-30 16:53:41 +00:00
|
|
|
from .exceptions import MakeRequestError
|
2018-01-18 17:45:00 +00:00
|
|
|
from .managers import RequestManager
|
|
|
|
|
|
|
|
|
2020-02-16 02:12:30 +00:00
|
|
|
radio_settings = global_preferences_registry.manager()
|
|
|
|
|
|
|
|
|
2018-03-30 16:53:41 +00:00
|
|
|
class RadioProfile(Disableable, Timestampable, models.Model):
|
2018-01-18 17:45:00 +00:00
|
|
|
user = models.OneToOneField(settings.AUTH_USER_MODEL,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
null=True,
|
|
|
|
blank=True)
|
|
|
|
favorites = models.ManyToManyField(Song, related_name='song_favorites')
|
|
|
|
ratings = models.ManyToManyField(Song,
|
|
|
|
related_name='song_ratings',
|
|
|
|
through='Rating')
|
|
|
|
song_requests = models.ManyToManyField(Song,
|
|
|
|
related_name='song_requests',
|
|
|
|
through='SongRequest')
|
|
|
|
|
2018-03-30 16:53:41 +00:00
|
|
|
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)
|
2020-02-16 02:12:30 +00:00
|
|
|
max_requests = radio_settings['general__max_song_requests']
|
2018-03-30 16:53:41 +00:00
|
|
|
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):
|
2018-09-24 17:28:36 +00:00
|
|
|
try:
|
|
|
|
song = Song.objects.get(pk=song_requested)
|
|
|
|
except Song.DoesNotExist:
|
|
|
|
raise MakeRequestError('Song does not exist.')
|
2018-03-30 16:53:41 +00:00
|
|
|
else:
|
|
|
|
song = song_requested
|
|
|
|
|
|
|
|
if self.disabled:
|
|
|
|
raise MakeRequestError('User is currently disabled.')
|
|
|
|
|
2020-02-13 16:30:39 +00:00
|
|
|
if self.has_reached_request_max() and not self.user.is_staff:
|
2020-02-16 02:12:30 +00:00
|
|
|
max_requests = radio_settings['general__max_song_requests']
|
2018-03-30 16:53:41 +00:00
|
|
|
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:
|
2018-03-30 21:21:40 +00:00
|
|
|
if not song.is_available:
|
2018-03-30 16:53:41 +00:00
|
|
|
raise MakeRequestError('Song not available at this time.')
|
2018-04-06 16:21:50 +00:00
|
|
|
|
2018-03-30 21:21:40 +00:00
|
|
|
if song.is_playable:
|
|
|
|
raise MakeRequestError('Song is already in request queue.')
|
2018-04-06 16:21:50 +00:00
|
|
|
|
2018-03-30 17:07:58 +00:00
|
|
|
play_again = song.get_date_when_requestable().isoformat(' ',
|
|
|
|
'seconds')
|
2018-03-30 16:53:41 +00:00
|
|
|
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)
|
|
|
|
|
2018-01-18 17:45:00 +00:00
|
|
|
def __str__(self):
|
|
|
|
return "{}'s profile".format(self.user.get_username())
|
|
|
|
|
|
|
|
|
|
|
|
class Rating(Timestampable, models.Model):
|
|
|
|
profile = models.ForeignKey(RadioProfile,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name='rating_profile')
|
|
|
|
song = models.ForeignKey(Song, on_delete=models.CASCADE)
|
|
|
|
value = models.PositiveIntegerField(_('song rating'),
|
|
|
|
validators=[MinValueValidator(1),
|
|
|
|
MaxValueValidator(5)])
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "{} - {}'s Rating: {}".format(self.song.title,
|
|
|
|
self.profile.user.get_username(),
|
|
|
|
self.value)
|
|
|
|
|
|
|
|
|
|
|
|
class SongRequest(Timestampable, models.Model):
|
|
|
|
profile = models.ForeignKey(RadioProfile,
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
related_name='request_profile')
|
|
|
|
song = models.ForeignKey(Song,
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
blank=True)
|
|
|
|
queued_at = models.DateTimeField(_('song queued at'),
|
|
|
|
default=None,
|
|
|
|
blank=True,
|
|
|
|
null=True)
|
|
|
|
played_at = models.DateTimeField(_('song played at'),
|
|
|
|
default=None,
|
|
|
|
blank=True,
|
|
|
|
null=True)
|
|
|
|
|
|
|
|
objects = models.Manager()
|
|
|
|
music = RequestManager()
|
|
|
|
|
2018-01-19 17:18:50 +00:00
|
|
|
class Meta:
|
2020-02-19 20:31:29 +00:00
|
|
|
ordering = ['-queued_at', '-created_date', ]
|
2018-01-19 17:18:50 +00:00
|
|
|
|
2018-01-18 17:45:00 +00:00
|
|
|
def __str__(self):
|
|
|
|
req_user = self.profile.user.get_username()
|
|
|
|
return "{} - Requested by {} at {}".format(self.song.title,
|
|
|
|
req_user,
|
|
|
|
self.created_date)
|