Mercurial > public > sg101
comparison podcast/views.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/podcast/views.py@6e425c9b9d16 |
children | 89b240fe9297 |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """ | |
2 Views for the podcast application. | |
3 | |
4 """ | |
5 import os.path | |
6 from urlparse import urlparse | |
7 | |
8 from django.shortcuts import render_to_response | |
9 from django.template import RequestContext | |
10 from django.shortcuts import get_object_or_404 | |
11 import django.utils.simplejson as json | |
12 | |
13 from podcast.models import Channel | |
14 from podcast.models import Item | |
15 | |
16 | |
17 def get_ext_from_url(url): | |
18 """ | |
19 This function returns the extension part of the path from the given | |
20 url. | |
21 """ | |
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 | |
56 | |
57 | |
58 def index(request): | |
59 try: | |
60 channel = Channel.objects.get(pk=1) | |
61 except Channel.DoesNotExist: | |
62 channel = None | |
63 | |
64 return render_to_response('podcast/index.html', { | |
65 'channel': channel, | |
66 }, | |
67 context_instance = RequestContext(request)) | |
68 | |
69 | |
70 def detail(request, id): | |
71 podcast = get_object_or_404(Item.objects.select_related(), pk = id) | |
72 | |
73 ext = get_ext_from_url(podcast.enclosure_url) | |
74 alt_ext = None | |
75 if podcast.alt_enclosure_url: | |
76 alt_ext = get_ext_from_url(podcast.alt_enclosure_url) | |
77 | |
78 jplayer_media, jplayer_supplied = jplayer_params(ext, podcast.enclosure_url, | |
79 alt_ext, podcast.alt_enclosure_url) | |
80 | |
81 return render_to_response('podcast/detail.html', { | |
82 'channel': podcast.channel, | |
83 'podcast': podcast, | |
84 'ext': ext, | |
85 'alt_ext': alt_ext, | |
86 'jplayer_media': jplayer_media, | |
87 'jplayer_supplied': jplayer_supplied, | |
88 }, | |
89 context_instance = RequestContext(request)) | |
90 | |
91 | |
92 def feed(request): | |
93 try: | |
94 channel = Channel.objects.get(pk=1) | |
95 except Channel.DoesNotExist: | |
96 channel = None | |
97 | |
98 if channel: | |
99 channel.items = Item.objects.filter(channel=channel) | |
100 | |
101 return render_to_response('podcast/feed.xml', { | |
102 'channel': channel, | |
103 }, | |
104 context_instance = RequestContext(request)) |