from datetime import datetime, timedelta import math from django.views.generic.base import TemplateView from emissions.models import Emission, Episode, Diffusion, SoundFile, Schedule class ProgramView(TemplateView): template_name = 'program.html' def get_context_data(self, **kwargs): context = super(ProgramView, self).get_context_data(**kwargs) schedules = Schedule.objects.all().order_by('datetime') days = [] for day in range(7): days.append({'schedules': [x for x in schedules if x.is_on_weekday(day+1)], 'datetime': datetime(2007, 1, day+1)}) context['days'] = days return context program = ProgramView.as_view() class TimeCell: emissions = None nonstop = None w = 1 h = 1 time_label = None def __init__(self, i, j): self.x = i self.y = j self.schedules = [] def add_schedule(self, schedule): end_time = schedule.datetime + timedelta( minutes=schedule.emission.duration) self.time_label = '%02d:%02d-%02d:%02d' % ( schedule.datetime.hour, schedule.datetime.minute, end_time.hour, end_time.minute) self.schedules.append(schedule) def __unicode__(self): if self.schedules: return ', '.join([x.emission.title for x in self.schedules]) else: return self.nonstop def __eq__(self, other): return (unicode(self) == unicode(other)) class Grid(TemplateView): template_name = 'grid.html' def get_context_data(self, **kwargs): context = super(Grid, self).get_context_data(**kwargs) schedules = Schedule.objects.all().order_by('datetime') nb_lines = 2 * 24 # the cells are half hours grid = [] times = ['%02d:%02d' % (x/2, x%2*30) for x in range(nb_lines)] # start grid after the night programs times = times[2*Schedule.DAY_HOUR_START:] + times[:2*Schedule.DAY_HOUR_START] nonstops = [[0, 2, 'Biodiversite'], [2, 5, 'Reveries'], [5, 7.5, 'La Panique'], [7.5, 10, 'Matin tranquille'], [10, 12, 'Up Beat Tempo'], [12, 13, 'L\'heure de pointe'], [13, 16, 'Le Mange Disque'], [16, 19, 'Hop Bop and co'], [19, 22, 'Acouphene'], [22, 24, 'Biodiversite'] ] for i in range(nb_lines): grid.append([]) for j in range(7): grid[-1].append(TimeCell(i, j)) nonstop = [x for x in nonstops if i>=x[0]*2 and i