Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BlogPage custom_type_label #1782

Merged
merged 12 commits into from
Jan 29, 2025
Merged
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