Commit 54a28c2b authored by fred's avatar fred

insert nonstop periods into program

parent 30b6aafa
from datetime import datetime, timedelta
from datetime import datetime, timedelta, time
import os
from PIL import Image
......@@ -51,7 +51,7 @@ def whatsonair():
def period_program(date_start, date_end):
from models import Diffusion, Schedule
from models import Diffusion, Schedule, Nonstop, WeekdayMixin
diffusions = Diffusion.objects.select_related().filter(
datetime__range=(date_start, date_end)).order_by('datetime')
......@@ -105,7 +105,59 @@ def period_program(date_start, date_end):
program[j] = None
j += 1
return [x for x in program if x is not None]
program = [x for x in program if x is not None]
# last step is adding nonstop zones between slots
nonstops = Nonstop.objects.all()
class NonstopSlot(WeekdayMixin):
def __init__(self, start, end):
self.datetime = None
labels = []
for nonstop in nonstops:
nonstop_day_start = start.replace(hour=nonstop.start.hour, minute=nonstop.start.minute)
nonstop_day_end = start.replace(hour=nonstop.end.hour, minute=nonstop.end.minute)
if nonstop.end < nonstop.start:
if start.time() < nonstop.end:
nonstop_day_start -= timedelta(days=1)
else:
nonstop_day_end += timedelta(days=1)
if nonstop_day_start < end and nonstop_day_end > start:
if self.datetime is None:
self.datetime = nonstop_day_end
labels.append(nonstop.title)
self.label = u', '.join(labels)
def __repr__(self):
return repr(self.label)
first_day_start = datetime(*date_start.replace(hour=6, minute=0).timetuple()[:5])
last_day_end = datetime(*date_end.replace(hour=5, minute=0).timetuple()[:5])
program.insert(0, NonstopSlot(first_day_start, program[0].datetime))
i = 0
while i < len(program)-1:
slot = program[i]
if not isinstance(slot, NonstopSlot):
slot_end = slot.datetime + timedelta(minutes=slot.get_duration())
next_slot = program[i+1]
if slot_end < next_slot.datetime:
next_day_start = next_slot.datetime.replace(hour=5, minute=0)
if slot_end < next_day_start and next_slot.datetime > next_day_start:
program[i+1:i+1] = [NonstopSlot(slot_end, next_day_start),
NonstopSlot(next_day_start, next_slot.datetime)]
else:
program[i+1:i+1] = [NonstopSlot(slot_end, next_slot.datetime)]
i += 1
program.append(NonstopSlot(program[-1].datetime, last_day_end))
return program
def day_program(date):
date_start = datetime(*date.timetuple()[:3])
......
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