website/www/conservancy/apps/news/views.py

93 lines
3.2 KiB
Python
Raw Normal View History

2015-03-03 18:40:18 +00:00
# from django.views.generic.list_detail import object_list
from django.views.generic import ListView
2015-03-04 20:35:30 +00:00
from django.shortcuts import render_to_response
2015-03-03 18:40:18 +00:00
from django.views.generic.dates import YearArchiveView, MonthArchiveView, DayArchiveView, DateDetailView
2015-03-04 19:30:51 +00:00
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from conservancy.apps.news.models import ExternalArticle
from conservancy.apps.news.models import PressRelease
from conservancy.apps.events.models import Event
from datetime import datetime
2015-03-03 18:40:18 +00:00
# for debugging...
from django.http import HttpResponse
2015-03-03 18:40:18 +00:00
class NewsListView(ListView):
extra_context = {}
def get_context_data(self, **kwargs):
context = super(NewsListView, self).get_context_data(**kwargs)
# context['key'] = 'value'
context.update(self.extra_context)
return context
def listing(request, *args, **kwargs):
news_queryset = PressRelease.objects.all()
# if (not kwargs.has_key('allow_future')) or not kwargs['allow_future']:
news_queryset = news_queryset.filter(**{'%s__lte' % kwargs['date_field']:
datetime.now()})
date_list = news_queryset.dates(kwargs['date_field'], 'year')
paginate_by = kwargs.get('paginate_by', 6) # Show 6 news items per page, by default
paginator = Paginator(news_queryset, paginate_by)
page = request.GET.get('page')
try:
news = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
news = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
news = paginator.page(paginator.num_pages)
return render_to_response('pressrelease_list.html', {"news": news, "date_list" : date_list})
2015-03-03 18:40:18 +00:00
class NewsYearArchiveView(YearArchiveView):
# queryset = Article.objects.all()
# date_field = "pub_date"
make_object_list = True
allow_future = True
# def archive_year(request, **kwargs):
# callable = NewsYearArchiveView.as_view(**kwargs)
# return callable(request)
class NewsMonthArchiveView(MonthArchiveView):
allow_future = True
# def archive_month(request, **kwargs):
# # return HttpResponse("archive_month")
# callable = NewsMonthArchiveView.as_view(**kwargs)
# return callable(request)
class NewsDayArchiveView(DayArchiveView):
allow_future = True
# def archive_day(request, **kwargs):
# # return HttpResponse("archive_day")
# callable = NewsDayArchiveView.as_view(**kwargs)
# return callable(request)
class NewsDateDetailView(DateDetailView):
# extra_context = {}
allow_future = True
# slug_url_kwarg = 'slug'
# def get_context_data(self, **kwargs):
# context = super(NewsDateDetailView, self).get_context_data(**kwargs)
# context.update(self.extra_context)
# return context
# def object_detail(request, **kwargs):
# # extra_context = {}
# # extra_context['slug'] = kwargs['slug']
# # del kwargs['slug']
# # kwargs['extra_context'] = extra_context
# # return HttpResponse("object_detail: " + str(kwargs))
# # slug = kwargs['slug']
# # del kwargs['slug']
# callable = NewsDateDetailView.as_view(**kwargs)
# return callable(request)