1 from datetime import datetime, timedelta, date
8 from django.core.urlresolvers import reverse
9 from django.conf import settings
10 from django.http import Http404, JsonResponse
11 from django.utils.encoding import force_text
12 from django.utils.encoding import python_2_unicode_compatible
13 from django.utils.six.moves.urllib import parse as urlparse
14 from django.views.decorators.cache import cache_control
15 from django.views.generic.base import TemplateView
16 from django.views.generic.detail import DetailView
17 from django.views.decorators.csrf import csrf_exempt
18 from django.views.generic.dates import _date_from_string
19 from django.views.generic.dates import MonthArchiveView
21 from django.core.paginator import Paginator
23 from django.contrib.sites.models import Site
24 from django.contrib.syndication.views import Feed, add_domain
25 from django.utils.feedgenerator import Atom1Feed, Rss201rev2Feed
27 from haystack.query import SearchQuerySet
29 from emissions.models import Category, Emission, Episode, Diffusion, SoundFile, \
30 Schedule, Nonstop, NewsItem, NewsCategory, Focus
32 from emissions.utils import whatsonair, period_program
34 from newsletter.forms import SubscribeForm
35 from nonstop.utils import get_current_nonstop_track
36 from nonstop.models import SomaLogLine
38 from panikombo.models import ItemTopik
44 def get_emission_context(self, emission, episode_ids=None):
47 # get all episodes, with an additional attribute to get the date of
48 # their first diffusion
49 episodes_queryset = Episode.objects.select_related()
50 if episode_ids is not None:
51 episodes_queryset = episodes_queryset.filter(id__in=episode_ids)
53 episodes_queryset = episodes_queryset.filter(emission=emission)
55 context['episodes'] = \
56 episodes_queryset.extra(select={
57 'first_diffusion': 'emissions_diffusion.datetime',
59 select_params=(False, True),
60 where=['''datetime = (SELECT MIN(datetime)
61 FROM emissions_diffusion
62 WHERE episode_id = emissions_episode.id
63 AND datetime <= CURRENT_TIMESTAMP)'''],
64 tables=['emissions_diffusion'],
65 ).order_by('-first_diffusion').distinct()
67 context['futurEpisodes'] = \
68 episodes_queryset.extra(select={
69 'first_diffusion': 'emissions_diffusion.datetime',
71 select_params=(False, True),
72 where=['''datetime = (SELECT MIN(datetime)
73 FROM emissions_diffusion
74 WHERE episode_id = emissions_episode.id
75 AND datetime > CURRENT_TIMESTAMP)'''],
76 tables=['emissions_diffusion'],
77 ).order_by('first_diffusion').distinct()
80 # get all related soundfiles in a single query
82 if episode_ids is not None:
83 for episode_id in episode_ids:
84 soundfiles[episode_id] = None
86 for episode in Episode.objects.filter(emission=emission):
87 soundfiles[episode.id] = None
89 for soundfile in SoundFile.objects.select_related().filter(podcastable=True,
90 fragment=False, episode__emission=emission):
91 soundfiles[soundfile.episode_id] = soundfile
93 Episode.set_prefetched_soundfiles(soundfiles)
95 #context['futurEpisodes'] = context['episodes'].filter(first_diffusion='2013')[0:3]
100 class EmissionDetailView(DetailView, EmissionMixin):
103 def get_context_data(self, **kwargs):
104 context = super(EmissionDetailView, self).get_context_data(**kwargs)
105 context['schedules'] = Schedule.objects.select_related().filter(
106 emission=self.object).order_by('rerun', 'datetime')
107 context['news'] = NewsItem.objects.all(
108 ).filter(emission=self.object.id
109 ).exclude(expiration_date__lt=date.today() # expiration date
110 ).exclude(date__lt=date.today() - timedelta(days=60)
111 ).order_by('-date')[:3]
113 nonstop_object = Nonstop.objects.get(slug=self.object.slug)
114 except Nonstop.DoesNotExist:
118 dates = [today - timedelta(days=x) for x in range(7)]
119 if datetime.now().time() < nonstop_object.start:
121 context['nonstop'] = nonstop_object
122 context['nonstop_dates'] = dates
123 context.update(self.get_emission_context(self.object))
125 emission = EmissionDetailView.as_view()
127 class EpisodeDetailView(DetailView, EmissionMixin):
130 def get_context_data(self, **kwargs):
131 context = super(EpisodeDetailView, self).get_context_data(**kwargs)
132 context['diffusions'] = Diffusion.objects.select_related().filter(
133 episode=self.object.id).order_by('datetime')
135 context['emission'] = context['episode'].emission
136 except Emission.DoesNotExist:
138 if self.kwargs.get('emission_slug') != context['emission'].slug:
140 context.update(self.get_emission_context(context['emission']))
141 context['topiks'] = [x.topik for x in ItemTopik.objects.filter(episode=self.object)]
143 episode = EpisodeDetailView.as_view()
146 class NonstopPlaylistView(TemplateView):
147 template_name = 'nonstop_playlist.html'
149 def get_context_data(self, **kwargs):
150 context = super(NonstopPlaylistView, self).get_context_data(**kwargs)
152 context['emission'] = Emission.objects.get(slug=kwargs.get('slug'))
153 except Emission.DoesNotExist:
155 context['date'] = date(int(kwargs.get('year')),
156 int(kwargs.get('month')), int(kwargs.get('day')))
157 context['future'] = (context['date'] >= date.today())
159 nonstop_object = Nonstop.objects.get(slug=kwargs.get('slug'))
161 int(kwargs.get('year')), int(kwargs.get('month')), int(kwargs.get('day')),
162 nonstop_object.start.hour, nonstop_object.start.minute)
164 int(kwargs.get('year')), int(kwargs.get('month')), int(kwargs.get('day')),
165 nonstop_object.end.hour, nonstop_object.end.minute)
167 end = end + timedelta(days=1)
168 context['tracks'] = SomaLogLine.objects.filter(
169 play_timestamp__gte=start,
170 play_timestamp__lte=end,
171 on_air=True).select_related()
174 nonstop_playlist = NonstopPlaylistView.as_view()
176 class EmissionEpisodesDetailView(DetailView, EmissionMixin):
178 template_name = 'emissions/episodes.html'
180 def get_context_data(self, **kwargs):
181 context = super(EmissionEpisodesDetailView, self).get_context_data(**kwargs)
182 context['schedules'] = Schedule.objects.select_related().filter(
183 emission=self.object).order_by('rerun', 'datetime')
185 context['search_query'] = self.request.GET.get('q')
186 if context['search_query']:
188 sqs = SearchQuerySet().models(Episode).filter(
189 emission_slug_exact=self.object.slug, text=context['search_query'])
190 episode_ids = [x.pk for x in sqs]
194 context.update(self.get_emission_context(self.object, episode_ids=episode_ids))
196 emissionEpisodes = EmissionEpisodesDetailView.as_view()
199 class SoundFileEmbedView(DetailView):
201 template_name = 'soundfiles/embed.html'
203 def get_context_data(self, **kwargs):
204 context = super(SoundFileEmbedView, self).get_context_data(**kwargs)
205 if self.kwargs.get('episode_slug') != self.object.episode.slug:
207 if self.kwargs.get('emission_slug') != self.object.episode.emission.slug:
209 context['episode'] = self.object.episode
211 soundfile_embed = SoundFileEmbedView.as_view()
214 class SoundFileDialogEmbedView(DetailView):
216 template_name = 'soundfiles/dialog-embed.html'
218 def get_context_data(self, **kwargs):
219 context = super(SoundFileDialogEmbedView, self).get_context_data(**kwargs)
220 if self.kwargs.get('episode_slug') != self.object.episode.slug:
222 if self.kwargs.get('emission_slug') != self.object.episode.emission.slug:
224 context['episode'] = self.object.episode
226 soundfile_dlg_embed = SoundFileDialogEmbedView.as_view()
230 class ProgramView(TemplateView):
231 template_name = 'program.html'
233 def get_context_data(self, year=None, week=None, **kwargs):
234 context = super(ProgramView, self).get_context_data(**kwargs)
236 context['weekday'] = datetime.today().weekday()
238 context['week'] = week = int(week) if week is not None else datetime.today().isocalendar()[1]
239 context['year'] = year = int(year) if year is not None else datetime.today().isocalendar()[0]
240 context['week_first_day'] = utils.tofirstdayinisoweek(year, week)
241 context['week_last_day'] = context['week_first_day'] + timedelta(days=6)
245 program = ProgramView.as_view()
247 @python_2_unicode_compatible
254 def __init__(self, i, j):
259 def add_schedule(self, schedule):
260 end_time = schedule.datetime + timedelta(
261 minutes=schedule.get_duration())
262 self.time_label = '%02d:%02d-%02d:%02d' % (
263 schedule.datetime.hour,
264 schedule.datetime.minute,
267 self.schedules.append(schedule)
269 def sorted_schedules(self):
270 return sorted(self.schedules, key=lambda x: x.week_sort_key())
274 return ', '.join([x.emission.title for x in self.schedules])
278 def __eq__(self, other):
279 return (force_text(self) == force_text(other) and self.time_label == other.time_label)
282 class Grid(TemplateView):
283 template_name = 'grid.html'
285 def get_context_data(self, **kwargs):
286 context = super(Grid, self).get_context_data(**kwargs)
288 nb_lines = 2 * 24 # the cells are half hours
291 times = ['%02d:%02d' % (x/2, x%2*30) for x in range(nb_lines)]
292 # start grid after the night programs
293 times = times[2*Schedule.DAY_HOUR_START:] + times[:2*Schedule.DAY_HOUR_START]
296 for nonstop in Nonstop.objects.all():
297 if nonstop.start == nonstop.end:
299 if nonstop.start < nonstop.end:
300 nonstops.append([nonstop.start.hour + nonstop.start.minute/60.,
301 nonstop.end.hour + nonstop.end.minute/60.,
302 nonstop.title, nonstop.slug, nonstop])
305 nonstops.append([nonstop.start.hour + nonstop.start.minute/60.,
307 nonstop.title, nonstop.slug, nonstop])
309 nonstop.end.hour + nonstop.end.minute/60.,
310 nonstop.title, nonstop.slug, nonstop])
313 for i in range(nb_lines):
316 grid[-1].append(TimeCell(i, j))
318 nonstop = [x for x in nonstops if i>=x[0]*2 and i<x[1]*2][0]
319 for time_cell in grid[-1]:
320 time_cell.nonstop = nonstop[2]
321 time_cell.nonstop_slug = nonstop[3]
322 time_cell.redirect_path = nonstop[4].redirect_path
324 # the one ending at 5am will be cut down, so we inscribe
325 # its duration manually
326 time_cell.time_label = '%02d:00-%02d:00' % (
327 nonstop[0], nonstop[1])
329 for schedule in Schedule.objects.prefetch_related(
330 'emission__categories').select_related().order_by('datetime'):
331 row_start = schedule.datetime.hour * 2 + \
332 int(math.ceil(schedule.datetime.minute / 30))
333 day_no = schedule.get_weekday()
335 for step in range(int(math.ceil(schedule.get_duration() / 30.))):
336 if grid[(row_start+step)%nb_lines][day_no] is None:
337 grid[(row_start+step)%nb_lines][day_no] = TimeCell()
338 grid[(row_start+step)%nb_lines][day_no].add_schedule(schedule)
340 # start grid after the night programs
341 grid = grid[2*Schedule.DAY_HOUR_START:] + grid[:2*Schedule.DAY_HOUR_START]
343 # look for the case where the same emission has different schedules for
344 # the same time cell, for example if it lasts one hour the first week,
345 # and two hours the third week.
346 for i in range(nb_lines):
347 grid[i] = [x for x in grid[i] if x is not None]
348 for j, cell in enumerate(grid[i]):
349 if grid[i][j] is None:
351 if len(grid[i][j].schedules) > 1:
352 time_cell_emissions = {}
353 for schedule in grid[i][j].schedules:
354 if not schedule.emission.id in time_cell_emissions:
355 time_cell_emissions[schedule.emission.id] = []
356 time_cell_emissions[schedule.emission.id].append(schedule)
357 for schedule_list in time_cell_emissions.values():
358 if len(schedule_list) == 1:
360 # here it is, same cell, same emission, several
362 schedule_list.sort(key=lambda x: x.get_duration())
364 schedule = schedule_list[0]
365 end_time = schedule.datetime + timedelta(
366 minutes=schedule.get_duration())
367 grid[i][j].time_label = '%02d:%02d-%02d:%02d' % (
368 schedule.datetime.hour,
369 schedule.datetime.minute,
373 for schedule in schedule_list[1:]:
374 grid[i][j].schedules.remove(schedule)
375 end_time = schedule.datetime + timedelta(minutes=schedule.get_duration())
376 schedule_list[0].time_label_extra = ', -%02d:%02d %s' % (
377 end_time.hour, end_time.minute, schedule.weeks_string)
381 # 1st thing is to merge cells on the same line, this will mostly catch
382 # consecutive nonstop cells
383 for i in range(nb_lines):
384 for j, cell in enumerate(grid[i]):
385 if grid[i][j] is None:
389 # if the cells are identical, they are removed from the
390 # grid, and current cell width is increased
391 while grid[i][j+t] == cell:
398 # once we're done we remove empty cells
399 grid[i] = [x for x in grid[i] if x is not None]
401 # 2nd thing is to merge cells vertically, this is emissions that last
402 # for more than 30 minutes
403 for i in range(nb_lines):
404 grid[i] = [x for x in grid[i] if x is not None]
405 for j, cell in enumerate(grid[i]):
406 if grid[i][j] is None:
411 # we look if the next time cell has the same emissions
412 same_cell_below = [(bj, x) for bj, x in enumerate(grid[i+cell.h])
413 if x == cell and x.y == cell.y and x.w == cell.w]
415 # if the cell was identical, we remove it and
416 # increase current cell height
417 bj, same_cell_below = same_cell_below[0]
418 del grid[i+cell.h][bj]
421 # if the cell is different, we have a closer look
422 # to it, so we can remove emissions that will
423 # already be mentioned in the current cell.
426 # - 7am30, seuls contre tout, 1h30
427 # - 8am, du pied gauche & la voix de la rue, 1h
428 # should produce: (this is case A)
430 # | seuls contre tout |
431 # |---------------------|
434 # | la voix de la rue |
436 # On the other hand, if all three emissions started
437 # at 7am30, we want: (this is case B)
439 # | seuls contre tout |
441 # | la voix de la rue |
442 # that is we merge all of them, ignoring the fact
443 # that the other emissions will stop at 8am30
444 current_cell_schedules = set(grid[i][j].schedules)
445 current_cell_emissions = set([x.emission for x in current_cell_schedules])
447 while True and current_cell_schedules:
448 same_cell_below = [x for x in grid[i+cursor] if x.y == grid[i][j].y]
449 if not same_cell_below:
452 same_cell_below = same_cell_below[0]
453 same_cell_below_emissions = set([x.emission for x in same_cell_below.schedules])
455 if current_cell_emissions.issubset(same_cell_below_emissions):
456 # this handles case A (see comment above)
457 for schedule in current_cell_schedules:
458 if schedule in same_cell_below.schedules:
459 same_cell_below.schedules.remove(schedule)
460 elif same_cell_below_emissions and \
461 current_cell_emissions.issuperset(same_cell_below_emissions):
462 # this handles case B (see comment above)
463 # we set the cell time label to the longest
465 grid[i][j].time_label = same_cell_below.time_label
466 # then we sort emissions so the longest are
468 grid[i][j].schedules.sort(key=lambda x: -x.get_duration())
469 # then we add individual time labels to the
471 for schedule in current_cell_schedules:
472 if schedule not in same_cell_below.schedules:
473 end_time = schedule.datetime + timedelta(
474 minutes=schedule.get_duration())
475 schedule.time_label = '%02d:%02d-%02d:%02d' % (
476 schedule.datetime.hour,
477 schedule.datetime.minute,
481 grid[i+cursor].remove(same_cell_below)
482 elif same_cell_below_emissions and \
483 current_cell_emissions.intersection(same_cell_below_emissions):
484 same_cell_below.schedules = [x for x in
485 same_cell_below.schedules if
487 current_cell_emissions or
488 x.get_duration() < 30]
498 context['grid'] = grid
499 context['times'] = times
500 context['categories'] = Category.objects.all()
501 context['weekdays'] = ['Lundi', 'Mardi', 'Mercredi', 'Jeudi',
502 'Vendredi', 'Samedi', 'Dimanche']
506 grid = Grid.as_view()
509 class Home(TemplateView):
510 template_name = 'home.html'
512 def get_context_data(self, **kwargs):
513 context = super(Home, self).get_context_data(**kwargs)
514 context['emissions'] = Emission.objects.filter(archived=False).order_by('-creation_timestamp')[:3]
515 context['newsitems'] = NewsItem.objects.exclude(date__gt=date.today()).order_by('-date')[:3]
517 context['soundfiles'] = SoundFile.objects.prefetch_related('episode__emission__categories').filter(
518 podcastable=True, fragment=False) \
519 .select_related().extra(select={
520 'first_diffusion': 'emissions_diffusion.datetime', },
521 select_params=(False, True),
522 where=['''datetime = (SELECT MIN(datetime)
523 FROM emissions_diffusion
524 WHERE episode_id = emissions_episode.id)'''],
525 tables=['emissions_diffusion'],).order_by('-creation_timestamp').distinct() [:3]
527 context['newsletter_form'] = SubscribeForm()
531 home = Home.as_view()
533 class NewsItemView(DetailView):
535 def get_context_data(self, **kwargs):
536 context = super(NewsItemView, self).get_context_data(**kwargs)
537 context['categories'] = NewsCategory.objects.all()
538 context['news'] = NewsItem.objects.all().order_by('-date')
539 context['topiks'] = [x.topik for x in ItemTopik.objects.filter(newsitem=self.object)]
541 newsitemview = NewsItemView.as_view()
543 class News(TemplateView):
544 template_name = 'news.html'
545 def get_context_data(self, **kwargs):
546 context = super(News, self).get_context_data(**kwargs)
547 context['focus'] = NewsItem.objects.exclude(date__gt=date.today() # publication date
548 ).exclude(expiration_date__lt=date.today() # expiration date
549 ).filter(got_focus__isnull=False
550 ).select_related('category').order_by('-date')[:10]
551 context['news'] = NewsItem.objects.exclude(date__gt=date.today()).order_by('-date')
554 news = News.as_view()
557 class Agenda(TemplateView):
558 template_name = 'agenda.html'
559 def get_context_data(self, **kwargs):
560 context = super(Agenda, self).get_context_data(**kwargs)
561 context['agenda'] = NewsItem.objects.exclude(date__gt=date.today()).filter(
562 event_date__gte=date.today()).order_by('date')[:20]
563 context['news'] = NewsItem.objects.exclude(date__gt=date.today()).order_by('-date')
564 context['previous_month'] = datetime.today().replace(day=1) - timedelta(days=2)
567 agenda = Agenda.as_view()
570 class AgendaByMonth(MonthArchiveView):
571 template_name = 'agenda.html'
572 queryset = NewsItem.objects.filter(event_date__isnull=False)
574 date_field = 'event_date'
577 def get_context_data(self, **kwargs):
578 context = super(AgendaByMonth, self).get_context_data(**kwargs)
579 context['agenda'] = context['object_list']
580 context['news'] = NewsItem.objects.all().order_by('-date')
583 agenda_by_month = AgendaByMonth.as_view()
586 class Emissions(TemplateView):
587 template_name = 'emissions.html'
589 def get_context_data(self, **kwargs):
590 context = super(Emissions, self).get_context_data(**kwargs)
591 context['emissions'] = Emission.objects.prefetch_related('categories').filter(archived=False).order_by('title')
592 context['categories'] = Category.objects.all()
595 emissions = Emissions.as_view()
597 class EmissionsArchives(TemplateView):
598 template_name = 'emissions/archives.html'
600 def get_context_data(self, **kwargs):
601 context = super(EmissionsArchives, self).get_context_data(**kwargs)
602 context['emissions'] = Emission.objects.prefetch_related('categories').filter(archived=True).order_by('title')
603 context['categories'] = Category.objects.all()
605 emissionsArchives = EmissionsArchives.as_view()
608 class Listen(TemplateView):
609 template_name = 'listen.html'
611 def get_context_data(self, **kwargs):
612 context = super(Listen, self).get_context_data(**kwargs)
613 context['focus'] = SoundFile.objects.prefetch_related('episode__emission__categories').filter(
614 podcastable=True, got_focus__isnull=False) \
615 .select_related().extra(select={
616 'first_diffusion': 'emissions_diffusion.datetime', },
617 select_params=(False, True),
618 where=['''datetime = (SELECT MIN(datetime)
619 FROM emissions_diffusion
620 WHERE episode_id = emissions_episode.id)'''],
621 tables=['emissions_diffusion'],).order_by('-first_diffusion').distinct() [:10]
622 context['soundfiles'] = SoundFile.objects.prefetch_related('episode__emission__categories').filter(
624 .select_related().extra(select={
625 'first_diffusion': 'emissions_diffusion.datetime', },
626 select_params=(False, True),
627 where=['''datetime = (SELECT MIN(datetime)
628 FROM emissions_diffusion
629 WHERE episode_id = emissions_episode.id)'''],
630 tables=['emissions_diffusion'],).order_by('-creation_timestamp').distinct()
635 listen = Listen.as_view()
637 @cache_control(max_age=15)
643 'title': d['episode'].title,
644 'url': d['episode'].get_absolute_url()
646 if d.get('emission'):
648 if d['emission'].chat_open:
649 chat_url = reverse('emission-chat', kwargs={'slug': d['emission'].slug})
651 'title': d['emission'].title,
652 'url': d['emission'].get_absolute_url(),
656 redirect_path = d['nonstop'].redirect_path
658 'title': d['nonstop'].title,
661 d['nonstop']['url'] = redirect_path
662 d.update(get_current_nonstop_track())
663 if d.get('current_slot'):
664 del d['current_slot']
665 return JsonResponse({'data': d})
668 class NewsItemDetailView(DetailView):
671 newsitem = NewsItemDetailView.as_view()
673 class RssCustomPodcastsFeed(Rss201rev2Feed):
674 def add_root_elements(self, handler):
675 super(RssCustomPodcastsFeed, self).add_root_elements(handler)
676 emission = self.feed.get('emission')
677 if emission and emission.image and emission.image.url:
678 image_url = emission.image.url
680 image_url = '/static/img/logo-panik-500.png'
681 image_url = urlparse.urljoin(self.feed['link'], image_url)
682 handler.startElement('image', {})
684 handler.addQuickElement('title', emission.title)
686 handler.addQuickElement('title', settings.RADIO_NAME)
687 handler.addQuickElement('url', image_url)
688 handler.endElement('image')
689 handler.addQuickElement('itunes:explicit', 'no') # invidividual items will get their own value
690 handler.addQuickElement('itunes:image', None, {'href': image_url})
692 if emission.subtitle:
693 handler.addQuickElement('itunes:subtitle', emission.subtitle)
694 for category in emission.categories.all():
695 if category.itunes_category:
696 handler.addQuickElement('itunes:category', None, {'text': category.itunes_category})
698 handler.addQuickElement('itunes:author', emission.title)
699 handler.startElement('itunes:owner', {})
701 handler.addQuickElement('itunes:email', emission.email)
702 handler.addQuickElement('itunes:name', emission.title)
703 handler.endElement('itunes:owner')
705 handler.addQuickElement('itunes:author', settings.RADIO_NAME)
706 handler.startElement('itunes:owner', {})
707 handler.addQuickElement('itunes:email', 'info@radiopanik.org')
708 handler.addQuickElement('itunes:name', settings.RADIO_NAME)
709 handler.endElement('itunes:owner')
711 def root_attributes(self):
712 attrs = super(RssCustomPodcastsFeed, self).root_attributes()
713 attrs['xmlns:dc'] = 'http://purl.org/dc/elements/1.1/'
714 attrs['xmlns:itunes'] = 'http://www.itunes.com/dtds/podcast-1.0.dtd'
717 def add_item_elements(self, handler, item):
718 super(RssCustomPodcastsFeed, self).add_item_elements(handler, item)
720 for tag in item.get('tags') or []:
721 handler.addQuickElement('dc:subject', tag)
722 if tag == 'explicit':
725 handler.addQuickElement('itunes:keywords', ','.join(item.get('tags')))
726 handler.addQuickElement('itunes:explicit', explicit)
727 episode = item.get('episode')
728 if episode and episode.image and episode.image.url:
729 image_url = urlparse.urljoin(self.feed['link'], episode.image.url)
730 handler.addQuickElement('itunes:image', None, {'href': image_url})
731 soundfile = item.get('soundfile')
732 if soundfile.duration:
733 handler.addQuickElement('itunes:duration', '%02d:%02d:%02d' % (
734 soundfile.duration / 3600,
735 soundfile.duration % 3600 / 60,
736 soundfile.duration % 60))
739 class PodcastsFeed(Feed):
740 title = '%s - Podcasts' % settings.RADIO_NAME
742 description_template = 'feed/soundfile.html'
743 feed_type = RssCustomPodcastsFeed
746 return SoundFile.objects.select_related().filter(
747 podcastable=True).order_by('-creation_timestamp')[:50]
749 def item_title(self, item):
751 return '[%s] %s - %s' % (item.episode.emission.title, item.title, item.episode.title)
752 return '[%s] %s' % (item.episode.emission.title, item.episode.title)
754 def item_link(self, item):
756 return item.episode.get_absolute_url() + '#%s' % item.id
757 return item.episode.get_absolute_url() + '#%s' % item.id
759 def item_enclosure_url(self, item):
760 current_site = Site.objects.get(id=settings.SITE_ID)
761 return add_domain(current_site.domain, item.get_format_url('mp3'))
763 def item_enclosure_length(self, item):
764 sound_path = item.get_format_path('mp3')
766 return os.stat(sound_path)[stat.ST_SIZE]
770 def item_enclosure_mime_type(self, item):
773 def item_pubdate(self, item):
774 return item.creation_timestamp
776 def item_extra_kwargs(self, item):
777 return {'tags': [x.name for x in item.episode.tags.all()],
779 'episode': item.episode}
781 podcasts_feed = PodcastsFeed()
784 class RssNewsFeed(Feed):
785 title = settings.RADIO_NAME
787 description_template = 'feed/newsitem.html'
790 return NewsItem.objects.order_by('-date')[:20]
792 rss_news_feed = RssNewsFeed()
794 class Atom1FeedWithBaseXml(Atom1Feed):
795 def root_attributes(self):
796 root_attributes = super(Atom1FeedWithBaseXml, self).root_attributes()
797 scheme, netloc, path, params, query, fragment = urlparse.urlparse(self.feed['feed_url'])
798 root_attributes['xml:base'] = urlparse.urlunparse((scheme, netloc, '/', params, query, fragment))
799 return root_attributes
801 class AtomNewsFeed(RssNewsFeed):
802 feed_type = Atom1FeedWithBaseXml
804 atom_news_feed = AtomNewsFeed()
807 class EmissionPodcastsFeed(PodcastsFeed):
808 description_template = 'feed/soundfile.html'
809 feed_type = RssCustomPodcastsFeed
811 def __call__(self, request, *args, **kwargs):
812 self.emission = Emission.objects.get(slug=kwargs.get('slug'))
813 return super(EmissionPodcastsFeed, self).__call__(request, *args, **kwargs)
815 def item_title(self, item):
817 return '%s - %s' % (item.title, item.episode.title)
818 return item.episode.title
822 return self.emission.title
825 def description(self):
826 return self.emission.subtitle
830 return reverse('emission-view', kwargs={'slug': self.emission.slug})
832 def feed_extra_kwargs(self, obj):
833 return {'emission': self.emission}
836 return SoundFile.objects.select_related().filter(
838 episode__emission__slug=self.emission.slug).order_by('-creation_timestamp')[:50]
840 emission_podcasts_feed = EmissionPodcastsFeed()
843 class Party(TemplateView):
844 template_name = 'party.html'
846 def get_context_data(self, **kwargs):
847 context = super(Party, self).get_context_data(**kwargs)
848 t = random.choice(['newsitem']*2 + ['emission']*3 + ['soundfile']*1 + ['episode']*2)
851 focus.newsitem = NewsItem.objects.exclude(
852 image__isnull=True).exclude(image__exact='').order_by('?')[0]
853 elif t == 'emission':
854 focus.emission = Emission.objects.exclude(
855 image__isnull=True).exclude(image__exact='').order_by('?')[0]
857 focus.episode = Episode.objects.exclude(
858 image__isnull=True).exclude(image__exact='').order_by('?')[0]
859 elif t == 'soundfile':
860 focus.soundfile = SoundFile.objects.exclude(
861 episode__image__isnull=True).exclude(episode__image__exact='').order_by('?')[0]
863 context['focus'] = focus
867 party = Party.as_view()
871 class Chat(DetailView, EmissionMixin):
873 template_name = 'chat.html'
875 chat = cache_control(max_age=15)(Chat.as_view())