Source code for cdhweb.blog.views
from datetime import datetime
from django.contrib.syndication.views import Feed
from django.utils.feedgenerator import Atom1Feed
from django.views.generic.dates import (
ArchiveIndexView,
MonthArchiveView,
YearArchiveView,
)
from django.views.generic.detail import DetailView
from cdhweb.blog.models import BlogPost
from cdhweb.pages.views import LastModifiedListMixin, LastModifiedMixin
[docs]class BlogPostMixinView(object):
"""Mixin that sets model to BlogPost and orders/filters queryset."""
model = BlogPost
lastmodified_attr = "last_published_at"
[docs] def get_queryset(self):
"""Return published posts with most recent first."""
return BlogPost.objects.live().recent()
[docs]class BlogPostArchiveMixin(BlogPostMixinView, LastModifiedListMixin):
"""Mixin with common settings for blogpost archive views"""
date_field = "first_published_at"
context_object_name = "posts"
make_object_list = True
paginate_by = 12
template_name = "blog/blogpost_archive.html"
[docs]class BlogIndexView(BlogPostArchiveMixin, ArchiveIndexView):
"""Main blog post list view"""
date_list_period = "month"
[docs] def get_queryset(self):
return super().get_queryset().prefetch_related("page_ptr")
[docs] def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context.update({"page_title": "Latest Updates"})
return context
[docs]class BlogYearArchiveView(BlogPostArchiveMixin, YearArchiveView):
"""Blog post archive by year"""
[docs] def get_context_data(self, *args, **kwargs):
context = super(BlogYearArchiveView, self).get_context_data(*args, **kwargs)
context.update(
{
"date_list": BlogPost.objects.dates(
self.date_field, "month", order="DESC"
),
"page_title": "%s Updates" % self.kwargs["year"],
}
)
return context
[docs]class BlogMonthArchiveView(BlogPostArchiveMixin, MonthArchiveView):
"""Blog post archive by month"""
month_format = "%m"
[docs] def get_context_data(self, *args, **kwargs):
context = super(BlogMonthArchiveView, self).get_context_data(*args, **kwargs)
# current requested month/year for display
date = datetime.strptime("%(year)s %(month)s" % self.kwargs, "%Y %m")
context.update(
{
"date_list": BlogPost.objects.dates(
self.date_field, "month", order="DESC"
),
"page_title": "%s Updates" % date.strftime("%B %Y"),
}
)
return context
[docs]class BlogDetailView(BlogPostMixinView, DetailView, LastModifiedMixin):
"""Blog post detail view"""
context_object_name = "page"
[docs] def get_context_data(self, *args, **kwargs):
"""Add next/previous post to context."""
context = super(BlogDetailView, self).get_context_data(*args, **kwargs)
# NOTE mezzanine Displayable previously handled this; we need to do it
# manually for Wagtail. See:
# http://mezzanine.jupo.org/docs/_modules/mezzanine/core/models.html#Displayable.get_next_by_publish_date
next = self.model.objects.filter(
live=True, first_published_at__gt=self.object.first_published_at
).order_by("first_published_at")
if next.exists():
next = next[0]
else:
next = None
prev = self.model.objects.filter(
live=True, first_published_at__lt=self.object.first_published_at
).order_by("-first_published_at")
if prev.exists():
prev = prev[0]
else:
prev = None
context.update({"opengraph_type": "article", "next": next, "previous": prev})
return context
[docs]class AtomBlogPostFeed(RssBlogPostFeed):
"""Blog post Atom feed"""
feed_type = Atom1Feed
subtitle = RssBlogPostFeed.description