Skip to content

Commit

Permalink
Redo ExternalAuthor serialisation (added image)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbiggs committed Oct 23, 2024
1 parent 2228431 commit 115ae95
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions etna/people/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from wagtail.images import get_image_model_string
from wagtail.models import Page

from rest_framework import serializers

from etna.core.models import BasePage
from etna.core.serializers import (
DefaultPageSerializer,
Expand Down Expand Up @@ -309,6 +311,10 @@ class ExternalAuthorTag(models.Model):
added to pages via their name, a description, and an image.
"""

@cached_property
def name(self):
return f"{self.first_name} {self.last_name}"

page = ParentalKey(
Page, on_delete=models.CASCADE, related_name="external_author_tags"
)
Expand All @@ -324,6 +330,20 @@ class ExternalAuthorTag(models.Model):
)


class ExternalAuthorSerializer(serializers.ModelSerializer):
image = ImageSerializer(rendition_size="fill-128x128")

class Meta:
model = ExternalAuthorTag
fields = (
"first_name",
"last_name",
"name",
"description",
"image",
)


class ExternalAuthorMixin:
"""
A mixin for pages that allow us to add external authors
Expand All @@ -341,18 +361,8 @@ def get_authors_inlinepanel(cls, max_num=3):

@cached_property
def external_authors(self):
return [
{
"first_name": item.first_name,
"last_name": item.last_name,
"name": f"{item.first_name} {item.last_name}",
"description": item.description,
}
for item in self.external_author_tags.order_by("last_name")
]
return self.external_author_tags.order_by("last_name")

api_fields = [
APIField(
"external_authors",
),
APIField("external_authors", serializer=ExternalAuthorSerializer(many=True)),
]

0 comments on commit 115ae95

Please sign in to comment.