changeset 403:6e425c9b9d16

In support of #161; improve query performance of the feed view; also cache the feed view in urls.py.
author Brian Neal <bgneal@gmail.com>
date Sun, 27 Mar 2011 18:22:48 +0000 (2011-03-27)
parents 9175392da056
children 41411066b16d
files gpp/podcast/urls.py gpp/podcast/views.py gpp/templates/podcast/base.html gpp/templates/podcast/feed.xml
diffstat 4 files changed, 81 insertions(+), 67 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/podcast/urls.py	Sun Mar 27 01:51:18 2011 +0000
+++ b/gpp/podcast/urls.py	Sun Mar 27 18:22:48 2011 +0000
@@ -1,8 +1,15 @@
-"""urls for the podcast application"""
+"""
+urls for the podcast application
+
+"""
 from django.conf.urls.defaults import *
+from django.views.decorators.cache import cache_page
+
+from podcast.views import feed
+
 
 urlpatterns = patterns('podcast.views',
    url(r'^$', 'index', name='podcast-main'),
-   (r'^(\d+)/$', 'detail'),
-   (r'^feed.xml/$', 'feed'),
+   url(r'^(\d+)/$', 'detail', name='podcast-detail'),
+   url(r'^feed.xml/$', cache_page(feed, 3600), name='podcast-feed'),
 )
--- a/gpp/podcast/views.py	Sun Mar 27 01:51:18 2011 +0000
+++ b/gpp/podcast/views.py	Sun Mar 27 18:22:48 2011 +0000
@@ -15,86 +15,90 @@
 
 
 def get_ext_from_url(url):
-    """
-    This function returns the extension part of the path from the given
-    url.
-    """
-    ext = os.path.splitext(urlparse(url).path)[1]
-    if ext.startswith('.'):
-        ext = ext[1:]
-    return ext
+     """
+     This function returns the extension part of the path from the given
+     url.
+     """
+     ext = os.path.splitext(urlparse(url).path)[1]
+     if ext.startswith('.'):
+          ext = ext[1:]
+     return ext
 
 
 def jplayer_params(ext, url, alt_ext, alt_url):
-    """
-    Compute and returns a 2-tuple: (jplayer_media, jplayer_supplied)
-    where
-        jplayer_media: a string representation of the JSON for the
-            jplayer setMedia parameter
-        jplayer_supplied: the string for the jplayer supplied parameter
+     """
+     Compute and returns a 2-tuple: (jplayer_media, jplayer_supplied)
+     where
+          jplayer_media: a string representation of the JSON for the
+                jplayer setMedia parameter
+          jplayer_supplied: the string for the jplayer supplied parameter
 
-    media_list is an input list or tuple of 2-tuples of the form
-        (media_type, url)
-    where media_type is e.g. mp3, m4a, ogg, etc.
+     media_list is an input list or tuple of 2-tuples of the form
+          (media_type, url)
+     where media_type is e.g. mp3, m4a, ogg, etc.
 
-    """
-    media = dict([(ext, url)])
-    if alt_ext and alt_url:
-        media[alt_ext] = alt_url
+     """
+     media = dict([(ext, url)])
+     if alt_ext and alt_url:
+          media[alt_ext] = alt_url
 
-    # prefer mp4 to mp3
-    if alt_ext is None:
-        supplied = [ext]
-    elif ext == "m4a":
-        supplied = (ext, alt_ext)
-    else:
-        supplied = (alt_ext, ext)
+     # prefer mp4 to mp3
+     if alt_ext is None:
+          supplied = [ext]
+     elif ext == "m4a":
+          supplied = (ext, alt_ext)
+     else:
+          supplied = (alt_ext, ext)
 
-    supplied = ", ".join(supplied)
+     supplied = ", ".join(supplied)
 
-    return json.dumps(media), supplied
+     return json.dumps(media), supplied
 
 
 def index(request):
-   try:
-      channel = Channel.objects.get(pk=1)
-   except Channel.DoesNotExist:
-      channel = None
+    try:
+        channel = Channel.objects.get(pk=1)
+    except Channel.DoesNotExist:
+        channel = None
 
-   return render_to_response('podcast/index.html', {
-      'channel': channel,
-      },
-      context_instance = RequestContext(request))
+    return render_to_response('podcast/index.html', {
+        'channel': channel,
+        },
+        context_instance = RequestContext(request))
 
 
 def detail(request, id):
