From 8952f7eae58e2d30c7952bc2196093836256c6b1 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 16 Aug 2022 21:32:46 +0100 Subject: [PATCH] Create base content page rather than need mixin This makes the subclassing much simpler, and removes type ignore hacks --- website/blog/models.py | 38 +++++++++++--------------------------- website/common/models.py | 15 +++++---------- website/contact/models.py | 6 ++---- website/search/models.py | 6 ++---- 4 files changed, 20 insertions(+), 45 deletions(-) diff --git a/website/blog/models.py b/website/blog/models.py index 94eff1d..4e5072d 100644 --- a/website/blog/models.py +++ b/website/blog/models.py @@ -11,11 +11,11 @@ from wagtail.admin.panels import FieldPanel from wagtail.contrib.routable_page.models import RoutablePageMixin, route from wagtail.query import PageQuerySet -from website.common.models import BaseContentMixin, BasePage +from website.common.models import BaseContentPage from website.common.utils import TocEntry -class BlogListPage(BaseContentMixin, RoutablePageMixin, BasePage): # type: ignore[misc] +class BlogListPage(RoutablePageMixin, BaseContentPage): max_count = 1 subpage_types = [ "blog.BlogPostPage", @@ -23,8 +23,6 @@ class BlogListPage(BaseContentMixin, RoutablePageMixin, BasePage): # type: igno "blog.BlogCollectionListPage", "blog.BlogCollectionPage", ] - content_panels = BasePage.content_panels + BaseContentMixin.content_panels - search_fields = BasePage.search_fields + BaseContentMixin.search_fields @cached_property def reading_time(self) -> int: @@ -69,29 +67,24 @@ class BlogListPage(BaseContentMixin, RoutablePageMixin, BasePage): # type: igno )(request) -class BlogPostPage(BaseContentMixin, BasePage): # type: ignore[misc] +class BlogPostPage(BaseContentPage): subpage_types: list[Any] = [] parent_page_types = [BlogListPage, "blog.BlogCollectionPage"] - search_fields = BasePage.search_fields + BaseContentMixin.search_fields tags = ParentalManyToManyField("blog.BlogPostTagPage", blank=True) date = models.DateField(default=timezone.now) - content_panels = ( - BasePage.content_panels - + BaseContentMixin.content_panels - + [FieldPanel("date"), FieldPanel("tags")] - ) + content_panels = BaseContentPage.content_panels + [ + FieldPanel("date"), + FieldPanel("tags"), + ] -class BlogPostTagListPage(BaseContentMixin, BasePage): # type: ignore[misc] +class BlogPostTagListPage(BaseContentPage): max_count = 1 parent_page_types = [BlogListPage] subpage_types = ["blog.BlogPostTagPage"] - content_panels = BasePage.content_panels + BaseContentMixin.content_panels - search_fields = BasePage.search_fields + BaseContentMixin.search_fields - @cached_property def table_of_contents(self) -> list[TocEntry]: 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 -class BlogPostTagPage(BaseContentMixin, RoutablePageMixin, BasePage): # type: ignore[misc] +class BlogPostTagPage(RoutablePageMixin, BaseContentPage): subpage_types: list[Any] = [] parent_page_types = [BlogPostTagListPage] - content_panels = BasePage.content_panels + BaseContentMixin.content_panels - search_fields = BasePage.search_fields + BaseContentMixin.search_fields - @cached_property def table_of_contents(self) -> list[TocEntry]: return [ @@ -136,14 +126,11 @@ class BlogPostTagPage(BaseContentMixin, RoutablePageMixin, BasePage): # type: i )(request) -class BlogCollectionListPage(BaseContentMixin, BasePage): # type: ignore[misc] +class BlogCollectionListPage(BaseContentPage): subpage_types: list[Any] = [] parent_page_types = [BlogListPage] max_count = 1 - content_panels = BasePage.content_panels + BaseContentMixin.content_panels - search_fields = BasePage.search_fields + BaseContentMixin.search_fields - @cached_property def table_of_contents(self) -> list[TocEntry]: return [ @@ -160,13 +147,10 @@ class BlogCollectionListPage(BaseContentMixin, BasePage): # type: ignore[misc] return context -class BlogCollectionPage(BaseContentMixin, BasePage): # type: ignore[misc] +class BlogCollectionPage(BaseContentPage): parent_page_types = [BlogListPage] subpage_types = [BlogPostPage] - content_panels = BasePage.content_panels + BaseContentMixin.content_panels - search_fields = BasePage.search_fields + BaseContentMixin.search_fields - @cached_property def table_of_contents(self) -> list[TocEntry]: return [ diff --git a/website/common/models.py b/website/common/models.py index e188ef3..3a339ca 100644 --- a/website/common/models.py +++ b/website/common/models.py @@ -37,7 +37,7 @@ class BasePage(Page): return self.get_ancestors().exclude(depth__lte=2) -class BaseContentMixin(models.Model): +class BaseContentPage(BasePage): subtitle = models.CharField(max_length=255, blank=True) hero_image = models.ForeignKey( 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) - content_panels = [ + content_panels = BasePage.content_panels + [ FieldPanel("subtitle"), FieldPanel("hero_image"), FieldPanel("hero_unsplash_photo", widget=UnsplashPhotoChooser), FieldPanel("body"), ] - search_fields = [ + search_fields = BasePage.search_fields + [ index.SearchField("body"), index.SearchField("subtitle"), ] @@ -106,16 +106,11 @@ class BaseContentMixin(models.Model): return None -class ContentPage(BasePage, BaseContentMixin): # type: ignore[misc] +class ContentPage(BaseContentPage): 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] - content_panels = BasePage.content_panels + BaseContentMixin.content_panels - search_fields = BasePage.search_fields + BaseContentMixin.search_fields - +class ListingPage(BaseContentPage): def get_context(self, request: HttpRequest) -> dict: context = super().get_context(request) context["child_pages"] = ( diff --git a/website/contact/models.py b/website/contact/models.py index 9f715f4..9be1cc9 100644 --- a/website/contact/models.py +++ b/website/contact/models.py @@ -6,7 +6,7 @@ from wagtail.admin.panels import FieldPanel from wagtail.search import index 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 @@ -36,11 +36,9 @@ class OnlineAccount(models.Model, index.Indexed): return slugify(self.name) -class ContactPage(BaseContentMixin, BasePage): # type: ignore[misc] +class ContactPage(BaseContentPage): max_count = 1 subpage_types: list = [] - content_panels = BasePage.content_panels + BaseContentMixin.content_panels - search_fields = BasePage.search_fields + BaseContentMixin.search_fields @cached_property def reading_time(self) -> int: diff --git a/website/search/models.py b/website/search/models.py index 94cd0bd..6363b94 100644 --- a/website/search/models.py +++ b/website/search/models.py @@ -10,19 +10,17 @@ from wagtail.models import Page from wagtail.query import PageQuerySet 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.home.models import HomePage from .serializers import MIN_SEARCH_LENGTH, SearchParamsSerializer -class SearchPage(BaseContentMixin, RoutablePageMixin, BasePage): # type: ignore[misc] +class SearchPage(RoutablePageMixin, BaseContentPage): max_count = 1 subpage_types: list = [] parent_page_types = ["home.HomePage"] - content_panels = BasePage.content_panels + BaseContentMixin.content_panels - search_fields = BasePage.search_fields + BaseContentMixin.search_fields PAGE_SIZE = 15 @cached_property