bgneal@402: """ bgneal@402: Views for the podcast application. bgneal@402: bgneal@402: """ bgneal@679: import json bgneal@143: import os.path bgneal@143: from urlparse import urlparse gremmie@1: bgneal@1032: from django.shortcuts import render gremmie@1: from django.shortcuts import get_object_or_404 gremmie@1: gremmie@1: from podcast.models import Channel gremmie@1: from podcast.models import Item gremmie@1: gremmie@1: bgneal@143: def get_ext_from_url(url): bgneal@403: """ bgneal@403: This function returns the extension part of the path from the given bgneal@403: url. bgneal@403: """ bgneal@403: ext = os.path.splitext(urlparse(url).path)[1] bgneal@403: if ext.startswith('.'): bgneal@403: ext = ext[1:] bgneal@403: return ext bgneal@402: bgneal@402: bgneal@402: def jplayer_params(ext, url, alt_ext, alt_url): bgneal@403: """ bgneal@403: Compute and returns a 2-tuple: (jplayer_media, jplayer_supplied) bgneal@403: where bgneal@403: jplayer_media: a string representation of the JSON for the bgneal@403: jplayer setMedia parameter bgneal@403: jplayer_supplied: the string for the jplayer supplied parameter bgneal@402: bgneal@403: media_list is an input list or tuple of 2-tuples of the form bgneal@403: (media_type, url) bgneal@403: where media_type is e.g. mp3, m4a, ogg, etc. bgneal@402: bgneal@403: """ bgneal@403: media = dict([(ext, url)]) bgneal@403: if alt_ext and alt_url: bgneal@403: media[alt_ext] = alt_url bgneal@402: bgneal@403: # prefer mp4 to mp3 bgneal@403: if alt_ext is None: bgneal@403: supplied = [ext] bgneal@403: elif ext == "m4a": bgneal@403: supplied = (ext, alt_ext) bgneal@403: else: bgneal@403: supplied = (alt_ext, ext) bgneal@402: bgneal@403: supplied = ", ".join(supplied) bgneal@402: bgneal@403: return json.dumps(media), supplied bgneal@143: bgneal@143: gremmie@1: def index(request): bgneal@403: try: bgneal@403: channel = Channel.objects.get(pk=1) bgneal@403: except Channel.DoesNotExist: bgneal@403: channel = None gremmie@1: bgneal@1032: return render(request, 'podcast/index.html', { bgneal@403: 'channel': channel, bgneal@1086: 'V3_DESIGN': True, bgneal@1032: }) gremmie@1: gremmie@1: gremmie@1: def detail(request, id): bgneal@1086: podcast = get_object_or_404(Item.objects.select_related(), pk=id) bgneal@143: bgneal@403: ext = get_ext_from_url(podcast.enclosure_url) bgneal@403: alt_ext = None bgneal@403: if podcast.alt_enclosure_url: bgneal@403: alt_ext = get_ext_from_url(podcast.alt_enclosure_url) bgneal@143: bgneal@403: jplayer_media, jplayer_supplied = jplayer_params(ext, podcast.enclosure_url, bgneal@403: alt_ext, podcast.alt_enclosure_url) bgneal@402: bgneal@1032: return render(request, 'podcast/detail.html', { bgneal@403: 'channel': podcast.channel, bgneal@403: 'podcast': podcast, bgneal@403: 'ext': ext, bgneal@403: 'alt_ext': alt_ext, bgneal@403: 'jplayer_media': jplayer_media, bgneal@403: 'jplayer_supplied': jplayer_supplied, bgneal@1086: 'V3_DESIGN': True, bgneal@1032: }) bgneal@402: gremmie@1: gremmie@1: def feed(request): bgneal@403: try: bgneal@403: channel = Channel.objects.get(pk=1) bgneal@403: except Channel.DoesNotExist: bgneal@403: channel = None bgneal@403: bgneal@403: if channel: bgneal@403: channel.items = Item.objects.filter(channel=channel) bgneal@403: bgneal@1032: return render(request, 'podcast/feed.xml', { bgneal@403: 'channel': channel, bgneal@1032: })