comparison gpp/podcast/views.py @ 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
parents 9175392da056
children
comparison
equal deleted inserted replaced
402:9175392da056 403:6e425c9b9d16
13 from podcast.models import Channel 13 from podcast.models import Channel
14 from podcast.models import Item 14 from podcast.models import Item
15 15
16 16
17 def get_ext_from_url(url): 17 def get_ext_from_url(url):
18 """ 18 """
19 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
20 url. 20 url.
21 """ 21 """
22 ext = os.path.splitext(urlparse(url).path)[1] 22 ext = os.path.splitext(urlparse(url).path)[1]
23 if ext.startswith('.'): 23 if ext.startswith('.'):
24 ext = ext[1:] 24 ext = ext[1:]
25 return ext 25 return ext
26 26
27 27
28 def jplayer_params(ext, url, alt_ext, alt_url): 28 def jplayer_params(ext, url, alt_ext, alt_url):
29 """ 29 """
30 Compute and returns a 2-tuple: (jplayer_media, jplayer_supplied) 30 Compute and returns a 2-tuple: (jplayer_media, jplayer_supplied)
31 where 31 where
32 jplayer_media: a string representation of the JSON for the 32 jplayer_media: a string representation of the JSON for the
33 jplayer setMedia parameter 33 jplayer setMedia parameter
34 jplayer_supplied: the string for the jplayer supplied parameter 34 jplayer_supplied: the string for the jplayer supplied parameter
35 35
36 media_list is an input list or tuple of 2-tuples of the form 36 media_list is an input list or tuple of 2-tuples of the form
37 (media_type, url) 37 (media_type, url)
38 where media_type is e.g. mp3, m4a, ogg, etc. 38 where media_type is e.g. mp3, m4a, ogg, etc.
39 39
40 """ 40 """
41 media = dict([(ext, url)]) 41 media = dict([(ext, url)])
42 if alt_ext and alt_url: 42 if alt_ext and alt_url:
43 media[alt_ext] = alt_url 43 media[alt_ext] = alt_url
44 44
45 # prefer mp4 to mp3 45 # prefer mp4 to mp3
46 if alt_ext is None: 46 if alt_ext is None:
47 supplied = [ext] 47 supplied = [ext]
48 elif ext == "m4a": 48 elif ext == "m4a":
49 supplied = (ext, alt_ext) 49 supplied = (ext, alt_ext)
50 else: 50 else:
51 supplied = (alt_ext, ext) 51 supplied = (alt_ext, ext)
52 52
53 supplied = ", ".join(supplied) 53 supplied = ", ".join(supplied)
54 54
55 return json.dumps(media), supplied 55 return json.dumps(media), supplied
56 56
57 57
58 def index(request): 58 def index(request):
59 try: 59 try:
60 channel = Channel.objects.get(pk=1) 60 channel = Channel.objects.get(pk=1)
61 except Channel.DoesNotExist: 61 except Channel.DoesNotExist:
62 channel = None 62 channel = None
63 63
64 return render_to_response('podcast/index.html', { 64 return render_to_response('podcast/index.html', {
65 'channel': channel, 65 'channel': channel,
66 }, 66 },
67 context_instance = RequestContext(request)) 67 context_instance = RequestContext(request))
68 68
69 69
70 def detail(request, id): 70 def detail(request, id):
71 podcast = get_object_or_404(Item.objects.select_related(), pk = id) 71 podcast = get_object_or_404(Item.objects.select_related(), pk = id)
72 72
73 ext = get_ext_from_url(podcast.enclosure_url) 73 ext = get_ext_from_url(podcast.enclosure_url)
74 alt_ext = None 74 alt_ext = None
75 if podcast.alt_enclosure_url: 75 if podcast.alt_enclosure_url:
76 alt_ext = get_ext_from_url(podcast.alt_enclosure_url) 76 alt_ext = get_ext_from_url(podcast.alt_enclosure_url)
77 77
78 jplayer_media, jplayer_supplied = jplayer_params(ext, podcast.enclosure_url, 78 jplayer_media, jplayer_supplied = jplayer_params(ext, podcast.enclosure_url,
79 alt_ext, podcast.alt_enclosure_url) 79 alt_ext, podcast.alt_enclosure_url)
80 80
81 return render_to_response('podcast/detail.html', { 81 return render_to_response('podcast/detail.html', {
82 'channel': podcast.channel, 82 'channel': podcast.channel,
83 'podcast': podcast, 83 'podcast': podcast,
84 'ext': ext, 84 'ext': ext,
85 'alt_ext': alt_ext, 85 'alt_ext': alt_ext,
86 'jplayer_media': jplayer_media, 86 'jplayer_media': jplayer_media,
87 'jplayer_supplied': jplayer_supplied, 87 'jplayer_supplied': jplayer_supplied,
88 }, 88 },
89 context_instance = RequestContext(request)) 89 context_instance = RequestContext(request))
90 90
91 91
92 def feed(request): 92 def feed(request):
93 try: 93 try:
94 channel = Channel.objects.get(pk=1) 94 channel = Channel.objects.get(pk=1)
95 except Channel.DoesNotExist: 95 except Channel.DoesNotExist:
96 channel = None 96 channel = None
97 return render_to_response('podcast/feed.xml', { 97
98 'channel': channel, 98 if channel:
99 }, 99 channel.items = Item.objects.filter(channel=channel)
100 context_instance = RequestContext(request)) 100
101 return render_to_response('podcast/feed.xml', {
102 'channel': channel,
103 },
104 context_instance = RequestContext(request))