-   podcast = get_object_or_404(Item.objects.select_related(), pk = id)
+    podcast = get_object_or_404(Item.objects.select_related(), pk = id)
 
-   ext = get_ext_from_url(podcast.enclosure_url)
-   alt_ext = None
-   if podcast.alt_enclosure_url:
-      alt_ext = get_ext_from_url(podcast.alt_enclosure_url)
+    ext = get_ext_from_url(podcast.enclosure_url)
+    alt_ext = None
+    if podcast.alt_enclosure_url:
+        alt_ext = get_ext_from_url(podcast.alt_enclosure_url)
 
-   jplayer_media, jplayer_supplied = jplayer_params(ext, podcast.enclosure_url,
-           alt_ext, podcast.alt_enclosure_url)
+    jplayer_media, jplayer_supplied = jplayer_params(ext, podcast.enclosure_url,
+              alt_ext, podcast.alt_enclosure_url)
 
-   return render_to_response('podcast/detail.html', {
-      'channel': podcast.channel,
-      'podcast': podcast,
-      'ext': ext,
-      'alt_ext': alt_ext,
-      'jplayer_media': jplayer_media,
-      'jplayer_supplied': jplayer_supplied,
-      },
-      context_instance = RequestContext(request))
+    return render_to_response('podcast/detail.html', {
+        'channel': podcast.channel,
+        'podcast': podcast,
+        'ext': ext,
+        'alt_ext': alt_ext,
+        'jplayer_media': jplayer_media,
+        'jplayer_supplied': jplayer_supplied,
+        },
+        context_instance = RequestContext(request))
 
 
 def feed(request):
-   try:
-      channel = Channel.objects.get(pk=1)
-   except Channel.DoesNotExist:
-      channel = None
-   return render_to_response('podcast/feed.xml', {
-      'channel': channel,
-      },
-      context_instance = RequestContext(request))
+    try:
+        channel = Channel.objects.get(pk=1)
+    except Channel.DoesNotExist:
+        channel = None
+
+    if channel:
+        channel.items = Item.objects.filter(channel=channel)
+
+    return render_to_response('podcast/feed.xml', {
+        'channel': channel,
+        },
+        context_instance = RequestContext(request))
--- a/gpp/templates/podcast/base.html	Sun Mar 27 01:51:18 2011 +0000
+++ b/gpp/templates/podcast/base.html	Sun Mar 27 18:22:48 2011 +0000
@@ -1,5 +1,8 @@
 {% extends 'base.html' %}
 {% load url from future %}
+{% block custom_head %}
+<link rel="alternate" type="application/rss+xml" title="SurfGuitar101 Podcast" href="{% url 'podcast-feed' %}" />
+{% endblock %}
 {% block content %}
 <h2><a href="{% url 'podcast-main' %}">SurfGuitar101 Podcast</a></h2>
 <img src="{{ channel.image.url }}" alt="Podcast Logo" style="float: left; margin-right:10px;" />
@@ -15,7 +18,7 @@
 <a href="mailto:podcast@surfguitar101.com">podcast@surfguitar101.com</a>.
 </p>
 <p>
-Subscribe to the podcast via RSS: <a href="{% url 'podcast.views.feed' %}">Feed</a>
+<a href="{% url 'podcast-feed' %}"><img src="{{ STATIC_URL }}icons/feed.png" alt="RSS Feed" title="RSS Feed" /></a> <a href="{% url 'podcast-feed' %}">Subscribe to the podcast via RSS</a>.
 </p>
 <p>
 <strong>Hey iTunes users!</strong> Here is our <a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id=284928526">listing in iTunes</a>. Follow the previous link and then click the subscribe button to let iTunes automatically download episodes for you.
--- a/gpp/templates/podcast/feed.xml	Sun Mar 27 01:51:18 2011 +0000
+++ b/gpp/templates/podcast/feed.xml	Sun Mar 27 18:22:48 2011 +0000
@@ -21,7 +21,7 @@
 <itunes:image href="{{ channel.image.url }}" />
 <itunes:category text="{{ channel.category }}" />
 <itunes:explicit>{{ channel.explicit }}</itunes:explicit>
-{% for item in channel.item_set.all %}
+{% for item in channel.items %}
 <item>
 <title>{{ item.title }}</title>
 <itunes:author>{{ item.author }}</itunes:author>