Add option to set name to MenuItems generated by modeladmin (#8553)

Co-authored-by: Alexander Rogovskyy <A.rogovskyy@intershop.de>
pull/8857/head^2
vupham04 2022-05-16 09:39:30 -04:00 zatwierdzone przez Matt Westcott
rodzic 0d78f8eb56
commit 53e2bdbd0d
7 zmienionych plików z 65 dodań i 1 usunięć

Wyświetl plik

@ -72,6 +72,7 @@ Changelog
* Introduce a new auto-updating preview panel inside the page editor (Sage Abdullah)
* Add support for Twitter and other text-only embeds in Draftail embed previews (Iman Syed, Paarth Agarwal)
* Use new modal dialog component for privacy settings modal (Sage Abdullah)
* Add `menu_item_name` to modify MenuItem's name for ModelAdmin (Alexander Rogovskyy, Vu Pham)
* Fix: Typo in `ResumeWorkflowActionFormatter` message (Stefan Hammer)
* Fix: Throw a meaningful error when saving an image to an unrecognised image format (Christian Franke)
* Fix: Remove extra padding for headers with breadcrumbs on mobile viewport (Steven Steinwand)

Wyświetl plik

@ -612,6 +612,8 @@ Contributors
* Sandeep M A
* Bernd de Ridder
* Stefano Silvestri
* Alexander Rogovskyy
Translators
===========

Wyświetl plik

@ -52,3 +52,11 @@ This will only work for individual `ModelAdmin` classes registered with their ow
**Expected value**: `True` or `False`
If you'd like this model admin to be excluded from the menu, set to `False`.
(modeladmin_menu_item_name)=
## `ModelAdmin.menu_item_name`
**Expected value**: A string or `None`
Passed on as the `name` parameter when initialising the `MenuItem` for this class, becoming the `name` attribute value for that instance.

Wyświetl plik

@ -92,6 +92,7 @@ Wagtails page preview is now available in a side panel within the page editor
* Switch all report workflow, redirects, site settings views to use Wagtails reusable header component (Paarth Agarwal)
* Add support for Twitter and other text-only embeds in Draftail embed previews (Iman Syed, Paarth Agarwal)
* Use new modal dialog component for privacy settings modal (Sage Abdullah)
* Add `menu_item_name` to modify MenuItem's name for ModelAdmin (Alexander Rogovskyy, Vu Pham)
### Bug fixes

Wyświetl plik

@ -23,6 +23,7 @@ class ModelAdminMenuItem(MenuItem):
super().__init__(
label=model_admin.get_menu_label(),
url=url,
name=model_admin.get_menu_item_name(),
classnames=classnames,
icon_name=icon_name,
order=order,
@ -50,6 +51,7 @@ class GroupMenuItem(SubmenuMenuItem):
super().__init__(
label=modeladmingroup.get_menu_label(),
menu=menu,
name=modeladmingroup.get_menu_item_name(),
classnames=classnames,
icon_name=icon_name,
order=order,

Wyświetl plik

@ -104,6 +104,7 @@ class ModelAdmin(WagtailRegisterable):
model = None
menu_label = None
menu_item_name = None
menu_icon = None
menu_order = None
list_display = ("__str__",)
@ -216,6 +217,12 @@ class ModelAdmin(WagtailRegisterable):
"""
return self.menu_label or self.opts.verbose_name_plural.title()
def get_menu_item_name(self):
"""
Returns the name to be used for the menu item.
"""
return self.menu_item_name
def get_menu_icon(self):
"""
Returns the icon to be used for the menu item. The value is prepended
@ -699,6 +706,7 @@ class ModelAdminGroup(WagtailRegisterable):
items = ()
menu_label = None
menu_item_name = None
menu_order = None
menu_icon = None
@ -715,6 +723,9 @@ class ModelAdminGroup(WagtailRegisterable):
def get_menu_label(self):
return self.menu_label or self.get_app_label_from_subitems()
def get_menu_item_name(self):
return self.menu_item_name
def get_app_label_from_subitems(self):
for instance in self.modeladmin_instances:
return instance.opts.app_label.title()

Wyświetl plik

@ -26,7 +26,7 @@ from wagtail.test.modeladmintest.models import (
Token,
TranslatableBook,
)
from wagtail.test.modeladmintest.wagtail_hooks import BookModelAdmin
from wagtail.test.modeladmintest.wagtail_hooks import BookModelAdmin, EventsAdminGroup
from wagtail.test.utils import WagtailTestUtils
@ -1112,3 +1112,42 @@ There are no default tabs on non-Page models so there will be no\
# clean up for future checks
delattr(Publisher, "content_panels")
delattr(Publisher, "edit_handler")
class TestMenuSetting(TestCase, WagtailTestUtils):
fixtures = ["modeladmintest_test.json"]
def setUp(self):
self.login()
def test_default_menu_setting_model_admin(self):
modeladmin = BookModelAdmin()
menu_item = modeladmin.get_menu_item()
self.assertEqual(menu_item.label, "Books")
self.assertEqual(menu_item.name, "books")
def test_custom_menu_setting_model_admin(self):
modeladmin = BookModelAdmin()
modeladmin.menu_label = "Book Model Label"
modeladmin.menu_item_name = "bookitem"
menu_item = modeladmin.get_menu_item()
self.assertEqual(menu_item.label, "Book Model Label")
self.assertEqual(menu_item.name, "bookitem")
def test_default_menu_setting_model_admin_group(self):
modeladmin = EventsAdminGroup()
menu_item = modeladmin.get_menu_item()
self.assertEqual(menu_item.label, "Events")
self.assertEqual(menu_item.name, "events")
def test_custom_menu_setting_model_admin_group(self):
modeladmin = EventsAdminGroup()
modeladmin.menu_label = "Event Model Label"
modeladmin.menu_item_name = "eventitem"
menu_item = modeladmin.get_menu_item()
self.assertEqual(menu_item.label, "Event Model Label")
self.assertEqual(menu_item.name, "eventitem")