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