0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-24 01:57:32 +01:00

Switch cases in ImageBlock.bulk_to_python so that the ImageChooserBlock logic is the special case rather than the default

This commit is contained in:
Matt Westcott 2024-11-14 18:52:47 +00:00
parent 17378e06c8
commit 0dd9b7dcb1

View File

@ -148,16 +148,9 @@ class ImageBlock(StructBlock):
def bulk_to_python(self, values):
values = list(values)
# The normal case: `values` is a list of dicts. This is the normal representation of this block.
if all(isinstance(value, dict) for value in values):
# `values` is a list of dicts containing `image`, `decorative` and `alt_text` keys
struct_values = super().bulk_to_python(values)
# Else we are in a fallback for backwards compatibility with ImageChooserBlock
else:
# `values` might be a list of image IDs (as we might encounter if an ImageChooserBlock has been
# changed to an ImageBlock with no data migration)
# Or it could be a [None] if we are coming from an empty ImageChooserBlock
if values and all(value is None or isinstance(value, int) for value in values):
# `values` looks like a list of image IDs and/or None values (as we might encounter
# if an ImageChooserBlock has been changed to an ImageBlock with no data migration)
image_values = self.child_blocks["image"].bulk_to_python(values)
struct_values = [
@ -169,6 +162,12 @@ class ImageBlock(StructBlock):
for image in image_values
]
else:
# Treat `values` as the standard ImageBlock representation - a (possibly empty) list of
# dicts containing `image`, `decorative` and `alt_text` keys to be handled by the
# StructBlock superclass
struct_values = super().bulk_to_python(values)
return [
self._struct_value_to_image(struct_value) for struct_value in struct_values
]