浏览代码

Apply PreviewableMixin with single preview mode to FooterText model

Sage Abdullah 2 年之前
父节点
当前提交
8888011b66
共有 2 个文件被更改,包括 20 次插入3 次删除
  1. 14 2
      bakerydemo/base/models.py
  2. 6 1
      bakerydemo/base/templatetags/navigation_tags.py

+ 14 - 2
bakerydemo/base/models.py

@@ -12,7 +12,13 @@ from wagtail.admin.panels import (
 )
 )
 from wagtail.contrib.forms.models import AbstractEmailForm, AbstractFormField
 from wagtail.contrib.forms.models import AbstractEmailForm, AbstractFormField
 from wagtail.fields import RichTextField, StreamField
 from wagtail.fields import RichTextField, StreamField
-from wagtail.models import Collection, DraftStateMixin, Page, RevisionMixin
+from wagtail.models import (
+    Collection,
+    DraftStateMixin,
+    Page,
+    PreviewableMixin,
+    RevisionMixin,
+)
 from wagtail.search import index
 from wagtail.search import index
 from wagtail.snippets.models import register_snippet
 from wagtail.snippets.models import register_snippet
 
 
@@ -85,7 +91,7 @@ class Person(index.Indexed, ClusterableModel):
 
 
 
 
 @register_snippet
 @register_snippet
-class FooterText(DraftStateMixin, RevisionMixin, models.Model):
+class FooterText(DraftStateMixin, RevisionMixin, PreviewableMixin, models.Model):
     """
     """
     This provides editable text for the site footer. Again it uses the decorator
     This provides editable text for the site footer. Again it uses the decorator
     `register_snippet` to allow it to be accessible via the admin. It is made
     `register_snippet` to allow it to be accessible via the admin. It is made
@@ -103,6 +109,12 @@ class FooterText(DraftStateMixin, RevisionMixin, models.Model):
     def __str__(self):
     def __str__(self):
         return "Footer text"
         return "Footer text"
 
 
+    def get_preview_template(self, request, mode_name):
+        return "base.html"
+
+    def get_preview_context(self, request, mode_name):
+        return {"footer_text": self.body}
+
     class Meta:
     class Meta:
         verbose_name_plural = "Footer Text"
         verbose_name_plural = "Footer Text"
 
 

+ 6 - 1
bakerydemo/base/templatetags/navigation_tags.py

@@ -96,7 +96,12 @@ def breadcrumbs(context):
 
 
 @register.inclusion_tag("base/include/footer_text.html", takes_context=True)
 @register.inclusion_tag("base/include/footer_text.html", takes_context=True)
 def get_footer_text(context):
 def get_footer_text(context):
-    footer_text = ""
+    # Get the footer text from the context if exists,
+    # so that it's possible to pass a custom instance e.g. for previews
+    # or page types that need a custom footer
+    footer_text = context.get("footer_text", "")
+
+    # If the context doesn't have footer_text defined, get one that's live
     if not footer_text:
     if not footer_text:
         instance = FooterText.objects.filter(live=True).first()
         instance = FooterText.objects.filter(live=True).first()
         footer_text = instance.body if instance else ""
         footer_text = instance.body if instance else ""