Skip to content

Commit

Permalink
🥣 other_exhibits serializer (#141)
Browse files Browse the repository at this point in the history
* Adds custom other_exhibits serializer

* Returns None if missing author
  • Loading branch information
mrharpo authored Dec 20, 2023
1 parent 4bfb46c commit 890d7fc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
4 changes: 2 additions & 2 deletions authors/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class AuthorsOrderable(Orderable):

@property
def name(self):
return self.author.name
return self.author.name if self.author else None

@property
def image(self):
return self.author.image
return self.author.image if self.author else None

api_fields: ClassVar[list[APIField]] = [
APIField('author_id'),
Expand Down
28 changes: 27 additions & 1 deletion exhibits/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.db import models
from modelcluster.fields import ParentalKey
from pydantic import BaseModel
from rest_framework import serializers
from wagtail.admin.panels import FieldPanel, InlinePanel, MultiFieldPanel
from wagtail.api import APIField
from wagtail.fields import RichTextField
Expand All @@ -16,6 +17,8 @@


class ExhibitsOrderable(Orderable):
"""Ordered list of other exhibits related to this exhibit"""

page = ParentalKey('exhibits.ExhibitPage', related_name='other_exhibits', null=True)
exhibit = models.ForeignKey(
'exhibits.ExhibitPage',
Expand Down Expand Up @@ -49,6 +52,27 @@ def authors(self):
]


class OtherExhibitsField(APIField):
"""API field for other_exhibits"""

class Meta:
model = ExhibitsOrderable


class OtherExhibitsSerializer(serializers.ModelSerializer):
"""Serializer for other_exhibits field"""

cover_image = ImageRenditionField('fill-320x100')

class Meta:
model = ExhibitsOrderable
fields: ClassVar[list[str]] = [
'exhibit_id',
'title',
'cover_image',
]


class ImageApiSchema(BaseModel):
url: str
width: int
Expand Down Expand Up @@ -119,5 +143,7 @@ class ExhibitPage(HeadlessMixin, Page):
serializer=ImageRenditionField('fill-480x270', source='hero_image'),
),
APIField('authors'),
APIField('other_exhibits'),
OtherExhibitsField(
'other_exhibits', serializer=OtherExhibitsSerializer(many=True)
),
]

0 comments on commit 890d7fc

Please sign in to comment.