Added initial code for the podcast app, as running on SFLC server
This commit is contained in:
Bradley M. Kuhn 2008-11-17 15:21:40 -05:00
parent 9f7637c705
commit bddb5cf735
5 changed files with 298 additions and 0 deletions

0
apps/podcast/__init__.py Normal file
View file

36
apps/podcast/admin.py Normal file
View file

@ -0,0 +1,36 @@
# Copyright (C) 2008 Bradley M. Kuhn <bkuhn@ebb.org>
# Copyright (C) 2006, 2007 Software Freedom Law Center, Inc.
#
# This software's license gives you freedom; you can copy, convey,
# propogate, redistribute and/or modify this program under the terms of
# the GNU Affero General Public License (AGPL) as published by the Free
# Software Foundation (FSF), either version 3 of the License, or (at your
# option) any later version of the AGPL published by the FSF.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program in a file in the toplevel directory called
# "AGPLv3". If not, see <http://www.gnu.org/licenses/>.
#
from django.contrib import admin
from models import PodcastTag, Podcast
class PodcastTagAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug': ('label',)}
admin.site.register(PodcastTag, PodcastTagAdmin)
class PodcastAdmin(admin.ModelAdmin):
list_display = ('pub_date', 'title')
list_filter = ['pub_date']
date_hierarchy = 'pub_date'
search_fields = ['title', 'summary', 'body']
prepopulated_fields = {'slug': ("title",)}
filter_horizontal = ('tags',)
admin.site.register(Podcast, PodcastAdmin)

76
apps/podcast/models.py Normal file
View file

@ -0,0 +1,76 @@
# Copyright (C) 2008 Bradley M. Kuhn <bkuhn@ebb.org>
# Copyright (C) 2006, 2007 Software Freedom Law Center, Inc.
#
# This software's license gives you freedom; you can copy, convey,
# propogate, redistribute and/or modify this program under the terms of
# the GNU Affero General Public License (AGPL) as published by the Free
# Software Foundation (FSF), either version 3 of the License, or (at your
# option) any later version of the AGPL published by the FSF.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program in a file in the toplevel directory called
# "AGPLv3". If not, see <http://www.gnu.org/licenses/>.
#
from django.db import models
from django.conf import settings
from apps.staff.models import Person
from datetime import datetime, timedelta
class PodcastTag(models.Model):
"""Tagging for podcasts"""
label = models.CharField(max_length=100)
slug = models.SlugField()
class Meta:
db_table = 'podcast_tags' # legacy
def __unicode__(self):
return self.label
def get_absolute_url(self):
return u"/podcast/?tag=%s" % self.slug
class Podcast(models.Model):
"""Podcast"""
title = models.CharField(max_length=200)
slug = models.SlugField(unique=True)
summary = models.TextField(help_text="Use raw HTML. This summary is not included at the beginning of the body when the entry is displayed. It used only for the material on the front page.")
body = models.TextField(help_text="Use raw HTML. Include the full body of any show notes or other information about this episode. It will be labelled on the site as Show Notes. It is included on the detail entry, and in the description data on the podcast RSS feed.")
pub_date = models.DateTimeField()
poster = models.ForeignKey(Person)
tags = models.ManyToManyField(PodcastTag, null=True, blank=True)
ogg_path = models.CharField(max_length=300, blank=True,
help_text="Local filename of the Ogg file, relative to webroot. File should be uploaded into the static media area for podcasts.")
mp3_path = models.CharField(max_length=300, blank=True,
help_text="Local filename of the mp3 file, relative to webroot. File should be uploaded into the static media area for podcasts.")
ogg_length = models.IntegerField(blank=False, help_text="size in bytes of ogg file")
mp3_length = models.IntegerField(blank=False, help_text="size in bytes of mp3 file")
duration = models.CharField(max_length=8, blank=False, help_text="length in hh:mm:ss of mp3 file")
date_created = models.DateTimeField(auto_now_add=True)
date_last_modified = models.DateTimeField(auto_now=True)
class Meta:
db_table = 'podcasts_entries' # legacy
verbose_name_plural = 'podcasts'
ordering = ('-pub_date',)
get_latest_by = 'pub_date'
def __unicode__(self):
return self.title
def get_absolute_url(self):
return u"/podcast/%s/%s/" % (self.pub_date.strftime("%Y/%b/%d").lower(),
self.slug)
# FIXME
# return (u"/podcast/%s/" % (self.slug))
def is_recent(self):
return self.pub_date > (datetime.now() - timedelta(days=14))
# question: does datetime.now() do a syscall each time is it called?

79
apps/podcast/urls.py Normal file
View file

