Commit 01aace7e authored by fred's avatar fred
Browse files

initiate a "what's on air" command

parent d33fe145
from datetime import datetime, timedelta
import xml.etree.ElementTree as ET
from import BaseCommand, CommandError
from ...models import Emission, Episode, Diffusion, SoundFile, Schedule
class Command(BaseCommand):
def handle(self, *args, **options):
now =
date_start = now - timedelta(days=1)
date_end = now + 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 <= now and
now <= x.datetime + timedelta(minutes=x.episode.emission.duration)]
episode = None
emission = None
if diffusions: # there should be only one
diffusion = diffusions[0]
episode = diffusion.episode
emission = episode.emission
for schedule in Schedule.objects.all():
if not schedule.matches(now):
emission = schedule.emission
print 'found episode:', episode
print 'found emission:', emission
......@@ -98,6 +98,26 @@ class Schedule(models.Model):
return '2e et 4e'
return None
def matches(self, dt):
weekday = dt.weekday()
if dt.hour < self.DAY_HOUR_START:
weekday -= 1
if weekday != self.get_weekday():
return False
if self.weeks != 0b1111:
week_no = ( // 7
if (self.weeks & (0b0001<<(week_no)) == 0):
return False
if dt.time() >= self.datetime.time() and \
dt.time() <= (self.datetime + datetime.timedelta(minutes=self.emission.duration)).time():
return True
return False
def __unicode__(self):
return u'%s at %s' % (self.emission.title,
self.datetime.strftime('%a %H:%M'))
def get_episode_image_path(instance, filename):
return os.path.join('images', instance.episode.emission.slug,
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