Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blog landing page tiles, filtering and pagination #109

Merged
merged 5 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 32 additions & 21 deletions cdhweb/blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from wagtailautocomplete.edit_handlers import AutocompletePanel

from cdhweb.pages.mixin import StandardHeroMixinNoImage
from cdhweb.pages.models import BasePage, LinkPage, PagePreviewDescriptionMixin
from cdhweb.pages.models import BasePage, ContentPage, LinkPage
from cdhweb.people.models import Person


Expand Down Expand Up @@ -186,25 +186,6 @@ def author_list(self):
"""Comma-separated list of author names."""
return ", ".join(str(author.person) for author in self.authors.all())

def get_url_parts(self, *args, **kwargs):
"""Custom blog post URLs of the form /updates/2014/03/01/my-post."""
url_parts = super().get_url_parts(*args, **kwargs)
# NOTE evidently these can sometimes be None; unclear why – perhaps it
# gets called in a context where the request is unavailable. Seems to
# happen immediately on page creation; the creation still succeeds.
if url_parts and self.first_published_at:
site_id, root_url, _ = url_parts
page_path = reverse(
"blog:detail",
kwargs={
"year": self.first_published_at.year,
# force two-digit month and day
"month": "%02d" % self.first_published_at.month,
"day": "%02d" % self.first_published_at.day,
"slug": self.slug,
},
)
return site_id, root_url, page_path

def get_sitemap_urls(self, request):
"""Override sitemap listings to add priority for featured posts."""
Expand Down Expand Up @@ -236,4 +217,34 @@ class BlogLandingPage(StandardHeroMixinNoImage, Page):

settings_panels = Page.settings_panels

subpage_types = [BlogPost]
subpage_types = [BlogPost, ContentPage]

def get_posts_for_year_and_month(self, month, year):
# get blogs by year and month
return (
self.get_children()
.live()
.filter(first_published_at__year=year, first_published_at__month=month)
.order_by("-first_published_at")
)

def get_posts_for_year(self, year):
# get blogs by year
return (
self.get_children()
.live()
.filter(first_published_at__year=year)
.order_by("-first_published_at")
)

def get_latest_posts(self):
child_pages = self.get_children().live()

# Fetch all posts ordered by most recently published
return child_pages.order_by("-first_published_at")

def get_list_of_dates(self):
# get list of dates to sort by

child_pages = self.get_children().live()
return child_pages.dates("first_published_at", "month", order="DESC")
15 changes: 7 additions & 8 deletions cdhweb/blog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@

app_name = "blog"
urlpatterns = [
path("", views.BlogIndexView.as_view(), name="list"),
re_path(r"^(?P<year>\d{4})/$", views.BlogYearArchiveView.as_view(), name="by-year"),
re_path(r"^(?P<slug>[\w-]+)/$", views.BlogLandingPageView.as_view(), name="list"),
re_path(
r"^(?P<year>\d{4})/(?P<month>\d{2})/$",
views.BlogMonthArchiveView.as_view(),
name="by-month",
r"^(?P<slug>[\w-]+)/(?P<year>\d{4})/$",
views.BlogLandingPageView.as_view(),
name="by-year",
),
re_path(
r"^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<slug>[\w-]+)/$",
views.BlogDetailView.as_view(),
name="detail",
r"^(?P<slug>[\w-]+)/(?P<year>\d{4})/(?P<month>\d{2})/$",
views.BlogLandingPageView.as_view(),
name="by-month",
),
path("rss/", views.RssBlogPostFeed(), name="rss"),
path("atom/", views.AtomBlogPostFeed(), name="atom"),
Expand Down
116 changes: 31 additions & 85 deletions cdhweb/blog/views.py
Original file line number Diff line number Diff line change
@@ -1,100 +1,46 @@
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 import ListView
from django.views.generic.detail import DetailView

from cdhweb.blog.models import BlogPost
from cdhweb.pages.views import LastModifiedListMixin, LastModifiedMixin


class BlogPostMixinView(object):
"""Mixin that sets model to BlogPost and orders/filters queryset."""

model = BlogPost
lastmodified_attr = "last_published_at"

def get_queryset(self):
"""Return published posts with most recent first."""
return BlogPost.objects.live().recent()

from cdhweb.blog.models import BlogLandingPage, BlogPost
from cdhweb.pages.views import LastModifiedMixin

class BlogPostArchiveMixin(BlogPostMixinView, LastModifiedListMixin):
"""Mixin with common settings for blogpost archive views"""

date_field = "first_published_at"
class BlogLandingPageView(ListView):
model = BlogLandingPage
template_name = "blog/blog_landing_page.html"
context_object_name = "posts"
paginate_by = 15
make_object_list = True
paginate_by = 12
template_name = "blog/blogpost_archive.html"


class BlogIndexView(BlogPostArchiveMixin, ArchiveIndexView):
"""Main blog post list view"""

date_list_period = "month"

def get_queryset(self):
return super().get_queryset().prefetch_related("page_ptr")

def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context.update({"page_title": "Latest Updates"})
return context


class BlogYearArchiveView(BlogPostArchiveMixin, YearArchiveView):
"""Blog post archive by year"""

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


class BlogMonthArchiveView(BlogPostArchiveMixin, MonthArchiveView):
"""Blog post archive by month"""

month_format = "%m"

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"),
}
)
def get_object(self):
slug = self.kwargs.get("slug")
return BlogLandingPage.objects.get(slug=slug)

