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