1
+ from django .db import models
1
2
from django .utils .functional import cached_property
2
3
from wagtail .admin .panels import FieldPanel
3
4
from wagtail .api import APIField
@@ -36,6 +37,14 @@ class BlogPage(HeroImageMixin, BasePageWithRequiredIntro):
36
37
blogs within this blog.
37
38
"""
38
39
40
+ custom_type_label = models .CharField (
41
+ max_length = 20 ,
42
+ blank = True ,
43
+ null = True ,
44
+ help_text = "Override the chip for child blog posts. If left blank, the chip will be the title of the blog." ,
45
+ verbose_name = "Chip override" ,
46
+ )
47
+
39
48
parent_page_types = [
40
49
"blog.BlogIndexPage" ,
41
50
"blog.BlogPage" ,
@@ -50,9 +59,15 @@ class BlogPage(HeroImageMixin, BasePageWithRequiredIntro):
50
59
BasePageWithRequiredIntro .content_panels + HeroImageMixin .content_panels
51
60
)
52
61
53
- promote_panels = BasePageWithRequiredIntro .promote_panels
62
+ promote_panels = BasePageWithRequiredIntro .promote_panels + [
63
+ FieldPanel ("custom_type_label" ),
64
+ ]
54
65
55
- api_fields = BasePageWithRequiredIntro .api_fields + HeroImageMixin .api_fields
66
+ api_fields = (
67
+ BasePageWithRequiredIntro .api_fields
68
+ + HeroImageMixin .api_fields
69
+ + [APIField ("custom_type_label" )]
70
+ )
56
71
57
72
58
73
class BlogPostPage (
@@ -80,12 +95,12 @@ def type_label(cls) -> str:
80
95
Overrides the type_label method from BasePage, to return the correct
81
96
type label for the blog post page.
82
97
"""
83
- parent = cls .get_parent ()
84
- if not parent :
98
+ top_level = cls .get_ancestors (). type ( BlogPage ). first (). specific
99
+ if not top_level :
85
100
return "Blog post"
86
- while parent . get_parent (). specific_class == BlogPage :
87
- parent = parent . get_parent ()
88
- return parent .title
101
+ if top_level . custom_type_label :
102
+ return top_level . custom_type_label
103
+ return top_level .title
89
104
90
105
content_panels = (
91
106
BasePageWithRequiredIntro .content_panels
0 commit comments