Skip to content

Commit

Permalink
Add social fields to API meta fields and add serializer for social im…
Browse files Browse the repository at this point in the history
…age (#166)

Co-authored-by: Thibaud Colas <[email protected]>
  • Loading branch information
Tijani-Dia and thibaudcolas authored Feb 25, 2024
1 parent b448445 commit 96fc35a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
SECRET_KEY: fake
DJANGO_SETTINGS_MODULE: wagtailio.settings.dev
DATABASE_URL: postgres://wagtailorg:wagtailorg@db:5432/wagtailorg
PRIMARY_HOST: http://localhost:8000
PRIMARY_HOST: localhost:8000
# The env vars get picked up by postgresql client tools (psql, pg_dump, etc)
PGHOST: db
PGUSER: wagtailorg
Expand Down
12 changes: 12 additions & 0 deletions wagtailio/areweheadlessyet/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from wagtail.images.api.fields import ImageRenditionField
from wagtail.images.models import SourceImageIOError


class ImageRenditionURLField(ImageRenditionField):
"""A field that serialises a rendition's url."""

def to_representation(self, image):
try:
return image.get_rendition(self.filter_spec).full_url
except SourceImageIOError:
return None
9 changes: 9 additions & 0 deletions wagtailio/areweheadlessyet/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from wagtail.snippets.models import register_snippet

from wagtailio.areweheadlessyet.blocks import HomePageBlock, TopicPageBlock
from wagtailio.areweheadlessyet.fields import ImageRenditionURLField
from wagtailio.utils.models import CrossPageMixin, SocialMediaMixin


Expand Down Expand Up @@ -58,6 +59,14 @@ class AreWeHeadlessYetHomePage(Page, SocialMediaMixin, CrossPageMixin):
APIField("body"),
]

api_meta_fields = [
APIField("social_text"),
APIField(
"social_image_url",
serializer=ImageRenditionURLField("fill-100x100", source="social_image"),
),
]

promote_panels = (
Page.promote_panels + SocialMediaMixin.panels + CrossPageMixin.panels
)
Expand Down

0 comments on commit 96fc35a

Please sign in to comment.