diff --git a/website/common/models.py b/website/common/models.py index 3a339ca..0d79f19 100644 --- a/website/common/models.py +++ b/website/common/models.py @@ -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 diff --git a/website/common/templates/common/listing-item.html b/website/common/templates/common/listing-item.html index 89db290..2c05a59 100644 --- a/website/common/templates/common/listing-item.html +++ b/website/common/templates/common/listing-item.html @@ -3,9 +3,9 @@
- {% if page.hero_image_url %} + {% if page.list_image_url %} - + {% endif %}
diff --git a/website/common/tests/test_pages.py b/website/common/tests/test_pages.py index e8407df..29382bc 100644 --- a/website/common/tests/test_pages.py +++ b/website/common/tests/test_pages.py @@ -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) diff --git a/website/contrib/unsplash/models.py b/website/contrib/unsplash/models.py index e2927c4..4a3708b 100644 --- a/website/contrib/unsplash/models.py +++ b/website/contrib/unsplash/models.py @@ -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"]