Use smaller image URL for listing

This commit is contained in:
Jake Howard 2022-08-19 13:57:25 +01:00
parent 09c4b93245
commit 24d92f4760
Signed by: jake
GPG key ID: 57AFB45680EDD477
4 changed files with 31 additions and 17 deletions

View file

@ -100,11 +100,19 @@ class BaseContentPage(BasePage):
@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()
return self.hero_unsplash_photo.get_image_urls()["regular"]
elif self.hero_image_id is not None:
return generate_image_url(self.hero_image, "width-1200")
return None
@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
class ContentPage(BaseContentPage):
subpage_types: list[Any] = []
@ -114,7 +122,11 @@ class ListingPage(BaseContentPage):
def get_context(self, request: HttpRequest) -> dict:
context = super().get_context(request)
context["child_pages"] = (
self.get_children().live().specific().select_related("hero_image")
self.get_children()
.live()
.specific()
.select_related("hero_image")
.select_related("hero_unsplash_photo")
)
return context

View file

@ -3,9 +3,9 @@
<article class="media listing-item">
<div class="columns">
<figure class="media-left column is-3 image-column">
{% if page.hero_image_url %}
{% if page.list_image_url %}
<a href="{% pageurl page %}" class="image">
<img src="{{ page.hero_image_url }}">
<img src="{{ page.list_image_url }}">
</a>
{% endif %}
</figure>

View file

@ -35,7 +35,7 @@ class ContentPageTestCase(TestCase):
self.assertEqual(response.status_code, 200)
def test_queries(self) -> None:
with self.assertNumQueries(15):
with self.assertNumQueries(16):
self.client.get(self.page.url)
@ -57,13 +57,5 @@ class ListingPageTestCase(TestCase):
self.assertEqual(len(response.context["child_pages"]), 2)
def test_queries(self) -> None:
expected_queries = 18
with self.assertNumQueries(expected_queries):
self.client.get(self.page.url)
# Add another page, and check queries don't change
ContentPageFactory(parent=self.page)
with self.assertNumQueries(expected_queries):
with self.assertNumQueries(19):
self.client.get(self.page.url)

View file

@ -1,7 +1,17 @@
from typing import TypedDict
from django.db import models
from django.utils import timezone
class ImageURLs(TypedDict):
raw: str
full: str
regular: str
small: str
thumb: str
class UnsplashPhoto(models.Model):
unsplash_id = models.CharField(unique=True, max_length=11, db_index=True)
data = models.JSONField()
@ -11,8 +21,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_image_urls(self) -> ImageURLs:
return self.data["urls"]
def get_thumbnail_url(self) -> str:
return self.data["urls"]["thumb"]
return self.get_image_urls()["thumb"]