Skip to content

Commit

Permalink
Add SimplePageSerializer to show authors in blog posts API (#1732)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosgood authored Oct 22, 2024
1 parent c3038ff commit 2e13afe
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
11 changes: 8 additions & 3 deletions etna/blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,14 @@ class BlogPostPage(
AuthorPageMixin.get_authors_inlinepanel(),
]

default_api_fields = BasePageWithRequiredIntro.default_api_fields + [
APIField("published_date", serializer=DateTimeSerializer()),
]
default_api_fields = (
BasePageWithRequiredIntro.default_api_fields
+ AuthorPageMixin.default_api_fields
+ [
APIField("published_date", serializer=DateTimeSerializer()),
APIField("last_published_at"),
]
)

api_fields = (
BasePageWithRequiredIntro.api_fields
Expand Down
3 changes: 2 additions & 1 deletion etna/core/serializers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from .date import DateTimeSerializer
from .images import DetailedImageSerializer, HighlightImageSerializer, ImageSerializer
from .pages import DefaultPageSerializer
from .pages import DefaultPageSerializer, SimplePageSerializer
from .richtext import RichTextSerializer
from .tags import MourningSerializer, TaggableSerializer

__all__ = [
"DateTimeSerializer",
"DefaultPageSerializer",
"SimplePageSerializer",
"DetailedImageSerializer",
"HighlightImageSerializer",
"ImageSerializer",
Expand Down
11 changes: 11 additions & 0 deletions etna/core/serializers/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,14 @@ def __init__(self, instance=None, data=empty, required_api_fields=[], **kwargs):

def to_representation(self, instance):
return get_api_data(instance, required_api_fields=self.required_api_fields)


class SimplePageSerializer(serializers.Serializer):
def to_representation(self, instance):
return {
"id": instance.id,
"title": instance.title,
"teaser_text": instance.teaser_text,
"url": instance.url,
"full_url": instance.full_url,
}
8 changes: 8 additions & 0 deletions etna/people/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
DefaultPageSerializer,
ImageSerializer,
RichTextSerializer,
SimplePageSerializer,
)

from .blocks import ResearchSummaryStreamBlock
Expand Down Expand Up @@ -286,6 +287,13 @@ def author_names(self):
if self.authors:
return ", ".join([author.title for author in self.authors])

default_api_fields = [
APIField(
"authors",
serializer=SimplePageSerializer(many=True),
)
]

api_fields = [
APIField(
"authors",
Expand Down

0 comments on commit 2e13afe

Please sign in to comment.