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
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()
22 Episode.objects.filter(emission__archived=False, soundfile__isnull=False)
23 .order_by('-soundfile__creation_timestamp')
24 .select_related('emission')
26 if episode.emission_id in emissions_seen:
28 emissions_seen.add(episode.emission_id)
30 if len(shows) > settings.HOME_EMISSIONS_COUNT:
31 context['shows'] = shows[:12]
32 context['shows_display_more'] = True
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
43 context['newsitems'] = (
44 NewsItem.objects.exclude(date__gt=datetime.date.today())
45 .exclude(expiration_date__lt=datetime.date.today())
48 if context['newsitems'].count() > settings.HOME_NEWSITEMS_COUNT:
49 context['newsitems'] = context['newsitems'][:12]
50 context['newsitems_display_more'] = True
52 context['focus'] = list(
53 Focus.objects.filter(current=True).select_related(
54 'emission', 'newsitem', 'soundfile', 'episode', 'newsitem__category'
58 # create focus objects for episode scheduled for new broadcasting day.
60 Diffusion.objects.filter(datetime__gt=now())
62 .select_related('episode', 'episode__emission')[: settings.HOME_FOCUS_COUNT]
64 context['focus'].extend([Focus(episode=x.episode) for x in diffusions])
66 random.shuffle(context['focus'])
67 context['focus'] = context['focus'][: settings.HOME_FOCUS_COUNT]
75 class Emissions(panikweb.views.Emissions):
76 title = _('All Shows')
78 def get_queryset(self):
79 return super().get_queryset().exclude(categories__slug='collection')
82 emissions = Emissions.as_view()
85 class Collections(panikweb.views.Emissions):
86 title = _('All Collections')
88 def get_queryset(self):
89 return super().get_queryset().filter(categories__slug='collection')
92 collections = Collections.as_view()
95 class TagItems(TemplateView):
96 template_name = 'tag-items.html'
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'
104 context['episodes'] = Episode.objects.filter(tags__slug__in=[kwargs['slug']]).order_by(
105 '-creation_timestamp'
110 tag_items = TagItems.as_view()
113 class ComingSoon(TemplateView):
114 template_name = 'comingsoon.html'
117 coming_soon = ComingSoon.as_view()
120 class Calendar(TemplateView):
121 template_name = 'calendar.html'
123 def get_context_data(self, **kwargs):
124 context = super().get_context_data(**kwargs)
126 year = datetime.date.today().isocalendar()[0]
127 week = datetime.date.today().isocalendar()[1]
129 date = panikweb.utils.tofirstdayinisoweek(year, week)
130 date = datetime.datetime(*date.timetuple()[:3])
132 context['weekdays'] = [datetime.date(2018, 1, x) for x in range(1, 8)]
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)]
138 2 * emissions_app_settings.DAY_HOUR_START
139 + (1 if emissions_app_settings.DAY_MINUTE_START else 0) :
142 : 2 * emissions_app_settings.DAY_HOUR_START
143 + (1 if emissions_app_settings.DAY_MINUTE_START else 0)
147 context['times'] = times
149 context['weeks'] = weeks = []
150 for week in range(4):
151 start_date = date + datetime.timedelta(days=7 * week)
152 program = period_program(
154 start_date + datetime.timedelta(days=7),
155 prefetch_categories=False,
156 include_nonstop=False,
160 {'start_date': start_date, 'end_date': start_date + datetime.timedelta(days=6), 'grid': grid}
162 start_time = datetime.time(
163 emissions_app_settings.DAY_HOUR_START, emissions_app_settings.DAY_MINUTE_START
165 base_start_time = datetime.datetime.combine(start_date, start_time)
166 for line_no in range(48):
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
173 cell_end_time = cell_start_time + datetime.timedelta(minutes=30)
175 x for x in program if x.datetime < cell_end_time and x.end_datetime > cell_start_time
178 grid_cell = getattr(shows[0], 'grid_cell', None)
180 grid_cell['rowspan'] += 1
182 shows[0].grid_cell = {'show': shows[0], 'rowspan': 1}
183 grid_row.append(shows[0].grid_cell)
185 grid_row.append({'empty': True})
190 calendar = Calendar.as_view()