Files @ 956f8c6fdaad
Branch filter:

Location: website/conservancy/podjango/views.py

bsturmfels
podjango: Add "Podcast" model to support multiple podcasts

Each Cast (episode) can belong to one or more Podcast, allowing episodes to be
shared between podcasts. This enables us introductory episodes to be delivered
in their own feed, but also included in the main "The Corresponding Source"
feed.

This required adding an additional `podcast_slug` argument to most views. The
date archive views were dropped because they're not linked to from anywhere.

Added a `podcasts` view as an index of all available Podcasts.
#  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 datetime import datetime
from functools import reduce
from operator import or_

from django.shortcuts import get_object_or_404, render

from .models import Cast, CastTag, Podcast


def podcasts(request):
    podcasts = Podcast.objects.all()
    return render(request, 'podjango/podcasts.html', {'podcasts': podcasts})


def OR_filter(field_name, objs):
    from django.db.models import Q
    return reduce(or_,
                  [Q(**{field_name: x.id}) for x in objs])


def last_name(person):
    return person.formal_name.rpartition(' ')[2]


def custom_index(request, podcast_slug, *args, **kwargs):
    """Cast list view that allows scrolling and also shows an index by
    year.
    """

    podcast = get_object_or_404(Podcast, slug=podcast_slug)
    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 = Cast.objects.filter(
            podcast=podcast,
            **{'%s__lte' % date_field: datetime.now()},
        )

    tags = []
    if 'tag' in request.GET:
        tags = [get_object_or_404(CastTag, slug=tag)
                for tag in request.GET.getlist('tag')]
        extra_context['tags'] = tags
        queryset = queryset.filter(OR_filter('tags', tags), podcast=podcast)

    if tags:
        query_string = '&'.join('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 render(request, 'podjango/cast_list.html', {'podcast': podcast,
                                                       'object_list': queryset,
                                                       'tags': tags})