]> git.0d.be Git - panikweb.git/commitdiff
auto grid
authorFrédéric Péters <fpeters@0d.be>
Wed, 14 Aug 2013 15:33:42 +0000 (17:33 +0200)
committerFrédéric Péters <fpeters@0d.be>
Wed, 14 Aug 2013 15:33:42 +0000 (17:33 +0200)
panikweb/urls.py
panikweb/views.py
panikweb_templates/templates/grid.html [new file with mode: 0644]

index 567c57ee28fe70c4b06b27741a5621bc3e019809..278279474c6306092e6673377ed3d5f18f3d636b 100644 (file)
@@ -8,6 +8,7 @@ admin.autodiscover()
 urlpatterns = patterns('',
     url(r'^$', 'panikweb.views.home', name='home'),
     url(r'^program$', 'panikweb.views.program', name='program'),
+    url(r'^grid$', 'panikweb.views.grid', name='grid'),
     url(r'^emissions/', include('emissions.urls')),
     url(r'^ckeditor/', include('ckeditor.urls')),
 
index b3d9a6adfe537a675c7cd7f927f3011ace087f27..1ae7badff4f983cdf022f9162710fbe638bab0db 100644 (file)
@@ -1,4 +1,5 @@
 import datetime
+import math
 
 from django.views.generic.base import TemplateView
 
@@ -19,6 +20,108 @@ class ProgramView(TemplateView):
 
 program = ProgramView.as_view()
 
+class TimeCell:
+    emissions = None
+    nonstop = None
+    w = 1
+    h = 1
+
+    def __init__(self):
+        self.emissions = []
+
+    def add_emission(self, emission):
+        self.emissions.append(emission)
+
+    def __unicode__(self):
+        if self.emissions:
+            return ', '.join([x.title for x in self.emissions])
+        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 = []
+
+        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())
+
+            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))):
+                if grid[row_start+step][day_no] is None:
+                    print 'creating a time cell at', row_start+step, day_no
+                    grid[row_start+step][day_no] = TimeCell()
+                grid[row_start+step][day_no].add_emission(schedule.emission)
+
+
+        # start grid at 5am
+        grid = grid[2*5:] + grid[:2*5]
+
+        # merge adjencent cells
+        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
+
+        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:
+                    while grid[i+t][j] == cell and grid[i+t][j].w == cell.w:
+                        cell.h += 1
+                        grid[i+t][j] = None
+                        t += 1
+                except IndexError:
+                    pass
+
+        context['grid'] = grid
+
+        return context
+
+grid = Grid.as_view()
+
 
 class Home(TemplateView):
     template_name = 'home.html'
diff --git a/panikweb_templates/templates/grid.html b/panikweb_templates/templates/grid.html
new file mode 100644 (file)
index 0000000..7514817
--- /dev/null
@@ -0,0 +1,21 @@
+{% extends "base.html" %}
+
+{% block content %}
+
+<h2>Grille</h2>
+
+<table border=1>
+{% for time_cells in grid %}
+<tr>
+  <td>ti:me</td>
+  {% for cell in time_cells %}
+  <td {% if cell.w > 1 %}colspan="{{cell.w}}"{% endif %}
+      {% if cell.h > 1 %}rowspan="{{cell.h}}"{% endif %}>{{ cell }}</td>
+  {% endfor %}
+  <td>ti:me</td>
+</tr>
+{% endfor %}
+</table>
+
+
+{% endblock %}