Parcourir la source

Subclass custom HeadlessMixin in all page models

Sage Abdullah il y a 2 mois
Parent
commit
85963b5a43

+ 6 - 4
bakerydemo/base/models.py

@@ -36,6 +36,8 @@ from wagtail.models import (
 )
 from wagtail.search import index
 
+from bakerydemo.headless import CustomHeadlessMixin
+
 from .blocks import BaseStreamBlock
 
 # Allow filtering by collection
@@ -226,7 +228,7 @@ class FooterText(
         verbose_name_plural = "footer text"
 
 
-class StandardPage(Page):
+class StandardPage(CustomHeadlessMixin, Page):
     """
     A generic content page. On this demo site we use it for an about page but
     it could be used for any type of page content that only needs a title,
@@ -258,7 +260,7 @@ class StandardPage(Page):
     ]
 
 
-class HomePage(Page):
+class HomePage(CustomHeadlessMixin, Page):
     """
     The Home Page. This looks slightly more complicated than it is. You can
     see if you visit your site and edit the homepage that it is split between
@@ -438,7 +440,7 @@ class HomePage(Page):
         return self.title
 
 
-class GalleryPage(Page):
+class GalleryPage(CustomHeadlessMixin, Page):
     """
     This is a page to list locations from the selected Collection. We use a Q
     object to list any Collection created (/admin/collections/) even if they
@@ -499,7 +501,7 @@ class FormField(AbstractFormField):
     page = ParentalKey("FormPage", related_name="form_fields", on_delete=models.CASCADE)
 
 
-class FormPage(AbstractEmailForm):
+class FormPage(CustomHeadlessMixin, AbstractEmailForm):
     image = models.ForeignKey(
         "wagtailimages.Image",
         null=True,

+ 3 - 2
bakerydemo/blog/models.py

@@ -12,6 +12,7 @@ from wagtail.models import Orderable, Page
 from wagtail.search import index
 
 from bakerydemo.base.blocks import BaseStreamBlock
+from bakerydemo.headless import CustomHeadlessMixin
 
 
 class BlogPersonRelationship(Orderable, models.Model):
@@ -49,7 +50,7 @@ class BlogPageTag(TaggedItemBase):
     )
 
 
-class BlogPage(Page):
+class BlogPage(CustomHeadlessMixin, Page):
     """
     A Blog Page
 
@@ -142,7 +143,7 @@ class BlogPage(Page):
     subpage_types = []
 
 
-class BlogIndexPage(RoutablePageMixin, Page):
+class BlogIndexPage(CustomHeadlessMixin, RoutablePageMixin, Page):
     """
     Index page for blogs.
     We need to alter the page model's context to return the child page objects,

+ 3 - 2
bakerydemo/breads/models.py

@@ -10,6 +10,7 @@ from wagtail.models import DraftStateMixin, Page, RevisionMixin
 from wagtail.search import index
 
 from bakerydemo.base.blocks import BaseStreamBlock
+from bakerydemo.headless import CustomHeadlessMixin
 
 
 class Country(models.Model):
@@ -110,7 +111,7 @@ class BreadType(RevisionMixin, models.Model):
         verbose_name_plural = "bread types"
 
 
-class BreadPage(Page):
+class BreadPage(CustomHeadlessMixin, Page):
     """
     Detail view for a specific bread
     """
@@ -182,7 +183,7 @@ class BreadPage(Page):
     ]
 
 
-class BreadsIndexPage(Page):
+class BreadsIndexPage(CustomHeadlessMixin, Page):
     """
     Index page for breads.
 

+ 13 - 0
bakerydemo/headless.py

@@ -0,0 +1,13 @@
+from wagtail_headless_preview.models import HeadlessMixin
+
+
+class CustomHeadlessMixin(HeadlessMixin):
+    def get_client_root_url(self, request):
+        """
+        Use a dedicated API endpoint for drafts.
+        By default, the method uses the root URL of the client site.
+        """
+        root_url = super().get_client_root_url(request)
+        if getattr(request, "is_preview", False):
+            return f"{root_url}/api/draft"
+        return root_url

+ 3 - 2
bakerydemo/locations/models.py

@@ -11,6 +11,7 @@ from wagtail.models import Orderable, Page
 from wagtail.search import index
 
 from bakerydemo.base.blocks import BaseStreamBlock
+from bakerydemo.headless import CustomHeadlessMixin
 from bakerydemo.locations.choices import DAY_CHOICES
 
 
@@ -71,7 +72,7 @@ class LocationOperatingHours(Orderable, OperatingHours):
     )
 
 
-class LocationsIndexPage(Page):
+class LocationsIndexPage(CustomHeadlessMixin, Page):
     """
     A Page model that creates an index page (a listview)
     """
@@ -116,7 +117,7 @@ class LocationsIndexPage(Page):
     ]
 
 
-class LocationPage(Page):
+class LocationPage(CustomHeadlessMixin, Page):
     """
     Detail for a specific bakery location.
     """

+ 3 - 2
bakerydemo/recipes/models.py

@@ -12,6 +12,7 @@ from wagtail.models import Orderable, Page
 from wagtail.search import index
 
 from bakerydemo.base.blocks import BaseStreamBlock
+from bakerydemo.headless import CustomHeadlessMixin
 
 from .blocks import RecipeStreamBlock
 
@@ -43,7 +44,7 @@ class RecipePersonRelationship(Orderable, models.Model):
     ]
 
 
-class RecipePage(Page):
+class RecipePage(CustomHeadlessMixin, Page):
     """
     Recipe pages are more complex than blog pages, demonstrating more advanced StreamField patterns.
     """
@@ -147,7 +148,7 @@ class RecipePage(Page):
     subpage_types = []
 
 
-class RecipeIndexPage(Page):
+class RecipeIndexPage(CustomHeadlessMixin, Page):
     """
     Index page for recipe.
     We need to alter the page model's context to return the child page objects,