def get_queryset(self, **kwargs):
month = self.kwargs.get("month")
year = self.kwargs.get("year")

if month and year:
posts = self.get_object().get_posts_for_year_and_month(
int(month), int(year)
)
elif year:
posts = self.get_object().get_posts_for_year(int(year))
else:
# if month and year are not supplied then supply all posts from newest to oldest
posts = self.get_object().get_latest_posts()

return posts

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["date_list"] = self.get_object().get_list_of_dates()
context["self"] = self.get_object()
return context


class BlogDetailView(BlogPostMixinView, DetailView, LastModifiedMixin):
"""Blog post detail view"""

context_object_name = "page"

def get(self, request, *args, **kwargs):
self.object = self.get_object()

return self.object.serve(request, *args, **kwargs)


class RssBlogPostFeed(Feed):
"""Blog post RSS feed"""

Expand Down
2 changes: 1 addition & 1 deletion cdhweb/settings/components/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,4 +449,4 @@

# Springkit Settings
BILINGUAL_HEADINGS = False
BILINGUAL_LINKS = False
BILINGUAL_LINKS = False
2 changes: 1 addition & 1 deletion cdhweb/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
path("_500/", lambda _: 1 / 0), # for testing 500 error page
# main apps
path("people/", include("cdhweb.people.urls", namespace="people")),
path("blog/", include("cdhweb.blog.urls", namespace="blog")),
path("", include("cdhweb.blog.urls", namespace="blog")),
haydngreatnews marked this conversation as resolved.
Show resolved Hide resolved
path("events/", include("cdhweb.events.urls", namespace="event")),
path("projects/", include("cdhweb.projects.urls", namespace="projects")),
# search
Expand Down
26 changes: 24 additions & 2 deletions templates/blog/blog_landing_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,31 @@
{% block body_class %}template-blog-landing{% endblock %}

{% block main %}
{% include 'includes/breadcrumbs.html' with breadcrumbs=page.breadcrumbs current_page=page %}
{% include 'includes/breadcrumbs.html' with breadcrumbs=self.breadcrumbs current_page=self %}
{% include 'includes/standard_hero.html' %}

{# fixed tile block to go here #}

<div class="archive-nav">
<a class="toggle">{{ page_title }} <i class="fa fa-chevron-down" aria-hidden="true"></i></a>
<ul class="submenu">
{# include latest blogposts page link #}
{% url 'blog:list' slug=self.slug as latest_url %}
<li {% if request.path == latest_url %}class="current"{% endif %}><a href="{{ latest_url }}">Recent</a></li>
{% for archive_date in date_list %}
{% ifchanged %}<li>{{ archive_date.year }}</li>{% endifchanged %}
{% url 'blog:by-month' year=archive_date.year month=archive_date|date:"m" slug=self.slug as blog_url %}
<li {% if blog_url == request.path %}class="current"{% endif %}>
<a href="{{ blog_url }}">{{ archive_date|date:"F" }}</a></li>
{% endfor %}
</ul>
</div>

{% for post in posts %}
{% include 'cdhpages/blocks/tile.html' with internal_page=post tile_type='internal_page_tile' %}
{% endfor %}

{% if is_paginated %}
{% include "includes/pagination.html" %}
{% endif %}

{% endblock %}
Loading