Allow SnippetChooserBlock icon to take precedence over snippet viewset icon

Supersedes #12127 and 12883
pull/13118/head
Matt Westcott 2025-05-15 18:16:56 +01:00 zatwierdzone przez Sage Abdullah
rodzic 36a65fa9be
commit 9033700595
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: EB1A33CC51CC0217
4 zmienionych plików z 37 dodań i 4 usunięć

Wyświetl plik

@ -15,6 +15,7 @@ Changelog
* Ensure that bulk deletion views respect protected foreign keys (Sage Abdullah) * Ensure that bulk deletion views respect protected foreign keys (Sage Abdullah)
* Add minimum length validation for `RichTextBlock` and `RichTextField` (Alec Baron) * Add minimum length validation for `RichTextBlock` and `RichTextField` (Alec Baron)
* Support `preserve-svg` in Jinja2 image tags (Vishesh Garg) * Support `preserve-svg` in Jinja2 image tags (Vishesh Garg)
* Allow `SnippetChooserBlock`'s `icon` to take precedence over `SnippetViewSet.icon` (Matt Westcott)
* Fix: Handle lazy translation strings as `preview_value` for `RichTextBlock` (Seb Corbin) * Fix: Handle lazy translation strings as `preview_value` for `RichTextBlock` (Seb Corbin)
* Fix: Fix handling of newline-separated choices in form builder when using non-windows newline characters (Baptiste Mispelon) * Fix: Fix handling of newline-separated choices in form builder when using non-windows newline characters (Baptiste Mispelon)
* Fix: Ensure `WAGTAILADMIN_LOGIN_URL` is respected when logging out of the admin (Antoine Rodriguez, Ramon de Jezus) * Fix: Ensure `WAGTAILADMIN_LOGIN_URL` is respected when logging out of the admin (Antoine Rodriguez, Ramon de Jezus)

Wyświetl plik

@ -24,6 +24,7 @@ depth: 1
* Ensure that bulk deletion views respect protected foreign keys (Sage Abdullah) * Ensure that bulk deletion views respect protected foreign keys (Sage Abdullah)
* Add minimum length validation for `RichTextBlock` and `RichTextField` (Alec Baron) * Add minimum length validation for `RichTextBlock` and `RichTextField` (Alec Baron)
* Support `preserve-svg` in Jinja2 image tags (Vishesh Garg) * Support `preserve-svg` in Jinja2 image tags (Vishesh Garg)
* Allow `SnippetChooserBlock`'s `icon` to take precedence over `SnippetViewSet.icon` (Matt Westcott)
### Bug fixes ### Bug fixes

Wyświetl plik

@ -15,6 +15,10 @@ class SnippetChooserBlock(ChooserBlock):
def __init__(self, target_model, **kwargs): def __init__(self, target_model, **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
self._target_model = target_model self._target_model = target_model
self.has_explicit_icon = self.meta.icon is not None
if not self.has_explicit_icon:
# Use the default snippet icon until the model is available
self.set_meta_options({"icon": "snippet"})
@cached_property @cached_property
def target_model(self): def target_model(self):
@ -25,15 +29,18 @@ class SnippetChooserBlock(ChooserBlock):
from wagtail.snippets.widgets import AdminSnippetChooser from wagtail.snippets.widgets import AdminSnippetChooser
try: try:
icon = self.target_model.snippet_viewset.icon snippet_viewset = self.target_model.snippet_viewset
except AttributeError as e: except AttributeError as e:
raise ImproperlyConfigured( raise ImproperlyConfigured(
f"Cannot use SnippetChooserBlock with non-snippet model {self.target_model}" f"Cannot use SnippetChooserBlock with non-snippet model {self.target_model}"
) from e ) from e
# Override the default icon with the icon for the target model if not self.has_explicit_icon:
self.set_meta_options({"icon": icon}) # Pick up the icon from the snippet_viewset
self.set_meta_options({"icon": snippet_viewset.icon})
return AdminSnippetChooser(self.target_model, icon=self.meta.icon) return AdminSnippetChooser(self.target_model, icon=self.meta.icon)
class Meta: class Meta:
icon = "snippet" # If no explicit icon is set on the block, we will use the one from the snippet_viewset
icon = None

Wyświetl plik

@ -156,6 +156,30 @@ class TestSnippetChooserBlockWithIcon(TestCase):
# It should use the icon defined in the FullFeaturedSnippetViewSet # It should use the icon defined in the FullFeaturedSnippetViewSet
self.assertEqual(js_args[2]["icon"], "cog") self.assertEqual(js_args[2]["icon"], "cog")
def test_adapt_with_explicit_icon(self):
block = SnippetChooserBlock(FullFeaturedSnippet, icon="thumbtack")
block.set_name("test_snippetchooserblock")
js_args = FieldBlockAdapter().js_args(block)
self.assertEqual(js_args[2]["icon"], "thumbtack")
def test_adapt_with_explicit_default_icon(self):
block = SnippetChooserBlock(FullFeaturedSnippet, icon="snippet")
block.set_name("test_snippetchooserblock")
js_args = FieldBlockAdapter().js_args(block)
self.assertEqual(js_args[2]["icon"], "snippet")
def test_adapt_with_no_icon_specified_on_block_or_viewset(self):
block = SnippetChooserBlock(Advert)
block.set_name("test_snippetchooserblock")
js_args = FieldBlockAdapter().js_args(block)
self.assertEqual(js_args[2]["icon"], "snippet")
def test_deconstruct(self): def test_deconstruct(self):
block = SnippetChooserBlock(FullFeaturedSnippet, required=False) block = SnippetChooserBlock(FullFeaturedSnippet, required=False)
path, args, kwargs = block.deconstruct() path, args, kwargs = block.deconstruct()