Add RadioManager to remaining Radio models.

This commit is contained in:
Josh Washburne 2018-03-29 12:14:24 -04:00
parent b927bd5bc0
commit 3b55456a78
3 changed files with 64 additions and 35 deletions

View file

@ -7,16 +7,52 @@ from django.db import models
from django.utils import timezone
from core.utils import get_setting
from .querysets import SongQuerySet
from .querysets import RadioQuerySet, SongQuerySet
# Set decimal precision
getcontext().prec = 16
class SongManager(models.Manager):
class RadioManager(models.Manager):
"""
Custom object manager for filtering out common behaviors for a playlist.
Custom object manager for filtering out common behaviors for radio
objects.
"""
def get_queryset(self):
"""
Return customized default QuerySet.
"""
return RadioQuerySet(self.model, using=self._db)
def disabled(self):
"""
Radio objects that are marked as disabled.
"""
return self.get_queryset().disabled()
def enabled(self):
"""
Radio objects that are marked as enabled.
"""
return self.get_queryset().enabled()
def published(self):
"""
Radio objects that are marked as published.
"""
return self.get_queryset().published()
def unpublished(self):
"""
Radio objects that are marked as unpublished.
"""
return self.get_queryset().unpublished()
class SongManager(RadioManager):
"""
Custom object manager for filtering out common behaviors for Song objects.
"""
def get_queryset(self):
"""
@ -28,13 +64,13 @@ class SongManager(models.Manager):
"""
Jingles that are currently published and are enabled.
"""
return self.get_queryset().jingles().enabled().published()
return self.enabled().published().jingles()
def available_songs(self):
"""
Songs that are currently published and are enabled.
"""
return self.get_queryset().songs().enabled().published()
return self.enabled().published().songs()
def playlist_length(self):
"""

View file

@ -4,9 +4,8 @@ from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from core.behaviors import Timestampable
from .behaviors import Disableable, Publishable
from .managers import SongManager
from core.behaviors import Disableable, Publishable, Timestampable
from .managers import RadioManager, SongManager
class Album(Disableable, Publishable, Timestampable, models.Model):
@ -20,6 +19,9 @@ class Album(Disableable, Publishable, Timestampable, models.Model):
editable=False,
max_length=255)
objects = models.Manager()
music = RadioManager()
class Meta:
ordering = ['sorted_title', ]
@ -40,6 +42,9 @@ class Artist(Disableable, Publishable, Timestampable, models.Model):
editable=False,
max_length=255)
objects = models.Manager()
music = RadioManager()
class Meta:
ordering = ['sorted_full_name', ]
@ -72,6 +77,9 @@ class Game(Disableable, Publishable, Timestampable, models.Model):
editable=False,
max_length=255)
objects = models.Manager()
music = RadioManager()
class Meta:
ordering = ['sorted_title', ]

View file

@ -1,30 +1,9 @@
from datetime import timedelta
from django.db import models
from django.utils import timezone
from core.querysets import EnabledQuerySet, PublishedQuerySet
class EnabledQuerySet(models.QuerySet):
"""
Queryset to select all objects that are not disabled.
"""
def enabled(self):
return self.filter(disabled=False)
class PublishedQuerySet(models.QuerySet):
"""
Queryset to select all objects that have been published.
"""
def published(self):
results = self.filter(
models.Q(published_date__isnull=False) &
models.Q(published_date__lte=timezone.now())
)
return results
class TypeQuerySet(models.QuerySet):
class SongTypeQuerySet(models.QuerySet):
"""
Queryset to select all objects that are either songs or jingles.
"""
@ -35,9 +14,15 @@ class TypeQuerySet(models.QuerySet):
return self.filter(song_type='J')
class SongQuerySet(EnabledQuerySet,
PublishedQuerySet,
TypeQuerySet):
class RadioQuerySet(EnabledQuerySet, PublishedQuerySet):
"""
Queryset combination that can easily select enabled and published
objects.
"""
pass
class SongQuerySet(RadioQuerySet, SongTypeQuerySet):
"""
Queryset combination that can easily select enabled objects, published
objects, and objects of a certain song type.