Create base content page rather than need mixin

This makes the subclassing much simpler, and removes type ignore hacks
This commit is contained in:
Jake Howard 2022-08-16 21:32:46 +01:00
parent 489a212b7a
commit 8952f7eae5
Signed by: jake
GPG key ID: 57AFB45680EDD477
4 changed files with 20 additions and 45 deletions

View file

@ -11,11 +11,11 @@ from wagtail.admin.panels import FieldPanel
from wagtail.contrib.routable_page.models import RoutablePageMixin, route from wagtail.contrib.routable_page.models import RoutablePageMixin, route
from wagtail.query import PageQuerySet from wagtail.query import PageQuerySet
from website.common.models import BaseContentMixin, BasePage from website.common.models import BaseContentPage
from website.common.utils import TocEntry from website.common.utils import TocEntry
class BlogListPage(BaseContentMixin, RoutablePageMixin, BasePage): # type: ignore[misc] class BlogListPage(RoutablePageMixin, BaseContentPage):
max_count = 1 max_count = 1
subpage_types = [ subpage_types = [
"blog.BlogPostPage", "blog.BlogPostPage",
@ -23,8 +23,6 @@ class BlogListPage(BaseContentMixin, RoutablePageMixin, BasePage): # type: igno
"blog.BlogCollectionListPage", "blog.BlogCollectionListPage",
"blog.BlogCollectionPage", "blog.BlogCollectionPage",
] ]
content_panels = BasePage.content_panels + BaseContentMixin.content_panels
search_fields = BasePage.search_fields + BaseContentMixin.search_fields
@cached_property @cached_property
def reading_time(self) -> int: def reading_time(self) -> int:
@ -69,29 +67,24 @@ class BlogListPage(BaseContentMixin, RoutablePageMixin, BasePage): # type: igno
)(request) )(request)
class BlogPostPage(BaseContentMixin, BasePage): # type: ignore[misc] class BlogPostPage(BaseContentPage):
subpage_types: list[Any] = [] subpage_types: list[Any] = []
parent_page_types = [BlogListPage, "blog.BlogCollectionPage"] parent_page_types = [BlogListPage, "blog.BlogCollectionPage"]
search_fields = BasePage.search_fields + BaseContentMixin.search_fields
tags = ParentalManyToManyField("blog.BlogPostTagPage", blank=True) tags = ParentalManyToManyField("blog.BlogPostTagPage", blank=True)
date = models.DateField(default=timezone.now) date = models.DateField(default=timezone.now)
content_panels = ( content_panels = BaseContentPage.content_panels + [
BasePage.content_panels FieldPanel("date"),
+ BaseContentMixin.content_panels FieldPanel("tags"),
+ [FieldPanel("date"), FieldPanel("tags")] ]
)
class BlogPostTagListPage(BaseContentMixin, BasePage): # type: ignore[misc] class BlogPostTagListPage(BaseContentPage):
max_count = 1 max_count = 1
parent_page_types = [BlogListPage] parent_page_types = [BlogListPage]
subpage_types = ["blog.BlogPostTagPage"] subpage_types = ["blog.BlogPostTagPage"]
content_panels = BasePage.content_panels + BaseContentMixin.content_panels
search_fields = BasePage.search_fields + BaseContentMixin.search_fields
@cached_property @cached_property
def table_of_contents(self) -> list[TocEntry]: def table_of_contents(self) -> list[TocEntry]:
return [TocEntry(page.title, page.slug, 0, []) for page in self.get_tags()] return [TocEntry(page.title, page.slug, 0, []) for page in self.get_tags()]
@ -105,13 +98,10 @@ class BlogPostTagListPage(BaseContentMixin, BasePage): # type: ignore[misc]
return context return context
class BlogPostTagPage(BaseContentMixin, RoutablePageMixin, BasePage): # type: ignore[misc] class BlogPostTagPage(RoutablePageMixin, BaseContentPage):
subpage_types: list[Any] = [] subpage_types: list[Any] = []
parent_page_types = [BlogPostTagListPage] parent_page_types = [BlogPostTagListPage]
content_panels = BasePage.content_panels + BaseContentMixin.content_panels
search_fields = BasePage.search_fields + BaseContentMixin.search_fields
@cached_property @cached_property
def table_of_contents(self) -> list[TocEntry]: def table_of_contents(self) -> list[TocEntry]:
return [ return [
@ -136,14 +126,11 @@ class BlogPostTagPage(BaseContentMixin, RoutablePageMixin, BasePage): # type: i
)(request) )(request)
class BlogCollectionListPage(BaseContentMixin, BasePage): # type: ignore[misc] class BlogCollectionListPage(BaseContentPage):
subpage_types: list[Any] = [] subpage_types: list[Any] = []
parent_page_types = [BlogListPage] parent_page_types = [BlogListPage]
max_count = 1 max_count = 1
content_panels = BasePage.content_panels + BaseContentMixin.content_panels
search_fields = BasePage.search_fields + BaseContentMixin.search_fields
@cached_property @cached_property
def table_of_contents(self) -> list[TocEntry]: def table_of_contents(self) -> list[TocEntry]:
return [ return [
@ -160,13 +147,10 @@ class BlogCollectionListPage(BaseContentMixin, BasePage): # type: ignore[misc]
return context return context
class BlogCollectionPage(BaseContentMixin, BasePage): # type: ignore[misc] class BlogCollectionPage(BaseContentPage):
parent_page_types = [BlogListPage] parent_page_types = [BlogListPage]
subpage_types = [BlogPostPage] subpage_types = [BlogPostPage]
content_panels = BasePage.content_panels + BaseContentMixin.content_panels
search_fields = BasePage.search_fields + BaseContentMixin.search_fields
@cached_property @cached_property
def table_of_contents(self) -> list[TocEntry]: def table_of_contents(self) -> list[TocEntry]:
return [ return [

View file

@ -37,7 +37,7 @@ class BasePage(Page):
return self.get_ancestors().exclude(depth__lte=2) return self.get_ancestors().exclude(depth__lte=2)
class BaseContentMixin(models.Model): class BaseContentPage(BasePage):
subtitle = models.CharField(max_length=255, blank=True) subtitle = models.CharField(max_length=255, blank=True)
hero_image = models.ForeignKey( hero_image = models.ForeignKey(
get_image_model_string(), null=True, blank=True, on_delete=models.SET_NULL get_image_model_string(), null=True, blank=True, on_delete=models.SET_NULL
@ -47,14 +47,14 @@ class BaseContentMixin(models.Model):
) )
body = StreamField(get_blocks(), blank=True, use_json_field=True) body = StreamField(get_blocks(), blank=True, use_json_field=True)
content_panels = [ content_panels = BasePage.content_panels + [
FieldPanel("subtitle"), FieldPanel("subtitle"),
FieldPanel("hero_image"), FieldPanel("hero_image"),
FieldPanel("hero_unsplash_photo", widget=UnsplashPhotoChooser), FieldPanel("hero_unsplash_photo", widget=UnsplashPhotoChooser),
FieldPanel("body"), FieldPanel("body"),
] ]
search_fields = [ search_fields = BasePage.search_fields + [
index.SearchField("body"), index.SearchField("body"),
index.SearchField("subtitle"), index.SearchField("subtitle"),
] ]
@ -106,16 +106,11 @@ class BaseContentMixin(models.Model):
return None return None
class ContentPage(BasePage, BaseContentMixin): # type: ignore[misc] class ContentPage(BaseContentPage):
subpage_types: list[Any] = [] subpage_types: list[Any] = []
content_panels = BasePage.content_panels + BaseContentMixin.content_panels
search_fields = BasePage.search_fields + BaseContentMixin.search_fields
class ListingPage(BasePage, BaseContentMixin): # type: ignore[misc] class ListingPage(BaseContentPage):
content_panels = BasePage.content_panels + BaseContentMixin.content_panels
search_fields = BasePage.search_fields + BaseContentMixin.search_fields
def get_context(self, request: HttpRequest) -> dict: def get_context(self, request: HttpRequest) -> dict:
context = super().get_context(request) context = super().get_context(request)
context["child_pages"] = ( context["child_pages"] = (

View file

@ -6,7 +6,7 @@ from wagtail.admin.panels import FieldPanel
from wagtail.search import index from wagtail.search import index
from wagtail.snippets.models import register_snippet from wagtail.snippets.models import register_snippet
from website.common.models import BaseContentMixin, BasePage from website.common.models import BaseContentPage
from website.common.utils import TocEntry from website.common.utils import TocEntry
@ -36,11 +36,9 @@ class OnlineAccount(models.Model, index.Indexed):
return slugify(self.name) return slugify(self.name)
class ContactPage(BaseContentMixin, BasePage): # type: ignore[misc] class ContactPage(BaseContentPage):
max_count = 1 max_count = 1
subpage_types: list = [] subpage_types: list = []
content_panels = BasePage.content_panels + BaseContentMixin.content_panels
search_fields = BasePage.search_fields + BaseContentMixin.search_fields
@cached_property @cached_property
def reading_time(self) -> int: def reading_time(self) -> int:

View file

@ -10,19 +10,17 @@ from wagtail.models import Page
from wagtail.query import PageQuerySet from wagtail.query import PageQuerySet
from wagtail.search.utils import parse_query_string from wagtail.search.utils import parse_query_string
from website.common.models import BaseContentMixin, BasePage from website.common.models import BaseContentPage
from website.common.utils import TocEntry from website.common.utils import TocEntry
from website.home.models import HomePage from website.home.models import HomePage
from .serializers import MIN_SEARCH_LENGTH, SearchParamsSerializer from .serializers import MIN_SEARCH_LENGTH, SearchParamsSerializer
class SearchPage(BaseContentMixin, RoutablePageMixin, BasePage): # type: ignore[misc] class SearchPage(RoutablePageMixin, BaseContentPage):
max_count = 1 max_count = 1
subpage_types: list = [] subpage_types: list = []
parent_page_types = ["home.HomePage"] parent_page_types = ["home.HomePage"]
content_panels = BasePage.content_panels + BaseContentMixin.content_panels
search_fields = BasePage.search_fields + BaseContentMixin.search_fields
PAGE_SIZE = 15 PAGE_SIZE = 15
@cached_property @cached_property