Skip to content

Commit 816525c

Browse files
jamesbiggsahosgood
andauthored
BlogPage custom_type_label (#1782)
Co-authored-by: Andrew Hosgood <[email protected]>
1 parent c1828fb commit 816525c

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

etna/api/urls.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,9 @@ def top_level_blogs_list_view(self, request):
309309
queryset = queryset.not_descendant_of(blog, inclusive=False)
310310
blog_posts = BlogPostPage.objects.all().live().descendant_of(blog).count()
311311
blog_post_counts[blog.id] = blog_posts
312-
serializer = DefaultPageSerializer(queryset, many=True)
312+
serializer = DefaultPageSerializer(
313+
queryset, required_api_fields=["custom_type_label"], many=True
314+
)
313315
blogs = sorted(serializer.data, key=lambda x: x["title"])
314316
blogs = [blog | {"posts": blog_post_counts[blog["id"]]} for blog in blogs]
315317
top_level_queryset = BlogIndexPage.objects.all().live()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 5.1.4 on 2025-01-28 10:52
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('blog', '0010_alter_blogpostpage_body'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='blogpage',
15+
name='custom_type_label',
16+
field=models.CharField(blank=True, help_text='Override the chip for child blog posts. If left blank, the chip will be the title of the blog.', max_length=20, null=True, verbose_name='Chip override'),
17+
),
18+
]

etna/blog/models.py

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from django.db import models
12
from django.utils.functional import cached_property
23
from wagtail.admin.panels import FieldPanel
34
from wagtail.api import APIField
@@ -36,6 +37,14 @@ class BlogPage(HeroImageMixin, BasePageWithRequiredIntro):
3637
blogs within this blog.
3738
"""
3839

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+
3948
parent_page_types = [
4049
"blog.BlogIndexPage",
4150
"blog.BlogPage",
@@ -50,9 +59,15 @@ class BlogPage(HeroImageMixin, BasePageWithRequiredIntro):
5059
BasePageWithRequiredIntro.content_panels + HeroImageMixin.content_panels
5160
)
5261

53-
promote_panels = BasePageWithRequiredIntro.promote_panels
62+
promote_panels = BasePageWithRequiredIntro.promote_panels + [
63+
FieldPanel("custom_type_label"),
64+
]
5465

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+
)
5671

5772

5873
class BlogPostPage(
@@ -80,12 +95,12 @@ def type_label(cls) -> str:
8095
Overrides the type_label method from BasePage, to return the correct
8196
type label for the blog post page.
8297
"""
83-
parent = cls.get_parent()
84-
if not parent:
98+
top_level = cls.get_ancestors().type(BlogPage).first().specific
99+
if not top_level:
85100
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
89104

90105
content_panels = (
91106
BasePageWithRequiredIntro.content_panels

0 commit comments

Comments
 (0)