Skip to content

Commit

Permalink
BlogPage custom_type_label (#1782)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Hosgood <andrew.hosgood@nationalarchives.gov.uk>
  • Loading branch information
jamesbiggs and ahosgood authored Jan 29, 2025
1 parent c1828fb commit 816525c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
4 changes: 3 additions & 1 deletion etna/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,9 @@ def top_level_blogs_list_view(self, request):
queryset = queryset.not_descendant_of(blog, inclusive=False)
blog_posts = BlogPostPage.objects.all().live().descendant_of(blog).count()
blog_post_counts[blog.id] = blog_posts
serializer = DefaultPageSerializer(queryset, many=True)
serializer = DefaultPageSerializer(
queryset, required_api_fields=["custom_type_label"], many=True
)
blogs = sorted(serializer.data, key=lambda x: x["title"])
blogs = [blog | {"posts": blog_post_counts[blog["id"]]} for blog in blogs]
top_level_queryset = BlogIndexPage.objects.all().live()
Expand Down
18 changes: 18 additions & 0 deletions etna/blog/migrations/0011_blogpage_custom_type_label.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.1.4 on 2025-01-28 10:52

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blog', '0010_alter_blogpostpage_body'),
]

operations = [
migrations.AddField(
model_name='blogpage',
name='custom_type_label',
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'),
),
]
29 changes: 22 additions & 7 deletions etna/blog/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.db import models
from django.utils.functional import cached_property
from wagtail.admin.panels import FieldPanel
from wagtail.api import APIField
Expand Down Expand Up @@ -36,6 +37,14 @@ class BlogPage(HeroImageMixin, BasePageWithRequiredIntro):
blogs within this blog.
"""

custom_type_label = models.CharField(
max_length=20,
blank=True,
null=True,
help_text="Override the chip for child blog posts. If left blank, the chip will be the title of the blog.",
verbose_name="Chip override",
)

parent_page_types = [
"blog.BlogIndexPage",
"blog.BlogPage",
Expand All @@ -50,9 +59,15 @@ class BlogPage(HeroImageMixin, BasePageWithRequiredIntro):
BasePageWithRequiredIntro.content_panels + HeroImageMixin.content_panels
)

promote_panels = BasePageWithRequiredIntro.promote_panels
promote_panels = BasePageWithRequiredIntro.promote_panels + [
FieldPanel("custom_type_label"),
]

api_fields = BasePageWithRequiredIntro.api_fields + HeroImageMixin.api_fields
api_fields = (
BasePageWithRequiredIntro.api_fields
+ HeroImageMixin.api_fields
+ [APIField("custom_type_label")]
)


class BlogPostPage(
Expand Down Expand Up @@ -80,12 +95,12 @@ def type_label(cls) -> str:
Overrides the type_label method from BasePage, to return the correct
type label for the blog post page.
"""
parent = cls.get_parent()
if not parent:
top_level = cls.get_ancestors().type(BlogPage).first().specific
if not top_level:
return "Blog post"
while parent.get_parent().specific_class == BlogPage:
parent = parent.get_parent()
return parent.title
if top_level.custom_type_label:
return top_level.custom_type_label
return top_level.title

content_panels = (
BasePageWithRequiredIntro.content_panels
Expand Down

0 comments on commit 816525c

Please sign in to comment.