4 from django.views.generic.base import TemplateView
6 from emissions.models import Emission, Episode, Schedule
8 class ProgramView(TemplateView):
9 template_name = 'program.html'
11 def get_context_data(self, **kwargs):
12 context = super(ProgramView, self).get_context_data(**kwargs)
13 schedules = Schedule.objects.all().order_by('datetime')
16 days.append({'schedules': [x for x in schedules if x.is_on_weekday(day+1)],
17 'datetime': datetime.datetime(2007, 1, day+1)})
18 context['days'] = days
21 program = ProgramView.as_view()
30 def __init__(self, i, j):
35 def add_schedule(self, schedule):
36 end_time = schedule.datetime + datetime.timedelta(
37 minutes=schedule.emission.duration)
38 self.time_label = '%02d:%02d-%02d:%02d' % (
39 schedule.datetime.hour,
40 schedule.datetime.minute,
43 self.schedules.append(schedule)
45 def __unicode__(self):
47 return ', '.join([x.emission.title for x in self.schedules])
51 def __eq__(self, other):
52 return (unicode(self) == unicode(other))
55 class Grid(TemplateView):
56 template_name = 'grid.html'
58 def get_context_data(self, **kwargs):
59 context = super(Grid, self).get_context_data(**kwargs)
60 schedules = Schedule.objects.all().order_by('datetime')
62 nb_lines = 2 * 24 # the cells are half hours
65 times = ['%02d:%02d' % (x/2, x%2*30) for x in range(nb_lines)]
66 # start grid after the night programs
67 times = times[2*Schedule.DAY_HOUR_START:] + times[:2*Schedule.DAY_HOUR_START]
69 nonstops = [[0, 2, 'Biodiversite'],
71 [5, 7.5, 'La Panique'],
72 [7.5, 10, 'Matin tranquille'],
73 [10, 12, 'Up Beat Tempo'],
74 [12, 13, 'L\'heure de pointe'],
75 [13, 16, 'Le Mange Disque'],
76 [16, 19, 'Hop Bop and co'],
77 [19, 22, 'Acouphene'],
78 [22, 24, 'Biodiversite']
81 for i in range(nb_lines):
84 grid[-1].append(TimeCell(i, j))
86 nonstop = [x for x in nonstops if i>=x[0]*2 and i<x[1]*2][0]
87 for time_cell in grid[-1]:
88 time_cell.nonstop = nonstop[2]
89 if nonstop[2] == 'Reveries':
90 time_cell.time_label = '02:00-05:00'
92 for schedule in Schedule.objects.all():
93 row_start = schedule.datetime.hour * 2 + \
94 int(math.ceil(schedule.datetime.minute / 30))
95 day_no = schedule.get_weekday()
97 for step in range(int(math.ceil(schedule.emission.duration / 30))):
98 if grid[(row_start+step)%nb_lines][day_no] is None:
99 grid[(row_start+step)%nb_lines][day_no] = TimeCell()
100 grid[(row_start+step)%nb_lines][day_no].add_schedule(schedule)
102 # start grid after the night programs
103 grid = grid[2*Schedule.DAY_HOUR_START:] + grid[:2*Schedule.DAY_HOUR_START]
105 # merge adjacent cells
106 for i in range(nb_lines):
107 for j, cell in enumerate(grid[i]):
108 if grid[i][j] is None:
112 while grid[i][j+t] == cell:
118 grid[i] = [x for x in grid[i] if x is not None]
120 for i in range(nb_lines):
121 grid[i] = [x for x in grid[i] if x is not None]
122 for j, cell in enumerate(grid[i]):
123 if grid[i][j] is None:
128 same_cell_below = [(bj, x) for bj, x in enumerate(grid[i+cell.h])
129 if x == cell and x.y == cell.y and x.w == cell.w]
130 if not same_cell_below:
132 bj, same_cell_below = same_cell_below[0]
133 del grid[i+cell.h][bj]
142 context['grid'] = grid
143 context['times'] = times
144 context['weekdays'] = ['Lundi', 'Mardi', 'Mercredi', 'Jeudi',
145 'Vendredi', 'Samedi', 'Dimanche']
149 grid = Grid.as_view()
152 class Home(TemplateView):
153 template_name = 'home.html'
155 def get_context_data(self, **kwargs):
156 context = super(Home, self).get_context_data(**kwargs)
157 context['emissions'] = Emission.objects.all().order_by('?')[:5]
160 home = Home.as_view()
162 class Get(TemplateView):
163 template_name = 'get.html'
165 def get_context_data(self, **kwargs):
166 context = super(Get, self).get_context_data(**kwargs)
167 context['emissions'] = Emission.objects.all().order_by('title')
172 class Player(TemplateView):
173 template_name = 'player.html'
175 def get_context_data(self, **kwargs):
176 context = super(Player, self).get_context_data(**kwargs)
177 context['emissions'] = Emission.objects.all().order_by('title')
180 player = Player.as_view()