diff --git a/website/api/serializers.py b/website/api/serializers.py index 2ebe8c5..551ffb5 100644 --- a/website/api/serializers.py +++ b/website/api/serializers.py @@ -26,10 +26,13 @@ class LMOTFYSerializer(serializers.ModelSerializer): def get_full_url(self, page: BlogPostPage) -> str: return page.get_full_url(request=self.context["request"]) - def get_image(self, page: BlogPostPage) -> str: - hero_image_url = page.hero_image_url + def get_image(self, page: BlogPostPage) -> str | None: + image_url = page.get_meta_image_url(request=self.context["request"]) - if isinstance(hero_image_url, str) and hero_image_url[0] == "/": - return self.context["request"].build_absolute_uri(hero_image_url) + if not image_url: + return None - return hero_image_url + if image_url[0] == "/": + return self.context["request"].build_absolute_uri(image_url) + + return image_url diff --git a/website/common/models.py b/website/common/models.py index b958144..078723e 100644 --- a/website/common/models.py +++ b/website/common/models.py @@ -128,27 +128,26 @@ class BaseContentPage(BasePage, MetadataMixin): def plain_text(self) -> str: return extract_text(self.content_html) - @cached_property - def hero_image_url(self) -> Optional[str]: + def hero_url(self, unsplash_size: str, wagtail_image_spec: str) -> Optional[str]: if self.hero_unsplash_photo_id is not None: - return self.hero_unsplash_photo.get_image_urls()["regular"] + return self.hero_unsplash_photo.get_image_urls()[unsplash_size] elif self.hero_image_id is not None: - return generate_image_url(self.hero_image, "width-1200") + return generate_image_url(self.hero_image, wagtail_image_spec) return None + @cached_property + def hero_image_url(self) -> Optional[str]: + return self.hero_url("full", "width-2000") + @cached_property def list_image_url(self) -> Optional[str]: - if self.hero_unsplash_photo_id is not None: - return self.hero_unsplash_photo.get_image_urls()["small"] - elif self.hero_image_id is not None: - return generate_image_url(self.hero_image, "width-400") - return None + return self.hero_url("small", "width-400") def get_meta_url(self) -> str: return self.full_url def get_meta_image_url(self, request: HttpRequest) -> Optional[str]: - return self.hero_image_url + return self.hero_url("regular", "width-1000|format-png") def get_meta_title(self) -> str: return self.html_title