From 02aff40d3c16417fc0f49b044acff10e4150dc8d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Bedna=C5=99=C3=ADk?= <jan.bednarik@gmail.com>
Date: Tue, 9 Feb 2021 13:12:17 +0100
Subject: [PATCH] shared: Refactor SubpageMixin usage

---
 donate/models.py         | 38 ++++++++++++++++++++++----------------
 senat_campaign/models.py | 22 ++++++----------------
 shared/models.py         |  5 ++++-
 uniweb/models.py         | 15 +--------------
 4 files changed, 33 insertions(+), 47 deletions(-)

diff --git a/donate/models.py b/donate/models.py
index cf025d2e..1eec15b5 100644
--- a/donate/models.py
+++ b/donate/models.py
@@ -20,28 +20,13 @@ from wagtail.images.blocks import ImageChooserBlock
 from wagtail.images.edit_handlers import ImageChooserPanel
 from wagtailmetadata.models import MetadataPageMixin
 
+from shared.models import SubpageMixin
 from tuning import help
 
 from .forms import DonateForm
 from .utils import get_donated_amount_from_api
 
 
-class SubpageMixin:
-    """Must be used in class definition before MetadataPageMixin!"""
-
-    # flag for rendering anchor links in menu
-    is_home = False
-
-    @property
-    def root_page(self):
-        if not hasattr(self, "_root_page"):
-            self._root_page = self.get_ancestors().type(DonateHomePage).specific().get()
-        return self._root_page
-
-    def get_meta_image(self):
-        return self.search_image or self.root_page.get_meta_image()
-
-
 class DonateFormMixin(models.Model):
     """Pages which has donate form. Must be in class definition before Page!"""
 
@@ -270,6 +255,9 @@ class DonateRegionIndexPage(Page, SubpageMixin, MetadataPageMixin):
 
     ### OTHERS
 
+    # flag for rendering anchor links in menu
+    is_home = False
+
     class Meta:
         verbose_name = "Přehled krajů"
 
