Skip to content

Commit

Permalink
#121 get_context of image block sometimes receives an image_pk and so…
Browse files Browse the repository at this point in the history
…metimes an image - dunno why this happens, add a test to make sure both inputs work 🤦‍
  • Loading branch information
ephes committed Mar 14, 2024
1 parent b8362a2 commit 242bf1a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cast/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class CastImageChooserBlock(ImageChooserBlock):
def get_image_and_renditions(self, image_id, context: dict) -> tuple[Image, dict[str, Rendition]]:
post_data: PostData | None = context.get("post_data")
if post_data is None:
print("image id? ", image_id)
image = super().to_python(image_id)
image_renditions = context.get("renditions_for_posts", {}).get(image.pk, [])
else:
Expand All @@ -109,9 +110,15 @@ def get_image_and_renditions(self, image_id, context: dict) -> tuple[Image, dict
fetched_renditions = {r.filter_spec: r for r in image_renditions}
return image, fetched_renditions

def get_context(self, image_pk: int, parent_context: dict | None = None) -> dict:
def get_context(self, image_or_pk: int | Image, parent_context: dict | None = None) -> dict:
if parent_context is None:
parent_context = {}
if isinstance(image_or_pk, Image):
# FIXME: dunno why this is here :/ 2024-03-14 Jochen
image = image_or_pk
image_pk = image.pk
else:
image_pk = image_or_pk
image, fetched_renditions = self.get_image_and_renditions(image_pk, parent_context)
images_for_slots = get_srcset_images_for_slots(image, "regular", fetched_renditions=fetched_renditions)
[image.regular] = images_for_slots.values()
Expand Down
10 changes: 10 additions & 0 deletions tests/blocks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,16 @@ def test_image_chooser_block_get_context_parent_context_none(image):
assert "value" in context


@pytest.mark.django_db
def test_image_chooser_block_get_context_image_or_pk(image):
"""Make sure get_context handles both an image or an image pk."""
cicb = CastImageChooserBlock()
context = cicb.get_context(image, parent_context=None)
assert context["value"] == image
context = cicb.get_context(image.pk, parent_context=None)
assert context["value"] == image


def test_gallery_block_get_context_parent_context_none():
"""Just make sure parent context is set to {} if it is None."""
cb = GalleryBlock(ImageChooserBlock())
Expand Down

0 comments on commit 242bf1a

Please sign in to comment.