Files
@ 0ef963a5bd8f
Branch filter:
Location: website/www/conservancy/apps/blog/views.py
0ef963a5bd8f
5.7 KiB
text/x-python
img: Add picture of LCA Outreachy donation push.
Captured from the video at <https://www.youtube.com/watch?v=brNmfD1Lb7M>,
around the 2:00:00 mark.
Captured from the video at <https://www.youtube.com/watch?v=brNmfD1Lb7M>,
around the 2:00:00 mark.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | from conservancy.apps.blog.models import Entry, EntryTag # relative import
# from django.views.generic.list_detail import object_list
from django.views.generic import ListView
from django.views.generic.dates import YearArchiveView, MonthArchiveView, DayArchiveView, DateDetailView
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from conservancy.apps.staff.models import Person
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
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):
"""Blog 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(EntryTag, 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)
kwargs['queryset'] = queryset
kwargs['extra_context'] = extra_context
paginate_by = kwargs.get('paginate_by', 6) # Show 6 news items per page, by default
paginator = Paginator(queryset, paginate_by)
page = request.GET.get('page')
try:
blog_entries = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
blog_entries = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
blog_entires = paginator.page(paginator.num_pages)
extra_context['blog_entries'] = blog_entries
return render_to_response('blog/entry_list.html', extra_context, context_instance=RequestContext(request))
def techblog_redirect(request):
"""Redirect from the old 'techblog' to the new blog
"""
path = request.path[len('/technology'):]
if path == '/blog/':
path += "?author=bkuhn"
return relative_redirect(request, path)
def query(request):
"""Page to query the blog based on authors 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 = '/blog/'
if 'rss' in d:
base_url = '/feeds/blog/'
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:
authors = sorted(Person.objects.filter(currently_employed=True,
entry__isnull=False).distinct(),
key=last_name)
tags = EntryTag.objects.all().order_by('label')
return render_to_response('blog/query.html',
{'authors': authors, 'tags': tags}, context_instance=RequestContext(request))
def relative_redirect(request, path):
from django import http
from django.conf import settings
host = request.get_host()
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)
class BlogYearArchiveView(YearArchiveView):
make_object_list = True
allow_future = True
extra_context = {}
def get_context_data(self, **kwargs):
context = super(BlogYearArchiveView, self).get_context_data(**kwargs)
context.update(self.extra_context)
return context
class BlogMonthArchiveView(MonthArchiveView):
allow_future = True
extra_context = {}
def get_context_data(self, **kwargs):
context = super(BlogMonthArchiveView, self).get_context_data(**kwargs)
context.update(self.extra_context)
return context
class BlogDayArchiveView(DayArchiveView):
allow_future = True
extra_context = {}
def get_context_data(self, **kwargs):
context = super(BlogDayArchiveView, self).get_context_data(**kwargs)
context.update(self.extra_context)
return context
class BlogDateDetailView(DateDetailView):
allow_future = True
extra_context = {}
def get_context_data(self, **kwargs):
context = super(BlogDateDetailView, self).get_context_data(**kwargs)
context.update(self.extra_context)
return context
|