From b155ec2e9b9db17c68936f2173cea81866eb51fb Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 12 Jul 2022 22:45:50 +0100 Subject: [PATCH] Allow unsplash images to be used as hero images --- website/blog/models.py | 1 + website/common/models.py | 13 ++++++++++--- website/common/templates/common/hero.html | 6 ++---- website/common/templates/common/listing-item.html | 6 +++--- website/contrib/unsplash/models.py | 3 +++ 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/website/blog/models.py b/website/blog/models.py index dafd381..32c8c19 100644 --- a/website/blog/models.py +++ b/website/blog/models.py @@ -53,6 +53,7 @@ class BlogListPage(BaseContentMixin, BasePage): # type: ignore[misc] self.get_children() .live() .select_related("hero_image") + .select_related("hero_unsplash_photo") .prefetch_related("tags") .order_by("-date") ) diff --git a/website/common/models.py b/website/common/models.py index 722e005..51914c1 100644 --- a/website/common/models.py +++ b/website/common/models.py @@ -1,5 +1,5 @@ import math -from typing import Any +from typing import Any, Optional from django.db import models from django.http.request import HttpRequest @@ -7,6 +7,7 @@ from django.utils.functional import cached_property, classproperty from wagtail.admin.panels import FieldPanel from wagtail.fields import StreamField from wagtail.images import get_image_model_string +from wagtail.images.views.serve import generate_image_url from wagtail.models import Page from website.common.utils import count_words @@ -19,8 +20,6 @@ from .utils import TocEntry, extract_text, get_table_of_contents, truncate_strin class BasePage(Page): show_in_menus_default = True - HERO_IMAGE_SIZE = "width-1200" - class Meta: abstract = True @@ -86,6 +85,14 @@ class BaseContentMixin(models.Model): def plain_text(self) -> str: return extract_text(self.content_html) + @cached_property + def hero_image_url(self) -> Optional[str]: + if self.hero_unsplash_photo_id is not None: + return self.hero_unsplash_photo.get_hero_image_url() # type: ignore + elif self.hero_image_id is not None: + return generate_image_url(self.hero_image, "width-1200") + return None + class ContentPage(BasePage, BaseContentMixin): # type: ignore[misc] subpage_types: list[Any] = [] diff --git a/website/common/templates/common/hero.html b/website/common/templates/common/hero.html index 1ea3fb5..1891bbd 100644 --- a/website/common/templates/common/hero.html +++ b/website/common/templates/common/hero.html @@ -1,7 +1,5 @@ -{% load wagtailimages_tags %} - -{% if page.hero_image %} - +{% if page.hero_image_url %} + {% endif %}
diff --git a/website/common/templates/common/listing-item.html b/website/common/templates/common/listing-item.html index adc099c..89db290 100644 --- a/website/common/templates/common/listing-item.html +++ b/website/common/templates/common/listing-item.html @@ -1,11 +1,11 @@ -{% load wagtailcore_tags wagtailimages_tags %} +{% load wagtailcore_tags %}
- {% if page.hero_image %} + {% if page.hero_image_url %} - + {% endif %}
diff --git a/website/contrib/unsplash/models.py b/website/contrib/unsplash/models.py index 73587f0..c7f5ab9 100644 --- a/website/contrib/unsplash/models.py +++ b/website/contrib/unsplash/models.py @@ -8,5 +8,8 @@ class UnsplashPhoto(models.Model): def get_description(self) -> str: return self.data["description"] + def get_hero_image_url(self) -> str: + return self.data["urls"]["regular"] + def get_thumbnail_url(self) -> str: return self.data["urls"]["thumb"]