]> git.0d.be Git - panikweb-studioneau.git/blob - panikweb_studioneau/views.py
adapt to new dawn settings
[panikweb-studioneau.git] / panikweb_studioneau / views.py
1 import datetime
2 import random
3
4 import panikweb.utils
5 import panikweb.views
6 from django.conf import settings
7 from django.utils.timezone import now
8 from django.utils.translation import ugettext_lazy as _
9 from django.views.generic.base import TemplateView
10 from emissions.app_settings import app_settings as emissions_app_settings
11 from emissions.models import Diffusion, Emission, Episode, Focus, NewsItem, Schedule
12 from emissions.utils import period_program
13
14
15 class Home(panikweb.views.Home):
16     def get_context_data(self, **kwargs):
17         context = super().get_context_data(**kwargs)
18         # get shows with a recent sound
19         context['shows'] = shows = []
20         emissions_seen = set()
21         for episode in (
22             Episode.objects.filter(emission__archived=False, soundfile__isnull=False)
23             .order_by('-soundfile__creation_timestamp')
24             .select_related('emission')
25         ):
26             if episode.emission_id in emissions_seen:
27                 continue
28             emissions_seen.add(episode.emission_id)
29             shows.append(episode)
30             if len(shows) > settings.HOME_EMISSIONS_COUNT:
31                 context['shows'] = shows[:12]
32                 context['shows_display_more'] = True
33                 break
34
35         if settings.HOME_COLLECTION_COUNT:
36             context['collections'] = Emission.objects.filter(archived=False).filter(
37                 categories__slug='collection'
38             )[: settings.HOME_COLLECTION_COUNT]
39             if context['collections'].count() > settings.HOME_COLLECTION_COUNT:
40                 context['collections'] = context['collections'][: settings.HOME_COLLECTION_COUNT]
41                 context['collections_display_more'] = True
42
43         context['newsitems'] = (
44             NewsItem.objects.exclude(date__gt=datetime.date.today())
45             .exclude(expiration_date__lt=datetime.date.today())
46             .order_by('-date')
47         )
48         if context['newsitems'].count() > settings.HOME_NEWSITEMS_COUNT:
49             context['newsitems'] = context['newsitems'][:12]
50             context['newsitems_display_more'] = True
51
52         context['focus'] = list(
53             Focus.objects.filter(current=True).select_related(
54                 'emission', 'newsitem', 'soundfile', 'episode', 'newsitem__category'
55             )
56         )
57
58         # create focus objects for episode scheduled for new broadcasting day.
59         diffusions = (
60             Diffusion.objects.filter(datetime__gt=now())
61             .order_by('datetime')
62             .select_related('episode', 'episode__emission')[: settings.HOME_FOCUS_COUNT]
63         )
64         context['focus'].extend([Focus(episode=x.episode) for x in diffusions])
65
66         random.shuffle(context['focus'])
67         context['focus'] = context['focus'][: settings.HOME_FOCUS_COUNT]
68
69         return context
70
71
72 home = Home.as_view()
73
74
75 class Emissions(panikweb.views.Emissions):
76     title = _('All Shows')
77
78     def get_queryset(self):
79         return super().get_queryset().exclude(categories__slug='collection')
80
81
82 emissions = Emissions.as_view()
83
84
85 class Collections(panikweb.views.Emissions):
86     title = _('All Collections')
87
88     def get_queryset(self):
89         return super().get_queryset().filter(categories__slug='collection')
90
91
92 collections = Collections.as_view()
93
94
95 class TagItems(TemplateView):
96     template_name = 'tag-items.html'
97
98     def get_context_data(self, **kwargs):
99         context = super().get_context_data(**kwargs)
100         context['tag'] = kwargs['slug']
101         context['emissions'] = Emission.objects.filter(tags__slug__in=[kwargs['slug']]).order_by(
102             '-creation_timestamp'
103         )
104         context['episodes'] = Episode.objects.filter(tags__slug__in=[kwargs['slug']]).order_by(
105             '-creation_timestamp'
106         )
107         return context
108
109
110 tag_items = TagItems.as_view()
111
112
113 class ComingSoon(TemplateView):
114     template_name = 'comingsoon.html'
115
116
117 coming_soon = ComingSoon.as_view()
118
119
120 class Calendar(TemplateView):
121     template_name = 'calendar.html'
122
123     def get_context_data(self, **kwargs):
124         context = super().get_context_data(**kwargs)
125
126         year = datetime.date.today().isocalendar()[0]
127         week = datetime.date.today().isocalendar()[1]
128
129         date = panikweb.utils.tofirstdayinisoweek(year, week)
130         date = datetime.datetime(*date.timetuple()[:3])
131
132         context['weekdays'] = [datetime.date(2018, 1, x) for x in range(1, 8)]
133
134         nb_lines = 2 * 24  # the cells are half hours
135         times = ['%02d:%02d' % (x / 2, x % 2 * 30) for x in range(nb_lines)]
136         times = (
137             times[
138                 2 * emissions_app_settings.DAY_HOUR_START
139                 + (1 if emissions_app_settings.DAY_MINUTE_START else 0) :
140             ]
141             + times[
142                 : 2 * emissions_app_settings.DAY_HOUR_START
143                 + (1 if emissions_app_settings.DAY_MINUTE_START else 0)
144             ]
145         )
146
147         context['times'] = times
148
149         context['weeks'] = weeks = []
150         for week in range(4):
151             start_date = date + datetime.timedelta(days=7 * week)
152             program = period_program(
153                 start_date,
154                 start_date + datetime.timedelta(days=7),
155                 prefetch_categories=False,
156                 include_nonstop=False,
157             )
158             grid = []
159             weeks.append(
160                 {'start_date': start_date, 'end_date': start_date + datetime.timedelta(days=6), 'grid': grid}
161             )
162             start_time = datetime.time(
163                 emissions_app_settings.DAY_HOUR_START, emissions_app_settings.DAY_MINUTE_START
164             )
165             base_start_time = datetime.datetime.combine(start_date, start_time)
166             for line_no in range(48):
167                 grid_row = []
168                 grid.append(grid_row)
169                 for column_no in range(7):
170                     cell_start_time = base_start_time + datetime.timedelta(
171                         days=column_no, minutes=line_no * 30
172                     )
173                     cell_end_time = cell_start_time + datetime.timedelta(minutes=30)
174                     shows = [
175                         x for x in program if x.datetime < cell_end_time and x.end_datetime > cell_start_time
176                     ]
177                     if shows:
178                         grid_cell = getattr(shows[0], 'grid_cell', None)
179                         if grid_cell:
180                             grid_cell['rowspan'] += 1
181                         else:
182                             shows[0].grid_cell = {'show': shows[0], 'rowspan': 1}
183                             grid_row.append(shows[0].grid_cell)
184                     else:
185                         grid_row.append({'empty': True})
186
187         return context
188
189
190 calendar = Calendar.as_view()