Sfoglia il codice sorgente

Insert default_alt_text when auto-converting from ImageChooserBlock to ImageBlock

Cynthia Kiser 2 mesi fa
parent
commit
3f551f212c

+ 6 - 2
wagtail/images/blocks.py

@@ -140,7 +140,11 @@ class ImageBlock(StructBlock):
         # For backward compatibility with ImageChooserBlock
         if value is None or isinstance(value, int):
             image = self.child_blocks["image"].to_python(value)
-            struct_value = {"image": image, "decorative": False, "alt_text": None}
+            struct_value = {
+                "image": image,
+                "decorative": False,
+                "alt_text": (image.default_alt_text if image else ""),
+            }
         else:
             struct_value = super().to_python(value)
         return self._struct_value_to_image(struct_value)
@@ -157,7 +161,7 @@ class ImageBlock(StructBlock):
                 {
                     "image": image,
                     "decorative": False,
-                    "alt_text": None,
+                    "alt_text": (image.default_alt_text if image else ""),
                 }
                 for image in image_values
             ]

+ 1 - 0
wagtail/images/rich_text/contentstate.py

@@ -1,6 +1,7 @@
 """
 Draftail / contentstate conversion
 """
+
 from draftjs_exporter.dom import DOM
 
 from wagtail.admin.rich_text.converters.contentstate_models import Entity

+ 1 - 0
wagtail/images/rich_text/editor_html.py

@@ -1,6 +1,7 @@
 """
 editor-html conversion for contenteditable editors
 """
+
 from wagtail.admin.rich_text.converters import editor_html
 from wagtail.images import get_image_model
 from wagtail.images.formats import get_image_format

+ 3 - 2
wagtail/images/tests/test_blocks.py

@@ -236,7 +236,7 @@ class TestImageBlock(TestImageChooserBlock):
         value = block.to_python(self.image.id)
 
         self.assertEqual(value.id, self.image.id)
-        self.assertEqual(value.contextual_alt_text, None)
+        self.assertEqual(value.contextual_alt_text, "Test image")  # Defaulted to title
         self.assertFalse(value.decorative)
 
     def test_to_python_with_dict(self):
@@ -267,8 +267,9 @@ class TestImageBlock(TestImageChooserBlock):
 
     def test_bulk_to_python_with_list_of_ints(self):
         block = ImageBlock(required=False)
+        single_image = block.to_python(self.image.id)
         result = block.bulk_to_python([None, self.image.id, self.image.id])
-        self.assertEqual(result, [None, self.image, self.image])
+        self.assertEqual(result, [None, single_image, single_image])
 
     def test_bulk_to_python_with_list_of_dicts(self):
         block = ImageBlock(required=False)