@@ -333,6 +321,9 @@ class DonateRegionPage(
 
     ### OTHERS
 
+    # flag for rendering anchor links in menu
+    is_home = False
+
     class Meta:
         verbose_name = "Kraj"
 
@@ -373,6 +364,9 @@ class DonateProjectIndexPage(Page, SubpageMixin, MetadataPageMixin):
 
     ### OTHERS
 
+    # flag for rendering anchor links in menu
+    is_home = False
+
     class Meta:
         verbose_name = "Přehled projektů"
 
@@ -479,6 +473,9 @@ class DonateProjectPage(
 
     ### OTHERS
 
+    # flag for rendering anchor links in menu
+    is_home = False
+
     class Meta:
         verbose_name = "Projekt"
 
@@ -565,6 +562,9 @@ class DonateTextPage(Page, SubpageMixin, MetadataPageMixin):
 
     ### OTHERS
 
+    # flag for rendering anchor links in menu
+    is_home = False
+
     class Meta:
         verbose_name = "Stránka s textem"
 
@@ -602,6 +602,9 @@ class DonateInfoPage(DonateFormMixin, Page, SubpageMixin, MetadataPageMixin):
 
     ### OTHERS
 
+    # flag for rendering anchor links in menu
+    is_home = False
+
     class Meta:
         verbose_name = "Infostránka s formulářem"
 
@@ -674,6 +677,9 @@ class DonateTargetedDonationsPage(
 
     ### OTHERS
 
+    # flag for rendering anchor links in menu
+    is_home = False
+
     class Meta:
         verbose_name = "Adresné dary"
 
diff --git a/senat_campaign/models.py b/senat_campaign/models.py
index 33bcf2d7..e84ef74d 100644
--- a/senat_campaign/models.py
+++ b/senat_campaign/models.py
@@ -17,6 +17,7 @@ from wagtail.images.edit_handlers import ImageChooserPanel
 from wagtailmetadata.models import MetadataPageMixin
 
 from calendar_utils.models import CalendarMixin
+from shared.models import SubpageMixin
 from tuning import help
 
 HELP_COMBINED_TITLE = (
@@ -25,18 +26,7 @@ HELP_COMBINED_TITLE = (
 )
 
 
-class SubpageMixin:
-    """Must be used in class definition before MetadataPageMixin!"""
-
-    @property
-    def root_page(self):
-        if not hasattr(self, "_root_page"):
-            self._root_page = self.get_parent().specific
-        return self._root_page
-
-    def get_meta_image(self):
-        return self.search_image or self.root_page.get_meta_image()
-
+class MetaMixin:
     def get_meta_title(self):
         if self.seo_title:
             return self.seo_title
@@ -288,7 +278,7 @@ class SenatCampaignHomePage(Page, MetadataPageMixin, CalendarMixin):
         return context
 
 
-class SenatCampaignNewsIndexPage(Page, SubpageMixin, MetadataPageMixin):
+class SenatCampaignNewsIndexPage(Page, SubpageMixin, MetaMixin, MetadataPageMixin):
     ### FIELDS
 
     ### PANELS
@@ -340,7 +330,7 @@ class SenatCampaignNewsIndexPage(Page, SubpageMixin, MetadataPageMixin):
         return context
 
 
-class SenatCampaignNewsPage(Page, SubpageMixin, MetadataPageMixin):
+class SenatCampaignNewsPage(Page, SubpageMixin, MetaMixin, MetadataPageMixin):
     ### FIELDS
 
     date = models.DateField("datum")
@@ -436,7 +426,7 @@ class ProgramBlock(blocks.StructBlock):
         label = "programovĂ˝ bod"
 
 
-class SenatCampaignProgramPage(Page, SubpageMixin, MetadataPageMixin):
+class SenatCampaignProgramPage(Page, SubpageMixin, MetaMixin, MetadataPageMixin):
     ### FIELDS
 
     committee_preference = StreamField(
@@ -484,7 +474,7 @@ class SenatCampaignProgramPage(Page, SubpageMixin, MetadataPageMixin):
         verbose_name = "Program"
 
 
-class SenatCampaignCookiesPage(Page, SubpageMixin, MetadataPageMixin):
+class SenatCampaignCookiesPage(Page, SubpageMixin, MetaMixin, MetadataPageMixin):
     ### FIELDS
 
     body = RichTextField("obsah", blank=True)
diff --git a/shared/models.py b/shared/models.py
index 5eeeef30..b2469220 100644
--- a/shared/models.py
+++ b/shared/models.py
@@ -15,11 +15,14 @@ class SubpageMixin:
     @property
     def root_page(self):
         if not hasattr(self, "_root_page"):
-            # Vypada to hackove ale lze takto pouzit: dle dokumentace get_ancestors
+            # vypada to hackove ale lze takto pouzit: dle dokumentace get_ancestors
             # vraci stranky v poradi od rootu, tedy domovska stranka je druha v poradi
             self._root_page = self.get_ancestors().specific()[1]
         return self._root_page
 
+    def get_meta_image(self):
+        return self.search_image or self.root_page.get_meta_image()
+
 
 class ArticleMixin(models.Model):
     """Common fields for articles"""
diff --git a/uniweb/models.py b/uniweb/models.py
index 7f7427ee..1a31145f 100644
--- a/uniweb/models.py
+++ b/uniweb/models.py
@@ -11,11 +11,11 @@ from wagtail.contrib.table_block.blocks import TableBlock
 from wagtail.core import blocks
 from wagtail.core.fields import StreamField
 from wagtail.core.models import Page
-from wagtail.documents.blocks import DocumentChooserBlock
 from wagtail.images.blocks import ImageChooserBlock
 from wagtail.images.edit_handlers import ImageChooserPanel
 from wagtailmetadata.models import MetadataPageMixin
 
+from shared.models import SubpageMixin
 from tuning import help
 
 from .constants import (
@@ -150,19 +150,6 @@ class MenuItemBlock(blocks.StructBlock):
         label = "stránka"
 
 
-class SubpageMixin:
-    """Must be used in class definition before MetadataPageMixin!"""
-
-    @property
-    def root_page(self):
-        if not hasattr(self, "_root_page"):
-            self._root_page = self.get_ancestors().type(UniwebHomePage).specific().get()
-        return self._root_page
-
-    def get_meta_image(self):
-        return self.search_image or self.root_page.get_meta_image()
-
-
 class UniwebContentMixin(models.Model):
     content = StreamField(
         [
-- 
GitLab