Commit 19fda372 authored by fred's avatar fred

handle recurring streamed diffusions

parent 3fd107cd
......@@ -6,7 +6,7 @@ import signal
from django.core.management.base import BaseCommand
from emissions.models import Nonstop
from nonstop.models import Track, ScheduledDiffusion
from nonstop.models import Track, ScheduledDiffusion, RecurringStreamOccurence
class Command(BaseCommand):
......@@ -134,13 +134,45 @@ class Command(BaseCommand):
if playlist:
self.playlist[self.playhead + 1:] = playlist
def recompute_slots(self):
def get_current_diffusion(self):
now = datetime.datetime.now()
# print(now, 'recompute_slots')
diffusion = ScheduledDiffusion.objects.filter(
diffusion__datetime__gt=now - datetime.timedelta(days=1),
diffusion__datetime__lt=now).last()
diffusion__datetime__lt=now).order_by('diffusion__datetime').last()
occurence = RecurringStreamOccurence.objects.filter(
datetime__gt=now - datetime.timedelta(days=1),
datetime__lt=now).order_by('datetime').last()
# note it shouldn't be possible to have both diffusion and occurence
# running at the moment.
if occurence and occurence.end_datetime > now:
return occurence
if diffusion and diffusion.end_datetime > now:
return diffusion
return None
def get_next_diffusion(self, before_datetime):
now = datetime.datetime.now()
diffusion = ScheduledDiffusion.objects.filter(
diffusion__datetime__gt=now,
diffusion__datetime__lt=before_datetime,
).order_by('diffusion__datetime').first()
occurence = RecurringStreamOccurence.objects.filter(
datetime__gt=now,
datetime__lt=before_datetime,
).order_by('datetime').first()
if diffusion and occurence:
return diffusion if diffusion.diffusion__datetime < occurence.datetime else occurence
if diffusion:
return diffusion
if occurence:
return occurence
return None
def recompute_slots(self):
now = datetime.datetime.now()
# print(now, 'recompute_slots')
diffusion = self.get_current_diffusion()
if diffusion:
self.slot = diffusion
else:
nonstops = list(Nonstop.objects.all().order_by('start'))
......@@ -164,9 +196,7 @@ class Command(BaseCommand):
if self.slot.end_datetime < self.slot.datetime:
self.slot.end_datetime += datetime.timedelta(days=1)
diffusion = ScheduledDiffusion.objects.filter(
diffusion__datetime__gt=now,
diffusion__datetime__lt=self.slot.end_datetime).first()
diffusion = self.get_next_diffusion(before_datetime=self.slot.end_datetime)
if diffusion:
self.slot.end_datetime = diffusion.datetime
......
......@@ -300,6 +300,21 @@ class RecurringStreamOccurence(models.Model):
diffusion = models.ForeignKey(RecurringStreamDiffusion, on_delete=models.CASCADE)
datetime = models.DateTimeField(_('Date/time'), db_index=True)
@property
def duration(self):
return self.diffusion.schedule.get_duration() * 60
@property
def end_datetime(self):
return self.datetime + datetime.timedelta(minutes=self.diffusion.schedule.get_duration())
@property
def stream(self):
return self.diffusion.stream
def is_stream(self):
return True
class RecurringRandomDirectoryDiffusion(models.Model):
# between soundfiles and nonstop zones, this is used for the "mix
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment