]> git.0d.be Git - panikweb.git/blob - panikweb/views.py
grid: add classes and tags required by osp style
[panikweb.git] / panikweb / views.py
1 import datetime
2 import math
3
4 from django.views.generic.base import TemplateView
5
6 from emissions.models import Emission, Episode, Schedule
7
8 class ProgramView(TemplateView):
9     template_name = 'program.html'
10
11     def get_context_data(self, **kwargs):
12         context = super(ProgramView, self).get_context_data(**kwargs)
13         schedules = Schedule.objects.all().order_by('datetime')
14         days = []
15         for day in range(7):
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
19         return context
20
21 program = ProgramView.as_view()
22
23 class TimeCell:
24     emissions = None
25     nonstop = None
26     w = 1
27     h = 1
28
29     def __init__(self, i, j):
30         self.x = i
31         self.y = j
32         self.emissions = []
33
34     def add_emission(self, emission):
35         self.emissions.append(emission)
36
37     def __unicode__(self):
38         if self.emissions:
39             return ', '.join([x.title for x in self.emissions])
40         else:
41             return self.nonstop
42
43     def __eq__(self, other):
44         return (unicode(self) == unicode(other))
45
46
47 class Grid(TemplateView):
48     template_name = 'grid.html'
49
50     def get_context_data(self, **kwargs):
51         context = super(Grid, self).get_context_data(**kwargs)
52         schedules = Schedule.objects.all().order_by('datetime')
53
54         nb_lines = 2 * 24 # the cells are half hours
55         grid = []
56
57         times = ['%02d:%02d' % (x/2, x%2*30) for x in range(nb_lines)]
58         # grid starts at 5am
59         times = times[2*5:] + times[:2*5]
60
61         nonstops = [[0, 2, 'Biodiversite'],
62                     [2, 5, 'Reveries'],
63                     [5, 7.5, 'La Panique'],
64                     [7.5, 10, 'Matin tranquille'],
65                     [10, 12, 'Up Beat Tempo'],
66                     [12, 13, 'L\'heure de pointe'],
67                     [13, 16, 'Le Mange Disque'],
68                     [16, 19, 'Hop Bop and co'],
69                     [19, 22, 'Acouphene'],
70                     [22, 24, 'Biodiversite']
71                    ]
72
73         for i in range(nb_lines):
74             grid.append([])
75             for j in range(7):
76                 grid[-1].append(TimeCell(i, j))
77
78             nonstop = [x for x in nonstops if i>=x[0]*2 and i<x[1]*2][0]
79             for time_cell in grid[-1]:
80                 time_cell.nonstop = nonstop[2]
81
82         for schedule in Schedule.objects.all():
83             row_start = schedule.datetime.hour * 2 + \
84                     int(math.ceil(schedule.datetime.minute / 30))
85             day_no = schedule.get_weekday()
86
87             for step in range(int(math.ceil(schedule.emission.duration / 30))):
88                 if grid[row_start+step][day_no] is None:
89                     print 'creating a time cell at', row_start+step, day_no
90                     grid[row_start+step][day_no] = TimeCell()
91                 grid[row_start+step][day_no].add_emission(schedule.emission)
92
93
94         # start grid at 5am
95         grid = grid[2*5:] + grid[:2*5]
96
97         # merge adjacent cells
98         for i in range(nb_lines):
99             for j, cell in enumerate(grid[i]):
100                 if grid[i][j] is None:
101                     continue
102                 t = 1
103                 try:
104                     while grid[i][j+t] == cell:
105                         cell.w += 1
106                         grid[i][j+t] = None
107                         t += 1
108                 except IndexError:
109                     pass
110             grid[i] = [x for x in grid[i] if x is not None]
111
112         for i in range(nb_lines):
113             grid[i] = [x for x in grid[i] if x is not None]
114             for j, cell in enumerate(grid[i]):
115                 if grid[i][j] is None:
116                     continue
117                 t = 1
118                 try:
119                     while True:
120                         same_cell_below = [(bj, x) for bj, x in enumerate(grid[i+cell.h])
121                                            if x == cell and x.y == cell.y and x.w == cell.w]
122                         if not same_cell_below:
123                             break
124                         bj, same_cell_below = same_cell_below[0]
125                         del grid[i+cell.h][bj]
126                         cell.h += 1
127                 except IndexError:
128                     pass
129
130         context['grid'] = grid
131         context['times'] = times
132         context['weekdays'] = ['Lundi', 'Mardi', 'Mercredi', 'Jeudi',
133                 'Vendredi', 'Samedi', 'Dimanche']
134
135         return context
136
137 grid = Grid.as_view()
138
139
140 class Home(TemplateView):
141     template_name = 'home.html'
142
143     def get_context_data(self, **kwargs):
144         context = super(Home, self).get_context_data(**kwargs)
145         context['emissions'] = Emission.objects.all().order_by('?')[:5]
146         return context
147
148 home = Home.as_view()
149
150 class Get(TemplateView):
151     template_name = 'get.html'
152
153     def get_context_data(self, **kwargs):
154         context = super(Get, self).get_context_data(**kwargs)
155         context['emissions'] = Emission.objects.all().order_by('title')
156         return context
157
158 get = Get.as_view()
159
160 class Player(TemplateView):
161     template_name = 'player.html'
162
163     def get_context_data(self, **kwargs):
164         context = super(Player, self).get_context_data(**kwargs)
165         #context['emissions'] = Emission.objects.all().order_by('title')
166         return context
167
168 player = Player.as_view()