@ -0,0 +1,79 @@
# Copyright (C) 2008 Bradley M. Kuhn <bkuhn@ebb.org>
# Copyright (C) 2006, 2007 Software Freedom Law Center, Inc.
#
# This software's license gives you freedom; you can copy, convey,
# propogate, redistribute and/or modify this program under the terms of
# the GNU Affero General Public License (AGPL) as published by the Free
# Software Foundation (FSF), either version 3 of the License, or (at your
# option) any later version of the AGPL published by the FSF.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program in a file in the toplevel directory called
# "AGPLv3". If not, see <http://www.gnu.org/licenses/>.
#
from django.conf.urls.defaults import *
from models import Podcast, PodcastTag # relative import
from apps.staff.models import Person
from datetime import datetime
extra_context = {}
info_dict = {
'queryset': Podcast.objects.all(),
'date_field': 'pub_date',
'extra_context': extra_context,
}
urlpatterns = patterns('django.views.generic.date_based',
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>[-\w]+)/$', 'object_detail', dict(info_dict, slug_field='slug')),
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/$', 'archive_day', info_dict),
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$', 'archive_month', info_dict),
(r'^(?P<year>\d{4})/$', 'archive_year', dict(info_dict,
make_object_list=True)),
# FIXME HOW DO I MAKE THE SLUG WORK WITH NO DATES IN IT.
# (r'^(?P<slug>[-\w]+)/$', 'object_detail', dict(info_dict, slug_field='slug')),
)
urlpatterns += patterns('apps.podcast.views',
(r'^/?$', 'custom_index', dict(info_dict, paginate_by=20)),
(r'^query/$', 'query'),
)
# Code to display authors and tags on each blog page
def all_tags_by_use_amount():
"""Returns all tags with an added 'cnt' attribute (how many times used)
Also sorts the tags so most-used tags appear first.
"""
# tally use amount
retval = []
current = None
for obj in PodcastTag.objects.filter(podcast__pub_date__lte=datetime.now(),
podcast__isnull=False).order_by('label'):
if current is not None and obj.id == current.id:
current.cnt += 1
else:
if current is not None:
retval.append(current)
current = obj
current.cnt = 1
if current is not None:
retval.append(current)
# sort and return
retval.sort(key=lambda x: -x.cnt)
return retval
# The functions are passed to the context uncalled so they will be
# called for each web request. If we want to only make these database
# queries a single time when a web server process begins, call both
# functions below (i.e. make both lines below end in '()')
extra_context['all_tags'] = all_tags_by_use_amount

107
apps/podcast/views.py Normal file
View file

@ -0,0 +1,107 @@
# Copyright (C) 2008 Bradley M. Kuhn <bkuhn@ebb.org>
# Copyright (C) 2006, 2007 Software Freedom Law Center, Inc.
#
# This software's license gives you freedom; you can copy, convey,
# propogate, redistribute and/or modify this program under the terms of
# the GNU Affero General Public License (AGPL) as published by the Free
# Software Foundation (FSF), either version 3 of the License, or (at your
# option) any later version of the AGPL published by the FSF.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program in a file in the toplevel directory called
# "AGPLv3". If not, see <http://www.gnu.org/licenses/>.
#
from models import Podcast, PodcastTag # relative import
from django.views.generic.list_detail import object_list
from apps.staff.models import Person
from django.shortcuts import get_object_or_404, render_to_response
from datetime import datetime
def OR_filter(field_name, objs):
from django.db.models import Q
return reduce(lambda x, y: x | y,
[Q(**{field_name: x.id}) for x in objs])
def last_name(person):
return person.formal_name.rpartition(' ')[2]
def custom_index(request, queryset, *args, **kwargs):
"""Podcast list view that allows scrolling and also shows an index by
year.
"""
kwargs = kwargs.copy()
kwargs['extra_context'] = kwargs.get('extra_context', {}).copy()
extra_context = kwargs['extra_context']
date_field = kwargs['date_field']
del kwargs['date_field']
if not kwargs.get('allow_future', False):
queryset = queryset.filter(**{'%s__lte' % date_field: datetime.now()})
authors = []
if 'author' in request.GET:
authors = [get_object_or_404(Person, username=author)
for author in request.GET.getlist('author')]
extra_context['authors'] = authors
queryset = queryset.filter(OR_filter('author', authors))
tags = []
if 'tag' in request.GET:
tags = [get_object_or_404(PodcastTag, slug=tag)
for tag in request.GET.getlist('tag')]
extra_context['tags'] = tags
queryset = queryset.filter(OR_filter('tags', tags))
if authors or tags:
query_string = '&'.join(['author=%s' % a.username for a in authors]
+ ['tag=%s' % t.slug for t in tags])
extra_context['query_string'] = query_string
else:
date_list = queryset.dates(date_field, 'year')
extra_context['date_list'] = date_list
return object_list(request, queryset, *args, **kwargs)
def query(request):
"""Page to query the podcast based on and tags
"""
if request.GET:
d = request.GET.copy()
if 'authors' in d.getlist('all'):
d.setlist('author', []) # remove author queries
if 'tags' in d.getlist('all'):
d.setlist('tag', []) # remove tag queries
d.setlist('all', []) # remove "all" from the query string
base_url = '/podcast/'
if 'rss' in d:
base_url = '/feeds/podcast/'
d.setlist('rss', []) # remove it
query_string = d.urlencode()
return relative_redirect(request, '%s%s%s' % (base_url, '?' if query_string else '', query_string))
else:
tags = PodcastTag.objects.all().order_by('label')
return render_to_response('podcast/query.html', {'tags': tags})
def relative_redirect(request, path):
from django import http
from django.conf import settings
host = http.get_host(request)
if settings.FORCE_CANONICAL_HOSTNAME:
host = settings.FORCE_CANONICAL_HOSTNAME
url = "%s://%s%s" % (request.is_secure() and 'https' or 'http', host, path)
return http.HttpResponseRedirect(url)