From c88dcbf5e803de071f8c57114b591d8cd3f5c99f Mon Sep 17 00:00:00 2001
From: Jarmil <jarmil.halamicek@seznam.cz>
Date: Wed, 14 Oct 2020 14:21:01 +0200
Subject: [PATCH] REF: univerzalni SubpageMixin

---
 district/models.py | 19 ++++++++-----------
 shared/models.py   | 18 +++++++++++++++---
 2 files changed, 23 insertions(+), 14 deletions(-)

diff --git a/district/models.py b/district/models.py
index 684c5be7..f2b8a682 100644
--- a/district/models.py
+++ b/district/models.py
@@ -7,19 +7,16 @@ from wagtail.core.fields import StreamField
 from wagtail.core.models import Page
 from wagtailmetadata.models import MetadataPageMixin
 
-from shared.models import Article, PersonPage
+from shared.models import Article, SharedSubpageMixin
 
 
 class SubpageMixin:
-    """Must be used in class definition before MetadataPageMixin!"""
+    """Must be used in class definition before MetadataPageMixin!
+        Obsolete, misto nej pouzivat SharedSubpageMixin
+        TODO je pouze v migracich, po odstraneni z nich lze smazat
+    """
 
-    @property
-    def root_page(self):
-        if not hasattr(self, "_root_page"):
-            self._root_page = (
-                self.get_ancestors().type(DistrictHomePage).specific().get()
-            )
-        return self._root_page
+    pass
 
 
 class DistrictHomePage(MetadataPageMixin, Page):
@@ -133,7 +130,7 @@ class DistrictHomePage(MetadataPageMixin, Page):
         return self
 
 
-class DistrictArticles(SubpageMixin, MetadataPageMixin, Page):
+class DistrictArticles(SharedSubpageMixin, MetadataPageMixin, Page):
     ### FIELDS
 
     max_items = models.IntegerField(
@@ -162,7 +159,7 @@ class DistrictArticles(SubpageMixin, MetadataPageMixin, Page):
         return context
 
 
-class DistrictContact(SubpageMixin, MetadataPageMixin, Page):
+class DistrictContact(SharedSubpageMixin, MetadataPageMixin, Page):
     class ContactItemBlock(blocks.StructBlock):
         name = blocks.CharBlock(label="Role")
         person = blocks.PageChooserBlock(
diff --git a/shared/models.py b/shared/models.py
index 8a976821..fcb488ee 100644
--- a/shared/models.py
+++ b/shared/models.py
@@ -9,11 +9,23 @@ from wagtail.images.edit_handlers import ImageChooserPanel
 from wagtailmetadata.models import MetadataPageMixin
 
 
+class SharedSubpageMixin:
+    """Must be used in class definition before MetadataPageMixin!"""
+
+    @property
+    def root_page(self):
+        if not hasattr(self, "_root_page"):
+            # 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
+
+
 class ArticleTag(TaggedItemBase):
     content_object = ParentalKey("shared.Article", on_delete=models.CASCADE)
 
 
-class Article(MetadataPageMixin, Page):
+class Article(SharedSubpageMixin, MetadataPageMixin, Page):
     """ Univerzalni clanek pro vsechny weby """
 
     ### FIELDS
@@ -47,7 +59,7 @@ class Article(MetadataPageMixin, Page):
         verbose_name = "Článek"
 
 
-class PersonPage(MetadataPageMixin, Page):
+class PersonPage(SharedSubpageMixin, MetadataPageMixin, Page):
     """ Stranka Pirata.
         Snaha je zadavat tady minimum informaci o osobe a zbytek vytahovat via API z jinych zdroju,
         neb nechceme dalsi databazi lidi.
@@ -114,7 +126,7 @@ class PersonPage(MetadataPageMixin, Page):
         return context
 
 
-class PeoplePage(MetadataPageMixin, Page):
+class PeoplePage(SharedSubpageMixin, MetadataPageMixin, Page):
     """ Stranka se zobrazenim lidi v dane organizaci
         TOOD: subpagemixin
     """
-- 
GitLab