Add default_alt_text to the state dictionary returned by ImageChooser view and used by AdminImageChooser widget

pull/12755/head
Matt Westcott 2024-12-19 20:22:45 +00:00 zatwierdzone przez Thibaud Colas
rodzic a6b64dc42e
commit 1db66a7ded
5 zmienionych plików z 30 dodań i 1 usunięć

Wyświetl plik

@ -34,6 +34,9 @@ export class ImageChooser extends Chooser {
width: this.previewImage.getAttribute('width'),
height: this.previewImage.getAttribute('height'),
};
state.default_alt_text = this.previewImage.getAttribute(
'data-default-alt-text',
);
}
return state;
}
@ -42,6 +45,10 @@ export class ImageChooser extends Chooser {
super.renderState(newState);
this.previewImage.setAttribute('src', newState.preview.url);
this.previewImage.setAttribute('width', newState.preview.width);
this.previewImage.setAttribute(
'data-default-alt-text',
newState.default_alt_text,
);
}
}

Wyświetl plik

@ -3,5 +3,5 @@
{% block chosen_icon %}
{# Empty alt because the chosen items title is already displayed next to the image. #}
<img class="chooser__image show-transparency" data-chooser-image alt="" decoding="async" height="{{ preview.height|unlocalize }}" src="{{ preview.url }}" width="{{ preview.width|unlocalize }}">
<img class="chooser__image show-transparency" data-chooser-image alt="" data-default-alt-text="{{ default_alt_text }}" decoding="async" height="{{ preview.height|unlocalize }}" src="{{ preview.url }}" width="{{ preview.width|unlocalize }}">
{% endblock chosen_icon %}

Wyświetl plik

@ -1876,6 +1876,7 @@ class TestImageChooserChosenView(WagtailTestUtils, TestCase):
self.image = Image.objects.create(
title="Test image",
file=get_test_image_file(),
description="Test description",
)
def get(self, params={}):
@ -1890,6 +1891,12 @@ class TestImageChooserChosenView(WagtailTestUtils, TestCase):
response_json = json.loads(response.content.decode())
self.assertEqual(response_json["step"], "chosen")
self.assertEqual(response_json["result"]["title"], "Test image")
self.assertEqual(
set(response_json["result"]["preview"].keys()), {"url", "width", "height"}
)
self.assertEqual(
response_json["result"]["default_alt_text"], "Test description"
)
def test_with_multiple_flag(self):
# if 'multiple' is passed as a URL param, the result should be returned as a single-item list
@ -1900,6 +1907,13 @@ class TestImageChooserChosenView(WagtailTestUtils, TestCase):
self.assertEqual(response_json["step"], "chosen")
self.assertEqual(len(response_json["result"]), 1)
self.assertEqual(response_json["result"][0]["title"], "Test image")
self.assertEqual(
set(response_json["result"][0]["preview"].keys()),
{"url", "width", "height"},
)
self.assertEqual(
response_json["result"][0]["default_alt_text"], "Test description"
)
class TestImageChooserChosenMultipleView(WagtailTestUtils, TestCase):
@ -1910,15 +1924,18 @@ class TestImageChooserChosenMultipleView(WagtailTestUtils, TestCase):
self.image1 = Image.objects.create(
title="Test image",
file=get_test_image_file(),
description="Test description",
)
self.image2 = Image.objects.create(
title="Another test image",
file=get_test_image_file(),
description="Another test description",
)
self.image3 = Image.objects.create(
title="Unchosen test image",
file=get_test_image_file(),
description="Unchosen test description",
)
def get(self, params={}):
@ -1940,6 +1957,8 @@ class TestImageChooserChosenMultipleView(WagtailTestUtils, TestCase):
self.assertEqual(len(response_json["result"]), 2)
titles = {item["title"] for item in response_json["result"]}
self.assertEqual(titles, {"Test image", "Another test image"})
alt_texts = {item["default_alt_text"] for item in response_json["result"]}
self.assertEqual(alt_texts, {"Test description", "Another test description"})
class TestImageChooserSelectFormatView(WagtailTestUtils, TestCase):

Wyświetl plik

@ -43,6 +43,7 @@ class ImageChosenResponseMixin(ChosenResponseMixin):
"width": preview_image.width,
"height": preview_image.height,
}
response_data["default_alt_text"] = image.default_alt_text
return response_data

Wyświetl plik

@ -31,11 +31,13 @@ class AdminImageChooser(BaseChooser):
"width": preview_image.width,
"height": preview_image.height,
}
data["default_alt_text"] = instance.default_alt_text
return data
def get_context(self, name, value_data, attrs):
context = super().get_context(name, value_data, attrs)
context["preview"] = value_data.get("preview", {})
context["default_alt_text"] = value_data.get("default_alt_text", "")
return context
@property