a11y: mark illustrative images with empty alt attribute
[panikweb-esperanzah.git] / panikweb_esperanzah / views.py
1 import datetime
2
3 from combo.data.models import Page
4 from django.http import HttpResponseRedirect
5 from django.urls import reverse
6 from django.views.generic.base import TemplateView
7 from emissions.models import Diffusion, Emission, Episode, NewsItem
8
9 import panikweb.views
10
11
12 class Home(panikweb.views.Home):
13     def get_context_data(self, **kwargs):
14         context = super().get_context_data(**kwargs)
15         context['emissions'] = list(Emission.objects.filter(archived=False).order_by('title'))
16         # force detour-vers-le-futur to go first
17         detour = [x for x in context['emissions'] if x.slug == 'detour-vers-le-futur']
18         context['emissions'] = detour + [x for x in context['emissions'] if x.slug != 'detour-vers-le-futur']
19         context['newsitems'] = (
20             NewsItem.objects.exclude(date__gt=datetime.date.today())
21             .exclude(expiration_date__lt=datetime.date.today())
22             .order_by('-date')[:3]
23         )
24         context['extra_pages'] = Page.objects.filter(exclude_from_navigation=False)
25         return context
26
27     def get(request, *args, **kwargs):
28         emissions = Emission.objects.filter(archived=False)
29         if len(emissions) == 1:
30             return HttpResponseRedirect(reverse('emission-view', kwargs={'slug': emissions[0].slug}))
31         return super().get(request, *args, **kwargs)
32
33
34 home = Home.as_view()
35
36
37 class EmissionDetailView(panikweb.views.EmissionDetailView):
38     def get_emission_context(self, emission, episode_ids=None):
39         context = super().get_emission_context(emission, episode_ids)
40
41         if emission.archived:
42             current_year = emission.creation_timestamp.replace(month=5, day=1)
43         else:
44             current_year = datetime.datetime.now().replace(month=5, day=1).replace(year=2021)
45
46         episodes_queryset = Episode.objects.select_related()
47         if episode_ids is not None:
48             episodes_queryset = episodes_queryset.filter(id__in=episode_ids)
49         else:
50             episodes_queryset = episodes_queryset.filter(emission=emission)
51
52         context['all_episodes'] = (
53             episodes_queryset.filter(creation_timestamp__gt=current_year)
54             .extra(
55                 select={
56                     'first_diffusion': 'emissions_diffusion.datetime',
57                 },
58                 select_params=(False, True),
59                 where=[
60                     '''datetime = (SELECT MIN(datetime)
61                                                 FROM emissions_diffusion
62                                                WHERE episode_id = emissions_episode.id
63                                              )'''
64                 ],
65                 tables=['emissions_diffusion'],
66             )
67             .order_by('first_diffusion')
68             .distinct()
69         )
70
71         return context
72
73
74 emission = EmissionDetailView.as_view()
75
76
77 class ArchivesView(TemplateView):
78     template_name = 'archives.html'
79
80     def get_context_data(self, **kwargs):
81         context = super().get_context_data(**kwargs)
82         context['diffusions'] = (
83             Diffusion.objects.filter(episode__soundfile__isnull=False).distinct().order_by('-datetime')
84         )
85         return context
86
87
88 archives = ArchivesView.as_view()