7 from datetime import date, datetime, timedelta
9 from django.conf import settings
10 from django.contrib.sites.models import Site
11 from django.contrib.syndication.views import Feed, add_domain
12 from django.core.files.storage import default_storage
13 from django.core.paginator import Paginator
14 from django.http import Http404, HttpResponse, JsonResponse
15 from django.urls import reverse
16 from django.utils.encoding import force_text, python_2_unicode_compatible
17 from django.utils.feedgenerator import Atom1Feed, Rss201rev2Feed
18 from django.views.decorators.cache import cache_control
19 from django.views.decorators.csrf import csrf_exempt
20 from django.views.generic import RedirectView
21 from django.views.generic.base import TemplateView
22 from django.views.generic.dates import MonthArchiveView, _date_from_string
23 from django.views.generic.detail import DetailView
24 from emissions.models import (
36 from emissions.utils import period_program, whatsonair
37 from emissions.views import EmissionEpisodeMixin
38 from haystack.query import SearchQuerySet
39 from newsletter.forms import SubscribeForm
40 from nonstop.models import SomaLogLine
41 from nonstop.utils import get_current_nonstop_track
42 from panikombo.models import ItemTopik
48 def get_emission_context(self, emission, episode_ids=None):
51 # get all episodes, with an additional attribute to get the date of
52 # their first diffusion
53 episodes_queryset = Episode.objects.select_related()
54 if episode_ids is not None:
55 episodes_queryset = episodes_queryset.filter(id__in=episode_ids)
57 episodes_queryset = episodes_queryset.filter(emission=emission)
59 if settings.USE_AGENDA_ONLY_FIELD:
60 episodes_queryset = episodes_queryset.exclude(agenda_only=True)
62 context['episodes'] = (
63 episodes_queryset.extra(
65 'first_diffusion': 'emissions_diffusion.datetime',
67 select_params=(False, True),
69 '''datetime = (SELECT MIN(datetime)
70 FROM emissions_diffusion
71 WHERE episode_id = emissions_episode.id
72 AND datetime <= CURRENT_TIMESTAMP)'''
74 tables=['emissions_diffusion'],
76 .order_by('-first_diffusion')
80 context['all_episodes'] = (
81 episodes_queryset.extra(
83 'first_diffusion': 'emissions_diffusion.datetime',
85 select_params=(False, True),
87 '''datetime = (SELECT MIN(datetime)
88 FROM emissions_diffusion
89 WHERE episode_id = emissions_episode.id)'''
91 tables=['emissions_diffusion'],
93 .order_by('-first_diffusion')
97 context['futurEpisodes'] = (
98 episodes_queryset.extra(
100 'first_diffusion': 'emissions_diffusion.datetime',
102 select_params=(False, True),
104 '''datetime = (SELECT MIN(datetime)
105 FROM emissions_diffusion
106 WHERE episode_id = emissions_episode.id
107 AND datetime > CURRENT_TIMESTAMP)'''
109 tables=['emissions_diffusion'],
111 .order_by('first_diffusion')
115 # get all related soundfiles in a single query
117 if episode_ids is not None:
118 for episode_id in episode_ids:
119 soundfiles[episode_id] = None
121 for episode in Episode.objects.filter(emission=emission):
122 soundfiles[episode.id] = None
124 for soundfile in SoundFile.objects.select_related().filter(
125 podcastable=True, fragment=False, episode__emission=emission
127 soundfiles[soundfile.episode_id] = soundfile
129 Episode.set_prefetched_soundfiles(soundfiles)
131 # context['futurEpisodes'] = context['episodes'].filter(first_diffusion='2013')[0:3]
136 class EmissionDetailView(DetailView, EmissionMixin):
139 def get_context_data(self, **kwargs):
140 context = super().get_context_data(**kwargs)
141 context['schedules'] = (
142 Schedule.objects.select_related().filter(emission=self.object).order_by('rerun', 'datetime')
145 NewsItem.objects.all()
146 .filter(emission=self.object.id)
147 .exclude(expiration_date__lt=date.today()) # expiration date
148 .exclude(date__lt=date.today() - timedelta(days=60))
149 .order_by('-date')[:3]
152 nonstop_object = Nonstop.objects.get(slug=self.object.slug)
153 except Nonstop.DoesNotExist:
157 dates = [today - timedelta(days=x) for x in range(7)]
158 if datetime.now().time() < nonstop_object.start:
160 context['nonstop'] = nonstop_object
161 context['nonstop_dates'] = dates
162 context.update(self.get_emission_context(self.object))
166 emission = EmissionDetailView.as_view()
169 class EpisodeDetailView(EmissionEpisodeMixin, DetailView, EmissionMixin):
172 def get_context_data(self, **kwargs):
173 context = super().get_context_data(**kwargs)
174 context['diffusions'] = (
175 Diffusion.objects.select_related().filter(episode=self.object.id).order_by('datetime')
178 context['emission'] = context['episode'].emission
179 except Emission.DoesNotExist:
181 if self.kwargs.get('emission_slug') != context['emission'].slug:
183 context.update(self.get_emission_context(context['emission']))
184 context['topik_pages'] = [x.page for x in ItemTopik.objects.filter(episode=self.object)]
188 episode = EpisodeDetailView.as_view()
191 class NonstopPlaylistView(TemplateView):
192 template_name = 'nonstop_playlist.html'
194 def get_context_data(self, **kwargs):
195 context = super().get_context_data(**kwargs)
197 context['emission'] = Emission.objects.get(slug=kwargs.get('slug'))
198 except Emission.DoesNotExist:
201 context['date'] = date(int(kwargs.get('year')), int(kwargs.get('month')), int(kwargs.get('day')))
204 context['future'] = context['date'] >= date.today()
206 nonstop_object = Nonstop.objects.get(slug=kwargs.get('slug'))
208 int(kwargs.get('year')),
209 int(kwargs.get('month')),
210 int(kwargs.get('day')),
211 nonstop_object.start.hour,
212 nonstop_object.start.minute,
215 int(kwargs.get('year')),
216 int(kwargs.get('month')),
217 int(kwargs.get('day')),
218 nonstop_object.end.hour,
219 nonstop_object.end.minute,
222 end = end + timedelta(days=1)
223 context['tracks'] = SomaLogLine.objects.filter(
224 play_timestamp__gte=start, play_timestamp__lte=end, on_air=True
229 nonstop_playlist = NonstopPlaylistView.as_view()
232 class EmissionEpisodesDetailView(DetailView, EmissionMixin):
234 template_name = 'emissions/episodes.html'
236 def get_context_data(self, **kwargs):
237 context = super().get_context_data(**kwargs)
238 context['schedules'] = (
239 Schedule.objects.select_related().filter(emission=self.object).order_by('rerun', 'datetime')
242 for schedule in context['schedules']:
245 count_per_month += bin(schedule.weeks).count('1')
247 context['count_per_month'] = count_per_month
249 context['search_query'] = self.request.GET.get('q')
250 if context['search_query']:
255 .filter(emission_slug_exact=self.object.slug, text=context['search_query'])
257 episode_ids = [x.pk for x in sqs]
261 context.update(self.get_emission_context(self.object, episode_ids=episode_ids))
265 emissionEpisodes = EmissionEpisodesDetailView.as_view()
268 class SoundFileEmbedView(DetailView):
270 template_name = 'soundfiles/embed.html'
272 def get_context_data(self, **kwargs):
273 context = super().get_context_data(**kwargs)
274 if self.kwargs.get('episode_slug') != self.object.episode.slug:
276 if self.kwargs.get('emission_slug') != self.object.episode.emission.slug:
278 context['episode'] = self.object.episode
282 soundfile_embed = SoundFileEmbedView.as_view()
285 class EpisodeEmbedRedirect(RedirectView):
286 def get_redirect_url(self, **kwargs):
288 soundfile = SoundFile.objects.get(
289 episode__slug=kwargs['episode_slug'],
290 episode__emission__slug=kwargs['emission_slug'],
293 except SoundFile.DoesNotExist:
295 kwargs['pk'] = soundfile.id
296 return reverse('soundfile-embed-view', kwargs=kwargs)
299 episode_embed_redirect = EpisodeEmbedRedirect.as_view()
302 class SoundFileDialogEmbedView(DetailView):
304 template_name = 'soundfiles/dialog-embed.html'
306 def get_context_data(self, **kwargs):
307 context = super().get_context_data(**kwargs)
308 if self.kwargs.get('episode_slug') != self.object.episode.slug:
310 if self.kwargs.get('emission_slug') != self.object.episode.emission.slug:
312 context['episode'] = self.object.episode
316 soundfile_dlg_embed = SoundFileDialogEmbedView.as_view()
319 class ProgramView(TemplateView):
320 template_name = 'program.html'
322 def get_context_data(self, year=None, week=None, **kwargs):
323 context = super().get_context_data(**kwargs)
325 context['weekday'] = datetime.today().weekday()
327 context['week'] = week = int(week) if week is not None else datetime.today().isocalendar()[1]
328 context['year'] = year = int(year) if year is not None else datetime.today().isocalendar()[0]
329 if context['week'] > 53:
331 context['week_first_day'] = utils.tofirstdayinisoweek(year, week)
332 context['week_last_day'] = context['week_first_day'] + timedelta(days=6)
337 program = ProgramView.as_view()
340 @python_2_unicode_compatible
347 def __init__(self, i, j):
352 def add_schedule(self, schedule):
353 end_time = schedule.datetime + timedelta(minutes=schedule.get_duration())
354 self.time_label = '%02d:%02d-%02d:%02d' % (
355 schedule.datetime.hour,
356 schedule.datetime.minute,
360 self.schedules.append(schedule)
362 def sorted_schedules(self):
363 return sorted(self.schedules, key=lambda x: x.week_sort_key())
367 return ', '.join([x.emission.title for x in self.schedules])
371 def __eq__(self, other):
372 return force_text(self) == force_text(other) and self.time_label == other.time_label
375 class Grid(TemplateView):
376 template_name = 'grid.html'
378 def get_context_data(self, **kwargs):
379 context = super().get_context_data(**kwargs)
381 nb_lines = 2 * 24 # the cells are half hours
384 times = ['%02d:%02d' % (x / 2, x % 2 * 30) for x in range(nb_lines)]
385 # start grid after the night programs
387 times[2 * Schedule.DAY_HOUR_START + (1 if Schedule.DAY_MINUTE_START else 0) :]
388 + times[: 2 * Schedule.DAY_HOUR_START + (1 if Schedule.DAY_MINUTE_START else 0)]
392 for nonstop in Nonstop.objects.all():
393 if nonstop.start == nonstop.end:
395 if nonstop.start < nonstop.end:
398 nonstop.start.hour + nonstop.start.minute / 60.0,
399 nonstop.end.hour + nonstop.end.minute / 60.0,
400 nonstop.get_public_label(),
409 nonstop.start.hour + nonstop.start.minute / 60.0,
411 nonstop.get_public_label(),
419 nonstop.end.hour + nonstop.end.minute / 60.0,
420 nonstop.get_public_label(),
427 for i in range(nb_lines):
430 grid[-1].append(TimeCell(i, j))
433 nonstop = [x for x in nonstops if i >= x[0] * 2 and i < x[1] * 2][0]
435 nonstop = [0, 24, '', '', None]
436 for time_cell in grid[-1]:
437 time_cell.nonstop = nonstop[2]
438 time_cell.nonstop_slug = nonstop[3]
439 time_cell.redirect_path = nonstop[4].redirect_path if nonstop[4] else None
440 if nonstop[1] == Schedule.DAY_HOUR_START + Schedule.DAY_MINUTE_START / 60:
441 # the one ending at 4:30am will be cut down, so we inscribe
442 # its duration manually
443 time_cell.time_label = '%02d:00-%02d:%02d' % (
446 Schedule.DAY_MINUTE_START,
450 Schedule.objects.prefetch_related('emission__categories').select_related().order_by('datetime')
452 row_start = schedule.datetime.hour * 2 + int(math.ceil(schedule.datetime.minute / 30))
453 if schedule.get_duration() < 30:
454 # special case for an emission during 12:45-13:00
455 row_start = schedule.datetime.hour * 2 + int(math.floor(schedule.datetime.minute / 30))
456 day_no = schedule.get_weekday()
458 for step in range(int(math.ceil(schedule.get_duration() / 30.0))):
459 if grid[(row_start + step) % nb_lines][day_no] is None:
460 grid[(row_start + step) % nb_lines][day_no] = TimeCell()
461 grid[(row_start + step) % nb_lines][day_no].add_schedule(schedule)
463 # start grid after the night programs
465 grid[2 * Schedule.DAY_HOUR_START + (1 if Schedule.DAY_MINUTE_START else 0) :]
466 + grid[: 2 * Schedule.DAY_HOUR_START + (1 if Schedule.DAY_MINUTE_START else 0)]
469 # look for the case where the same emission has different schedules for
470 # the same time cell, for example if it lasts one hour the first week,
471 # and two hours the third week.
472 for i in range(nb_lines):
473 grid[i] = [x for x in grid[i] if x is not None]
474 for j, cell in enumerate(grid[i]):
475 if grid[i][j] is None:
477 if len(grid[i][j].schedules) > 1:
478 time_cell_emissions = {}
479 for schedule in grid[i][j].schedules:
480 if not schedule.emission.id in time_cell_emissions:
481 time_cell_emissions[schedule.emission.id] = []
482 time_cell_emissions[schedule.emission.id].append(schedule)
483 for schedule_list in time_cell_emissions.values():
484 if len(schedule_list) == 1:
486 # here it is, same cell, same emission, several
488 schedule_list.sort(key=lambda x: x.get_duration())
490 schedule = schedule_list[0]
491 end_time = schedule.datetime + timedelta(minutes=schedule.get_duration())
492 grid[i][j].time_label = '%02d:%02d-%02d:%02d' % (
493 schedule.datetime.hour,
494 schedule.datetime.minute,
499 schedule_list.sort(key=lambda x: x.weeks)
500 for schedule in schedule_list[1:]:
501 grid[i][j].schedules.remove(schedule)
502 end_time = schedule.datetime + timedelta(minutes=schedule.get_duration())
503 if schedule_list[0].get_duration() == schedule.get_duration():
504 # same duration, append week info
505 schedule_list[0].time_label_extra = ', %s' % (schedule.weeks_string,)
507 # different durations, also append other
509 schedule_list[0].time_label_extra = ', -%02d:%02d %s' % (
512 schedule.weeks_string,
518 # 1st thing is to merge cells on the same line, this will mostly catch
519 # consecutive nonstop cells
520 for i in range(nb_lines):
521 for j, cell in enumerate(grid[i]):
522 if grid[i][j] is None:
526 # if the cells are identical, they are removed from the
527 # grid, and current cell width is increased
528 while grid[i][j + t] == cell:
530 grid[i][j + t] = None
535 # once we're done we remove empty cells
536 grid[i] = [x for x in grid[i] if x is not None]
538 # 2nd thing is to merge cells vertically, this is emissions that last
539 # for more than 30 minutes
540 for i in range(nb_lines):
541 grid[i] = [x for x in grid[i] if x is not None]
542 for j, cell in enumerate(grid[i]):
543 if grid[i][j] is None:
548 # we look if the next time cell has the same emissions
551 for bj, x in enumerate(grid[i + cell.h])
552 if x == cell and x.y == cell.y and x.w == cell.w
555 # if the cell was identical, we remove it and
556 # increase current cell height
557 bj, same_cell_below = same_cell_below[0]
558 del grid[i + cell.h][bj]
561 # if the cell is different, we have a closer look
562 # to it, so we can remove emissions that will
563 # already be mentioned in the current cell.
566 # - 7am30, seuls contre tout, 1h30
567 # - 8am, du pied gauche & la voix de la rue, 1h
568 # should produce: (this is case A)
570 # | seuls contre tout |
571 # |---------------------|
574 # | la voix de la rue |
576 # On the other hand, if all three emissions started
577 # at 7am30, we want: (this is case B)
579 # | seuls contre tout |
581 # | la voix de la rue |
582 # that is we merge all of them, ignoring the fact
583 # that the other emissions will stop at 8am30
584 current_cell_schedules = set(grid[i][j].schedules)
585 current_cell_emissions = {x.emission for x in current_cell_schedules}
587 while True and current_cell_schedules:
588 same_cell_below = [x for x in grid[i + cursor] if x.y == grid[i][j].y]
589 if not same_cell_below:
592 same_cell_below = same_cell_below[0]
593 same_cell_below_emissions = {x.emission for x in same_cell_below.schedules}
595 if current_cell_emissions.issubset(same_cell_below_emissions):
596 # this handles case A (see comment above)
597 for schedule in current_cell_schedules:
598 if schedule in same_cell_below.schedules:
599 same_cell_below.schedules.remove(schedule)
600 elif same_cell_below_emissions and current_cell_emissions.issuperset(
601 same_cell_below_emissions
603 # this handles case B (see comment above)
604 # we set the cell time label to the longest
606 grid[i][j].time_label = same_cell_below.time_label
607 # then we sort emissions so the longest are
609 grid[i][j].schedules.sort(key=lambda x: -x.get_duration())
610 # then we add individual time labels to the
612 for schedule in current_cell_schedules:
613 if schedule not in same_cell_below.schedules:
614 end_time = schedule.datetime + timedelta(
615 minutes=schedule.get_duration()
617 schedule.time_label = '%02d:%02d-%02d:%02d' % (
618 schedule.datetime.hour,
619 schedule.datetime.minute,
624 grid[i + cursor].remove(same_cell_below)
625 elif same_cell_below_emissions and current_cell_emissions.intersection(
626 same_cell_below_emissions
628 same_cell_below.schedules = [
630 for x in same_cell_below.schedules
631 if x.emission not in current_cell_emissions or x.get_duration() < 30
638 # cut late night hours
642 context['grid'] = grid
643 context['times'] = times
644 context['categories'] = Category.objects.all()
645 # dates from Monday to Sunday
646 context['weekdays'] = [date(2018, 1, x) for x in range(1, 8)]
651 grid = Grid.as_view()
654 class Home(TemplateView):
655 template_name = 'home.html'
657 def get_context_data(self, **kwargs):
658 context = super().get_context_data(**kwargs)
659 context['emissions'] = Emission.objects.filter(archived=False).order_by('-creation_timestamp')[
660 : settings.HOME_EMISSIONS_COUNT
662 context['newsitems'] = NewsItem.objects.exclude(date__gt=date.today()).order_by('-date')[
663 : settings.HOME_NEWSITEMS_COUNT
666 context['soundfiles'] = (
667 SoundFile.objects.prefetch_related('episode__emission__categories')
668 .filter(podcastable=True, fragment=False)
672 'first_diffusion': 'emissions_diffusion.datetime',
674 select_params=(False, True),
676 '''datetime = (SELECT MIN(datetime)
677 FROM emissions_diffusion
678 WHERE episode_id = emissions_episode.id)'''
680 tables=['emissions_diffusion'],
682 .order_by('-creation_timestamp')
683 .distinct()[: settings.HOME_PODCASTS_COUNT]
686 context['newsletter_form'] = SubscribeForm()
691 home = Home.as_view()
694 class NewsItemView(DetailView):
697 def get_context_data(self, **kwargs):
698 context = super().get_context_data(**kwargs)
699 context['categories'] = NewsCategory.objects.all()
700 context['news'] = NewsItem.objects.exclude(date__gt=date.today()).order_by('-date')
701 context['topik_pages'] = [x.page for x in ItemTopik.objects.filter(newsitem=self.object)]
705 newsitemview = NewsItemView.as_view()
708 class News(TemplateView):
709 template_name = 'news.html'
711 def get_context_data(self, **kwargs):
712 context = super().get_context_data(**kwargs)
714 NewsItem.objects.exclude(date__gt=date.today()) # publication date
715 .exclude(expiration_date__lt=date.today()) # expiration date
716 .filter(got_focus__isnull=False)
717 .select_related('category')
718 .order_by('-date')[:10]
720 context['news'] = NewsItem.objects.exclude(date__gt=date.today()).order_by('-date')
721 context['news_not_expired'] = (
722 NewsItem.objects.exclude(date__gt=date.today())
723 .exclude(expiration_date__lt=date.today())
729 news = News.as_view()
732 class Agenda(TemplateView):
733 template_name = 'agenda.html'
735 def get_context_data(self, **kwargs):
736 context = super().get_context_data(**kwargs)
737 context['agenda'] = (
738 NewsItem.objects.exclude(date__gt=date.today())
739 .filter(event_date__gte=date.today())
740 .order_by('event_date')[:20]
742 context['news'] = NewsItem.objects.exclude(date__gt=date.today()).order_by('-date')
743 context['previous_month'] = datetime.today().replace(day=1) - timedelta(days=2)
747 agenda = Agenda.as_view()
750 class AgendaByMonth(MonthArchiveView):
751 template_name = 'agenda.html'
752 queryset = NewsItem.objects.filter(event_date__isnull=False)
754 date_field = 'event_date'
757 def get_context_data(self, **kwargs):
758 context = super().get_context_data(**kwargs)
759 context['agenda'] = context['object_list']
760 context['news'] = NewsItem.objects.all().order_by('-date')
764 agenda_by_month = AgendaByMonth.as_view()
767 class Emissions(TemplateView):
768 template_name = 'emissions.html'
770 def get_queryset(self):
771 return Emission.objects.prefetch_related('categories').filter(archived=False).order_by('title')
773 def get_context_data(self, **kwargs):
774 context = super().get_context_data(**kwargs)
775 context['emissions'] = self.get_queryset()
776 context['categories'] = Category.objects.all()
780 emissions = Emissions.as_view()
783 class EmissionsArchives(TemplateView):
784 template_name = 'emissions/archives.html'
786 def get_context_data(self, **kwargs):
787 context = super().get_context_data(**kwargs)
788 context['emissions'] = (
789 Emission.objects.prefetch_related('categories').filter(archived=True).order_by('title')
791 context['categories'] = Category.objects.all()
795 emissionsArchives = EmissionsArchives.as_view()
798 class Listen(TemplateView):
799 template_name = 'listen.html'
801 def get_context_data(self, **kwargs):
802 context = super().get_context_data(**kwargs)
804 SoundFile.objects.prefetch_related('episode__emission__categories')
805 .filter(podcastable=True, got_focus__isnull=False)
809 'first_diffusion': 'emissions_diffusion.datetime',
811 select_params=(False, True),
813 '''datetime = (SELECT MIN(datetime)
814 FROM emissions_diffusion
815 WHERE episode_id = emissions_episode.id)'''
817 tables=['emissions_diffusion'],
819 .order_by('-first_diffusion')
822 context['soundfiles'] = (
823 SoundFile.objects.prefetch_related('episode__emission__categories')
824 .filter(podcastable=True)
828 'first_diffusion': 'emissions_diffusion.datetime',
830 select_params=(False, True),
832 '''datetime = (SELECT MIN(datetime)
833 FROM emissions_diffusion
834 WHERE episode_id = emissions_episode.id)'''
836 tables=['emissions_diffusion'],
838 .order_by('-creation_timestamp')
845 listen = Listen.as_view()
848 @cache_control(max_age=15)
853 d['episode'] = {'title': d['episode'].title, 'url': d['episode'].get_absolute_url()}
854 if d.get('emission'):
856 if d['emission'].chat_open:
857 chat_url = reverse('emission-chat', kwargs={'slug': d['emission'].slug})
859 'title': d['emission'].title,
860 'slug': d['emission'].slug,
861 'url': d['emission'].get_absolute_url(),
865 redirect_path = d['nonstop'].redirect_path
867 'title': d['nonstop'].get_public_label(),
868 'slug': d['current_slot'].slug,
871 d['nonstop']['url'] = redirect_path
872 today = datetime.today()
873 d['nonstop']['playlist_url'] = reverse(
877 'month': today.month,
879 'slug': d['current_slot'].slug,
882 d.update(get_current_nonstop_track())
883 if d.get('current_slot'):
884 del d['current_slot']
885 return JsonResponse({'data': d})
888 class NewsItemDetailView(DetailView):
892 newsitem = NewsItemDetailView.as_view()
895 class RssCustomPodcastsFeed(Rss201rev2Feed):
896 def add_root_elements(self, handler):
897 super().add_root_elements(handler)
898 emission = self.feed.get('emission')
899 if emission and emission.image and emission.image.url:
900 image_url = emission.image.url
902 image_url = settings.PODCASTS_DEFAULT_IMAGE_PATH
903 image_url = urllib.parse.urljoin(self.feed['link'], image_url)
904 handler.startElement('image', {})
906 handler.addQuickElement('title', emission.title)
908 handler.addQuickElement('title', settings.RADIO_NAME)
909 handler.addQuickElement('url', image_url)
910 handler.endElement('image')
911 handler.addQuickElement('itunes:explicit', 'no') # invidividual items will get their own value
912 handler.addQuickElement('itunes:image', None, {'href': image_url})
914 if emission.subtitle:
915 handler.addQuickElement('itunes:subtitle', emission.subtitle)
916 for category in emission.categories.all():
917 if category.itunes_category:
918 handler.addQuickElement('itunes:category', None, {'text': category.itunes_category})
920 handler.addQuickElement('itunes:author', settings.RADIO_NAME)
921 handler.startElement('itunes:owner', {})
922 if emission and emission.email:
923 handler.addQuickElement('itunes:email', emission.email)
925 handler.addQuickElement('itunes:email', settings.DEFAULT_FROM_EMAIL)
926 handler.addQuickElement('itunes:name', settings.RADIO_NAME)
927 handler.endElement('itunes:owner')
929 def root_attributes(self):
930 attrs = super().root_attributes()
931 attrs['xmlns:dc'] = 'http://purl.org/dc/elements/1.1/'
932 attrs['xmlns:itunes'] = 'http://www.itunes.com/dtds/podcast-1.0.dtd'
935 def add_item_elements(self, handler, item):
936 super().add_item_elements(handler, item)
938 for tag in item.get('tags') or []:
939 handler.addQuickElement('dc:subject', tag)
940 if tag == 'explicit':
943 handler.addQuickElement('itunes:keywords', ','.join(item.get('tags')))
944 handler.addQuickElement('itunes:explicit', explicit)
945 episode = item.get('episode')
946 if episode and episode.image and episode.image.url:
947 image_url = urllib.parse.urljoin(self.feed['link'], episode.image.url)
948 handler.addQuickElement('itunes:image', None, {'href': image_url})
949 soundfile = item.get('soundfile')
950 if soundfile.duration:
951 handler.addQuickElement(
954 % (soundfile.duration / 3600, soundfile.duration % 3600 / 60, soundfile.duration % 60),
958 class PodcastsFeed(Feed):
959 title = '%s - Podcasts' % settings.RADIO_NAME
961 description_template = 'feed/soundfile.html'
962 feed_type = RssCustomPodcastsFeed
964 def get_feed(self, obj, request):
965 self.request = request
966 return super().get_feed(obj, request)
969 def description(self):
970 return settings.RADIO_META_DESCRIPTION
974 SoundFile.objects.select_related()
975 .filter(podcastable=True)
976 .exclude(file__isnull=True)
978 .order_by('-creation_timestamp')[:50]
981 def item_title(self, item):
983 return '[%s] %s - %s' % (item.episode.emission.title, item.title, item.episode.title)
984 return '[%s] %s' % (item.episode.emission.title, item.episode.title)
986 def item_link(self, item):
988 return item.episode.get_absolute_url() + '#%s' % item.id
989 return item.episode.get_absolute_url()
991 def item_enclosure_url(self, item):
992 current_site = Site.objects.get(id=settings.SITE_ID)
993 return add_domain(current_site.domain, item.get_format_url('mp3'), self.request.is_secure())
995 def item_enclosure_length(self, item):
996 sound_path = item.get_format_path('mp3')
998 return os.stat(sound_path)[stat.ST_SIZE]
1002 def item_enclosure_mime_type(self, item):
1005 def item_pubdate(self, item):
1006 return item.creation_timestamp
1008 def item_extra_kwargs(self, item):
1009 return {'tags': [x.name for x in item.episode.tags.all()], 'soundfile': item, 'episode': item.episode}
1012 podcasts_feed = PodcastsFeed()
1015 class RssNewsFeed(Feed):
1016 title = settings.RADIO_NAME
1018 description_template = 'feed/newsitem.html'
1021 return NewsItem.objects.order_by('-date')[:20]
1024 rss_news_feed = RssNewsFeed()
1027 class Atom1FeedWithBaseXml(Atom1Feed):
1028 def root_attributes(self):
1029 root_attributes = super().root_attributes()
1030 scheme, netloc, path, params, query, fragment = urllib.parse.urlparse(self.feed['feed_url'])
1031 root_attributes['xml:base'] = urllib.parse.urlunparse((scheme, netloc, '/', params, query, fragment))
1032 return root_attributes
1035 class AtomNewsFeed(RssNewsFeed):
1036 feed_type = Atom1FeedWithBaseXml
1039 atom_news_feed = AtomNewsFeed()
1042 class EmissionPodcastsFeed(PodcastsFeed):
1043 description_template = 'feed/soundfile.html'
1044 feed_type = RssCustomPodcastsFeed
1046 def __call__(self, request, *args, **kwargs):
1047 self.emission = Emission.objects.get(slug=kwargs.get('slug'))
1048 return super().__call__(request, *args, **kwargs)
1050 def item_title(self, item):
1052 return '%s - %s' % (item.title, item.episode.title)
1053 return item.episode.title
1057 return self.emission.title
1060 def description(self):
1061 return self.emission.subtitle
1065 return reverse('emission-view', kwargs={'slug': self.emission.slug})
1067 def feed_extra_kwargs(self, obj):
1068 return {'emission': self.emission}
1072 SoundFile.objects.select_related()
1073 .filter(podcastable=True, episode__emission__slug=self.emission.slug)
1074 .order_by('-creation_timestamp')[:50]
1078 emission_podcasts_feed = EmissionPodcastsFeed()
1081 class Party(TemplateView):
1082 template_name = 'party.html'
1084 def get_context_data(self, **kwargs):
1085 context = super().get_context_data(**kwargs)
1086 t = random.choice(['newsitem'] * 2 + ['emission'] * 3 + ['soundfile'] * 1 + ['episode'] * 2)
1090 NewsItem.objects.exclude(image__isnull=True).exclude(image__exact='').order_by('?')[0]
1092 elif t == 'emission':
1094 Emission.objects.exclude(image__isnull=True).exclude(image__exact='').order_by('?')[0]
1096 elif t == 'episode':
1098 Episode.objects.exclude(image__isnull=True).exclude(image__exact='').order_by('?')[0]
1100 elif t == 'soundfile':
1102 SoundFile.objects.exclude(episode__image__isnull=True)
1103 .exclude(episode__image__exact='')
1107 context['focus'] = focus
1112 party = Party.as_view()
1115 class Chat(DetailView, EmissionMixin):
1117 template_name = 'chat.html'
1120 chat = cache_control(max_age=15)(Chat.as_view())
1123 def media_hosting(request, location, *args, **kwargs):
1124 local_path = default_storage.path(location)
1125 response = HttpResponse()
1126 if os.path.exists(local_path):
1127 response['X-Accel-Redirect'] = settings.OFFSITE_MEDIA_SOUNDS[0] + location
1129 response['X-Accel-Redirect'] = settings.OFFSITE_MEDIA_SOUNDS[1] + location