kopia lustrzana https://github.com/wagtail/wagtail
Allow SnippetChooserBlock icon to take precedence over snippet viewset icon
Supersedes #12127 and 12883pull/13118/head
rodzic
36a65fa9be
commit
9033700595
|
@ -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)
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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()
|
||||||
|
|
Ładowanie…
Reference in New Issue