Commit 63f6a2c1 authored by fred's avatar fred
Browse files

utility function to get the program of a day

parent 6e42cd87
from datetime import datetime
from django.core.management.base import BaseCommand, CommandError
from ...utils import day_program
class Command(BaseCommand):
def handle(self, year, month, day, **options):
date = datetime(int(year), int(month), int(day))
for entry in day_program(date):
print entry.datetime, entry
......@@ -116,6 +116,11 @@ class Schedule(models.Model):
else:
return self.emission.duration
def match_week(self, week_no):
if (self.weeks & (0b0001<<(week_no)) == 0):
return False
return True
def matches(self, dt):
weekday = dt.weekday()
if dt.hour < self.DAY_HOUR_START:
......@@ -124,7 +129,7 @@ class Schedule(models.Model):
return False
if self.weeks != 0b1111:
week_no = (dt.day-1) // 7
if (self.weeks & (0b0001<<(week_no)) == 0):
if self.match_week(week_no) is False:
return False
if dt.time() >= self.datetime.time() and \
dt.time() <= (self.datetime + datetime.timedelta(minutes=self.get_duration())).time():
......@@ -185,6 +190,9 @@ class Diffusion(models.Model):
return u'%s at %02d:%02d' % (self.episode.title, self.datetime.hour,
self.datetime.minute)
def get_duration(self):
return self.episode.get_duration()
def get_sound_path(instance, filename):
return os.path.join('sounds', instance.episode.emission.slug,
......
......@@ -38,3 +38,58 @@ def whatsonair():
return {'emission': emission,
'episode': episode}
def day_program(date):
from models import Diffusion, Schedule
date_start = datetime(*date.timetuple()[:3])
date_end = date_start + timedelta(days=1)
diffusions = Diffusion.objects.filter(
datetime__range=(date_start, date_end)).order_by('datetime')
diffusions = [x for x in diffusions if x.datetime >= date_start and
x.datetime < date_end]
dt = datetime(2007, 1, date.weekday()+1, Schedule.DAY_HOUR_START)
day_schedule = Schedule.objects.filter(datetime__gte=dt).filter(
datetime__lt=dt+timedelta(days=1)).order_by('datetime')
week_no = (date.day-1) // 7
program = [x for x in day_schedule if x.match_week(week_no)]
for i, schedule in enumerate(program):
if schedule is None:
continue
# look for a diffusion matching this schedule
d = [x for x in diffusions if x.datetime.hour == schedule.datetime.hour
and x.datetime.minute == schedule.datetime.minute]
if d:
program[i] = d[0]
for j, other_schedule in enumerate(program[i+1:]):
if other_schedule.datetime.hour == schedule.datetime.hour and \
other_schedule.datetime.minute == schedule.datetime.minute:
program[i+1+j] = None
else:
break
else:
# update schedule datetime to be the requested day
schedule.datetime = datetime(date.year, date.month, date.day,
schedule.datetime.hour, schedule.datetime.minute)
if schedule.datetime.hour < Schedule.DAY_HOUR_START:
schedule.datetime += timedelta(days=1)
program = [x for x in program if x is not None]
for i, slot in enumerate(program):
if slot is None:
continue
slot_end = slot.datetime + timedelta(minutes=slot.get_duration())
j = i+1
while j < len(program)-1:
if program[j]:
if slot_end > program[j].datetime:
program[j] = None
j += 1
return [x for x in program if x is not None]
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