# HG changeset patch # User Brian Neal # Date 1301190678 0 # Node ID 9175392da05671bf0d29ed6036fb64b992d36d06 # Parent 463649d7698b562c06ede5cfd222dde4c5f4aca1 Fixing #198; add a media player to the podcast pages. diff -r 463649d7698b -r 9175392da056 gpp/podcast/views.py --- a/gpp/podcast/views.py Sat Mar 26 21:30:36 2011 +0000 +++ b/gpp/podcast/views.py Sun Mar 27 01:51:18 2011 +0000 @@ -1,10 +1,14 @@ -"""Views for the podcast application""" +""" +Views for the podcast application. + +""" import os.path from urlparse import urlparse from django.shortcuts import render_to_response from django.template import RequestContext from django.shortcuts import get_object_or_404 +import django.utils.simplejson as json from podcast.models import Channel from podcast.models import Item @@ -15,7 +19,40 @@ This function returns the extension part of the path from the given url. """ - return os.path.splitext(urlparse(url).path)[1] + 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 + + 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 + + # 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) + + return json.dumps(media), supplied def index(request): @@ -25,7 +62,7 @@ channel = None return render_to_response('podcast/index.html', { - 'channel': channel, + 'channel': channel, }, context_instance = RequestContext(request)) @@ -38,14 +75,19 @@ 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) + return render_to_response('podcast/detail.html', { 'channel': podcast.channel, - 'podcast': podcast, + 'podcast': podcast, 'ext': ext, 'alt_ext': alt_ext, + 'jplayer_media': jplayer_media, + 'jplayer_supplied': jplayer_supplied, }, context_instance = RequestContext(request)) - + def feed(request): try: @@ -53,6 +95,6 @@ except Channel.DoesNotExist: channel = None return render_to_response('podcast/feed.xml', { - 'channel': channel, + 'channel': channel, }, context_instance = RequestContext(request)) diff -r 463649d7698b -r 9175392da056 gpp/templates/base.html --- a/gpp/templates/base.html Sat Mar 26 21:30:36 2011 +0000 +++ b/gpp/templates/base.html Sun Mar 27 01:51:18 2011 +0000 @@ -15,7 +15,7 @@ - +