website/website/blog/models.py

124 lines
3.7 KiB
Python
Raw Normal View History

from typing import Any, Type
2022-06-19 19:24:29 +01:00
2022-06-19 20:55:37 +01:00
from django.db import models
from django.db.models.functions import TruncMonth
2022-06-19 21:03:05 +01:00
from django.utils import timezone
from django.utils.functional import cached_property
from modelcluster.fields import ParentalManyToManyField
2022-06-19 20:55:37 +01:00
from wagtail.admin.panels import FieldPanel
2022-06-19 19:24:29 +01:00
from website.common.models import BaseContentPage, BaseListingPage
from website.common.utils import TocEntry
from website.common.views import ContentPageFeed
2022-06-19 19:24:29 +01:00
class BlogPostListPage(BaseListingPage):
2022-06-19 19:24:29 +01:00
max_count = 1
subpage_types = [
"blog.BlogPostPage",
"blog.BlogPostTagListPage",
"blog.BlogPostCollectionListPage",
"blog.BlogPostCollectionPage",
]
2022-06-19 19:24:29 +01:00
@cached_property
def table_of_contents(self) -> list[TocEntry]:
2022-08-26 23:13:06 +01:00
post_months = sorted(
{
dt.strftime("%Y-%m")
for dt in self.get_paginator_page()
.object_list.annotate(
2022-08-26 23:13:06 +01:00
post_month=TruncMonth("date", output_field=models.DateField())
)
.values_list("post_month", flat=True)
2022-08-26 23:13:06 +01:00
}
)
return [TocEntry(post_month, post_month, 0, []) for post_month in post_months]
def get_listing_pages(self) -> models.QuerySet:
return (
BlogPostPage.objects.descendant_of(self)
.live()
2022-06-26 12:55:08 +01:00
.select_related("hero_image")
.select_related("hero_unsplash_photo")
2022-06-26 12:55:08 +01:00
.prefetch_related("tags")
2022-08-26 23:13:06 +01:00
.order_by("-date", "title")
2022-06-19 19:24:29 +01:00
)
@property
def feed_class(self) -> Type[ContentPageFeed]:
2022-07-25 21:58:06 +01:00
from .views import BlogPostPageFeed
return BlogPostPageFeed
2022-07-25 21:58:06 +01:00
2022-06-19 19:24:29 +01:00
class BlogPostPage(BaseContentPage):
2022-06-19 19:24:29 +01:00
subpage_types: list[Any] = []
parent_page_types = [BlogPostListPage, "blog.BlogPostCollectionPage"]
2022-06-19 20:55:37 +01:00
tags = ParentalManyToManyField("blog.BlogPostTagPage", blank=True)
2022-06-19 21:03:05 +01:00
date = models.DateField(default=timezone.now)
2022-06-19 20:55:37 +01:00
content_panels = BaseContentPage.content_panels + [
FieldPanel("date"),
FieldPanel("tags"),
]
class BlogPostTagListPage(BaseListingPage):
max_count = 1
parent_page_types = [BlogPostListPage]
subpage_types = ["blog.BlogPostTagPage"]
@cached_property
def table_of_contents(self) -> list[TocEntry]:
return [
TocEntry(page.title, page.slug, 0, []) for page in self.get_listing_pages()
]
class BlogPostTagPage(BaseListingPage):
subpage_types: list[Any] = []
parent_page_types = [BlogPostTagListPage]
def get_listing_pages(self) -> models.QuerySet:
blog_list_page = BlogPostListPage.objects.all().live().get()
return blog_list_page.get_listing_pages().filter(tags=self)
@property
def feed_class(self) -> Type[ContentPageFeed]:
2022-07-25 21:58:06 +01:00
from .views import BlogPostPageFeed
return BlogPostPageFeed
2022-07-25 21:58:06 +01:00
class BlogPostCollectionListPage(BaseListingPage):
subpage_types: list[Any] = []
parent_page_types = [BlogPostListPage]
max_count = 1
@cached_property
def table_of_contents(self) -> list[TocEntry]:
return [
TocEntry(page.title, page.slug, 0, []) for page in self.get_listing_pages()
]
def get_listing_pages(self) -> models.QuerySet:
blog_list_page = BlogPostListPage.objects.all().live().get()
return BlogPostCollectionPage.objects.child_of(blog_list_page).live()
class BlogPostCollectionPage(BaseListingPage):
parent_page_types = [BlogPostListPage]
subpage_types = [BlogPostPage]
def get_listing_pages(self) -> models.QuerySet:
return super().get_listing_pages().order_by("-date")
@property
def feed_class(self) -> Type[ContentPageFeed]:
from .views import BlogPostPageFeed
return BlogPostPageFeed