views.py 5.55 KB
Newer Older
fred's avatar
fred committed
1
import datetime
fred's avatar
fred committed
2
import math
fred's avatar
fred committed
3

fred's avatar
fred committed
4
5
from django.views.generic.base import TemplateView

6
from emissions.models import Emission, Episode, Schedule
fred's avatar
fred committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

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.datetime(2007, 1, day+1)})
        context['days'] = days
        return context

program = ProgramView.as_view()

fred's avatar
fred committed
23
24
25
26
27
class TimeCell:
    emissions = None
    nonstop = None
    w = 1
    h = 1
28
    time_label = None
fred's avatar
fred committed
29

fred's avatar
fred committed
30
31
32
    def __init__(self, i, j):
        self.x = i
        self.y = j
33
34
35
36
37
38
39
40
41
42
43
        self.schedules = []

    def add_schedule(self, schedule):
        end_time = schedule.datetime + 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)
fred's avatar
fred committed
44
45

    def __unicode__(self):
46
47
        if self.schedules:
            return ', '.join([x.emission.title for x in self.schedules])
fred's avatar
fred committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
        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 = []

fred's avatar
fred committed
65
66
67
68
        times = ['%02d:%02d' % (x/2, x%2*30) for x in range(nb_lines)]
        # grid starts at 5am
        times = times[2*5:] + times[:2*5]

fred's avatar
fred committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
        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):
fred's avatar
fred committed
84
                grid[-1].append(TimeCell(i, j))
fred's avatar
fred committed
85
86
87
88
89
90
91
92
93
94
95

            nonstop = [x for x in nonstops if i>=x[0]*2 and i<x[1]*2][0]
            for time_cell in grid[-1]:
                time_cell.nonstop = nonstop[2]

        for schedule in Schedule.objects.all():
            row_start = schedule.datetime.hour * 2 + \
                    int(math.ceil(schedule.datetime.minute / 30))
            day_no = schedule.get_weekday()

            for step in range(int(math.ceil(schedule.emission.duration / 30))):
96
97
                if grid[(row_start+step)%nb_lines][day_no] is None:
                    grid[(row_start+step)%nb_lines][day_no] = TimeCell()
98
                grid[(row_start+step)%nb_lines][day_no].add_schedule(schedule)
fred's avatar
fred committed
99
100
101
102

        # start grid at 5am
        grid = grid[2*5:] + grid[:2*5]

fred's avatar
fred committed
103
        # merge adjacent cells
fred's avatar
fred committed
104
105
106
107
108
109
110
111
112
113
114
115
        for i in range(nb_lines):
            for j, cell in enumerate(grid[i]):
                if grid[i][j] is None:
                    continue
                t = 1
                try:
                    while grid[i][j+t] == cell:
                        cell.w += 1
                        grid[i][j+t] = None
                        t += 1
                except IndexError:
                    pass
fred's avatar
fred committed
116
            grid[i] = [x for x in grid[i] if x is not None]
fred's avatar
fred committed
117
118
119
120
121
122
123
124

        for i in range(nb_lines):
            grid[i] = [x for x in grid[i] if x is not None]
            for j, cell in enumerate(grid[i]):
                if grid[i][j] is None:
                    continue
                t = 1
                try:
fred's avatar
fred committed
125
126
127
128
129
130
131
                    while True:
                        same_cell_below = [(bj, x) for bj, x in enumerate(grid[i+cell.h])
                                           if x == cell and x.y == cell.y and x.w == cell.w]
                        if not same_cell_below:
                            break
                        bj, same_cell_below = same_cell_below[0]
                        del grid[i+cell.h][bj]
fred's avatar
fred committed
132
133
134
135
136
                        cell.h += 1
                except IndexError:
                    pass

        context['grid'] = grid
fred's avatar
fred committed
137
        context['times'] = times
138
139
        context['weekdays'] = ['Lundi', 'Mardi', 'Mercredi', 'Jeudi',
                'Vendredi', 'Samedi', 'Dimanche']
fred's avatar
fred committed
140
141
142
143
144

        return context

grid = Grid.as_view()

145

fred's avatar
fred committed
146
147
148
class Home(TemplateView):
    template_name = 'home.html'

149
150
151
152
153
    def get_context_data(self, **kwargs):
        context = super(Home, self).get_context_data(**kwargs)
        context['emissions'] = Emission.objects.all().order_by('?')[:5]
        return context

fred's avatar
fred committed
154
home = Home.as_view()
155
156
157
158
159
160
161
162
163
164

class Get(TemplateView):
    template_name = 'get.html'

    def get_context_data(self, **kwargs):
        context = super(Get, self).get_context_data(**kwargs)
        context['emissions'] = Emission.objects.all().order_by('title')
        return context

get = Get.as_view()
Simon Daron's avatar
Simon Daron committed
165
166
167
168
169
170

class Player(TemplateView):
    template_name = 'player.html'

    def get_context_data(self, **kwargs):
        context = super(Player, self).get_context_data(**kwargs)
171
        context['emissions'] = Emission.objects.all().order_by('title')
Simon Daron's avatar
Simon Daron committed
172
173
174
        return context

player = Player.as_view()