]> git.0d.be Git - panikweb.git/blobdiff - panikweb/views.py
feeds: include fragment id in guid
[panikweb.git] / panikweb / views.py
index f0490cf9ccd36cb1c3d19fa7949d5dcf0dc96b16..6e5209bbf0707175b5e8d167b126f9d4423fe3e0 100644 (file)
@@ -4,11 +4,13 @@ import random
 import os
 import stat
 import time
-import urlparse
 
 from django.core.urlresolvers import reverse
 from django.conf import settings
 from django.http import Http404, JsonResponse
+from django.utils.encoding import force_text
+from django.utils.encoding import python_2_unicode_compatible
+from django.utils.six.moves.urllib import parse as urlparse
 from django.views.decorators.cache import cache_control
 from django.views.generic.base import TemplateView
 from django.views.generic.detail import DetailView
@@ -102,7 +104,11 @@ class EmissionDetailView(DetailView, EmissionMixin):
         context = super(EmissionDetailView, self).get_context_data(**kwargs)
         context['schedules'] = Schedule.objects.select_related().filter(
                 emission=self.object).order_by('rerun', 'datetime')
-        context['news'] = NewsItem.objects.all().filter(emission=self.object.id).order_by('-date')[:3]
+        context['news'] = NewsItem.objects.all(
+                ).filter(emission=self.object.id
+                ).exclude(expiration_date__lt=date.today()  # expiration date
+                ).exclude(date__lt=date.today() - timedelta(days=60)
+                ).order_by('-date')[:3]
         try:
             nonstop_object = Nonstop.objects.get(slug=self.object.slug)
         except Nonstop.DoesNotExist:
@@ -238,6 +244,7 @@ class ProgramView(TemplateView):
 
 program = ProgramView.as_view()
 
+@python_2_unicode_compatible
 class TimeCell:
     nonstop = None
     w = 1
@@ -259,14 +266,17 @@ class TimeCell:
                 end_time.minute)
         self.schedules.append(schedule)
 
-    def __unicode__(self):
+    def sorted_schedules(self):
+        return sorted(self.schedules, key=lambda x: x.week_sort_key())
+
+    def __str__(self):
         if self.schedules:
             return ', '.join([x.emission.title for x in self.schedules])
         else:
             return self.nonstop
 
     def __eq__(self, other):
-        return (unicode(self) == unicode(other) and self.time_label == other.time_label)
+        return (force_text(self) == force_text(other) and self.time_label == other.time_label)
 
 
 class Grid(TemplateView):
@@ -349,7 +359,7 @@ class Grid(TemplateView):
                             continue
                         # here it is, same cell, same emission, several
                         # schedules
-                        schedule_list.sort(lambda x,y: cmp(x.get_duration(), y.get_duration()))
+                        schedule_list.sort(key=lambda x: x.get_duration())
 
                         schedule = schedule_list[0]
                         end_time = schedule.datetime + timedelta(
@@ -455,8 +465,7 @@ class Grid(TemplateView):
                                     grid[i][j].time_label = same_cell_below.time_label
                                     # then we sort emissions so the longest are
                                     # put first
-                                    grid[i][j].schedules.sort(
-                                            lambda x, y: -cmp(x.get_duration(), y.get_duration()))
+                                    grid[i][j].schedules.sort(key=lambda x: -x.get_duration())
                                     # then we add individual time labels to the
                                     # other schedules
                                     for schedule in current_cell_schedules:
@@ -674,17 +683,31 @@ class RssCustomPodcastsFeed(Rss201rev2Feed):
         if emission:
             handler.addQuickElement('title', emission.title)
         else:
-            handler.addQuickElement('title', 'Radio Panik')
+            handler.addQuickElement('title', settings.RADIO_NAME)
         handler.addQuickElement('url', image_url)
         handler.endElement('image')
+        handler.addQuickElement('itunes:explicit', 'no')  # invidividual items will get their own value
         handler.addQuickElement('itunes:image', None, {'href': image_url})
-        if emission and emission.subtitle:
-            handler.addQuickElement('itunes:subtitle', emission.subtitle)
         if emission:
+            if emission.subtitle:
+                handler.addQuickElement('itunes:subtitle', emission.subtitle)
             for category in emission.categories.all():
                 if category.itunes_category:
                     handler.addQuickElement('itunes:category', None, {'text': category.itunes_category})
 
+            handler.addQuickElement('itunes:author', emission.title)
+            handler.startElement('itunes:owner', {})
+            if emission.email:
+                handler.addQuickElement('itunes:email', emission.email)
+            handler.addQuickElement('itunes:name', emission.title)
+            handler.endElement('itunes:owner')
+        else:
+            handler.addQuickElement('itunes:author', settings.RADIO_NAME)
+            handler.startElement('itunes:owner', {})
+            handler.addQuickElement('itunes:email', 'info@radiopanik.org')
+            handler.addQuickElement('itunes:name', settings.RADIO_NAME)
+            handler.endElement('itunes:owner')
+
     def root_attributes(self):
         attrs = super(RssCustomPodcastsFeed, self).root_attributes()
         attrs['xmlns:dc'] = 'http://purl.org/dc/elements/1.1/'
@@ -714,7 +737,7 @@ class RssCustomPodcastsFeed(Rss201rev2Feed):
 
 
 class PodcastsFeed(Feed):
-    title = 'Radio Panik - Podcasts'
+    title = '%s - Podcasts' % settings.RADIO_NAME
     link = '/'
     description_template = 'feed/soundfile.html'
     feed_type = RssCustomPodcastsFeed
@@ -729,7 +752,9 @@ class PodcastsFeed(Feed):
         return '[%s] %s' % (item.episode.emission.title, item.episode.title)
 
     def item_link(self, item):
-        return item.episode.get_absolute_url()
+        if item.fragment:
+            return item.episode.get_absolute_url() + '#%s' % item.id
+        return item.episode.get_absolute_url() + '#%s' % item.id
 
     def item_enclosure_url(self, item):
         current_site = Site.objects.get(id=settings.SITE_ID)
@@ -757,7 +782,7 @@ podcasts_feed = PodcastsFeed()
 
 
 class RssNewsFeed(Feed):
-    title = 'Radio Panik'
+    title = settings.RADIO_NAME
     link = '/news/'
     description_template = 'feed/newsitem.html'