website/conservancy/podjango/views.py

80 lines
2.7 KiB
Python
Raw Normal View History

# 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])
2024-02-23 04:02:04 +00:00
def last_name(person):
return person.formal_name.rpartition(' ')[2]
2024-02-23 04:02:04 +00:00
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})