annotate gpp/podcast/views.py @ 487:77d878acea5e

For #233; add social media sharing buttons to podcast pages.
author Brian Neal <bgneal@gmail.com>
date Wed, 19 Oct 2011 00:05:54 +0000
parents 6e425c9b9d16
children
rev   line source
bgneal@402 1 """
bgneal@402 2 Views for the podcast application.
bgneal@402 3
bgneal@402 4 """
bgneal@143 5 import os.path
bgneal@143 6 from urlparse import urlparse
gremmie@1 7
gremmie@1 8 from django.shortcuts import render_to_response
gremmie@1 9 from django.template import RequestContext
gremmie@1 10 from django.shortcuts import get_object_or_404
bgneal@402 11 import django.utils.simplejson as json
gremmie@1 12
gremmie@1 13 from podcast.models import Channel
gremmie@1 14 from podcast.models import Item
gremmie@1 15
gremmie@1 16
bgneal@143 17 def get_ext_from_url(url):
bgneal@403 18 """
bgneal@403 19 This function returns the extension part of the path from the given
bgneal@403 20 url.
bgneal@403 21 """
bgneal@403 22 ext = os.path.splitext(urlparse(url).path)[1]
bgneal@403 23 if ext.startswith('.'):
bgneal@403 24 ext = ext[1:]
bgneal@403 25 return ext
bgneal@402 26
bgneal@402 27
bgneal@402 28 def jplayer_params(ext, url, alt_ext, alt_url):
bgneal@403 29 """
bgneal@403 30 Compute and returns a 2-tuple: (jplayer_media, jplayer_supplied)
bgneal@403 31 where
bgneal@403 32 jplayer_media: a string representation of the JSON for the
bgneal@403 33 jplayer setMedia parameter
bgneal@403 34 jplayer_supplied: the string for the jplayer supplied parameter
bgneal@402 35
bgneal@403 36 media_list is an input list or tuple of 2-tuples of the form
bgneal@403 37 (media_type, url)
bgneal@403 38 where media_type is e.g. mp3, m4a, ogg, etc.
bgneal@402 39
bgneal@403 40 """
bgneal@403 41 media = dict([(ext, url)])
bgneal@403 42 if alt_ext and alt_url:
bgneal@403 43 media[alt_ext] = alt_url
bgneal@402 44
bgneal@403 45 # prefer mp4 to mp3
bgneal@403 46 if alt_ext is None:
bgneal@403 47 supplied = [ext]
bgneal@403 48 elif ext == "m4a":
bgneal@403 49 supplied = (ext, alt_ext)
bgneal@403 50 else:
bgneal@403 51 supplied = (alt_ext, ext)
bgneal@402 52
bgneal@403 53 supplied = ", ".join(supplied)
bgneal@402 54
bgneal@403 55 return json.dumps(media), supplied
bgneal@143 56
bgneal@143 57
gremmie@1 58 def index(request):
bgneal@403 59 try:
bgneal@403 60 channel = Channel.objects.get(pk=1)
bgneal@403 61 except Channel.DoesNotExist:
bgneal@403 62 channel = None
gremmie@1 63
bgneal@403 64 return render_to_response('podcast/index.html', {
bgneal@403 65 'channel': channel,
bgneal@403 66 },
bgneal@403 67 context_instance = RequestContext(request))
gremmie@1 68
gremmie@1 69
gremmie@1 70 def detail(request, id):
bgneal@403 71 podcast = get_object_or_404(Item.objects.select_related(), pk = id)
bgneal@143 72
bgneal@403 73 ext = get_ext_from_url(podcast.enclosure_url)
bgneal@403 74 alt_ext = None
bgneal@403 75 if podcast.alt_enclosure_url:
bgneal@403 76 alt_ext = get_ext_from_url(podcast.alt_enclosure_url)
bgneal@143 77
bgneal@403 78 jplayer_media, jplayer_supplied = jplayer_params(ext, podcast.enclosure_url,
bgneal@403 79 alt_ext, podcast.alt_enclosure_url)
bgneal@402 80
bgneal@403 81 return render_to_response('podcast/detail.html', {
bgneal@403 82 'channel': podcast.channel,
bgneal@403 83 'podcast': podcast,
bgneal@403 84 'ext': ext,
bgneal@403 85 'alt_ext': alt_ext,
bgneal@403 86 'jplayer_media': jplayer_media,
bgneal@403 87 'jplayer_supplied': jplayer_supplied,
bgneal@403 88 },
bgneal@403 89 context_instance = RequestContext(request))
bgneal@402 90
gremmie@1 91
gremmie@1 92 def feed(request):
bgneal@403 93 try:
bgneal@403 94 channel = Channel.objects.get(pk=1)
bgneal@403 95 except Channel.DoesNotExist:
bgneal@403 96 channel = None
bgneal@403 97
bgneal@403 98 if channel:
bgneal@403 99 channel.items = Item.objects.filter(channel=channel)
bgneal@403 100
bgneal@403 101 return render_to_response('podcast/feed.xml', {
bgneal@403 102 'channel': channel,
bgneal@403 103 },
bgneal@403 104 context_instance = RequestContext(request))