def test_submenu(self): menu = Menu(register_hook_name="register_menu_item") submenu = Menu(register_hook_name="register_submenu_item") with hooks.register_temporarily([ ( "register_menu_item", menu_item_hook("My lovely submenu", submenu, cls=SubmenuMenuItem), ), ("register_submenu_item", menu_item_hook("Pages", "/pages/")), ]): rendered = menu.render_component(self.request) self.assertIsInstance(rendered, list) self.assertEqual(len(rendered), 1) self.assertIsInstance(rendered[0], sidebar.SubMenuItem) self.assertEqual(rendered[0].name, "my-lovely-submenu") self.assertEqual(rendered[0].label, "My lovely submenu") self.assertListEqual( rendered[0].menu_items, [ sidebar.LinkMenuItem("pages", "Pages", "/pages/"), ], )
def test_admin_only_menuitem(self): menu = Menu(register_hook_name="register_menu_item") with hooks.register_temporarily([ ("register_menu_item", menu_item_hook("Pages", "/pages/")), ( "register_menu_item", menu_item_hook("Secret pages", "/pages/secret/", cls=AdminOnlyMenuItem), ), ]): rendered = menu.render_component(self.request) self.request.user = self.create_user(username="******") rendered_non_admin = menu.render_component(self.request) self.assertListEqual( rendered, [ sidebar.LinkMenuItem("pages", "Pages", "/pages/"), sidebar.LinkMenuItem("secret-pages", "Secret pages", "/pages/secret/"), ], ) self.assertListEqual( rendered_non_admin, [ sidebar.LinkMenuItem("pages", "Pages", "/pages/"), ], )
def test_simple_menu(self): # Note: initialise the menu before registering hooks as this is what happens in reality. # (the real menus are initialised at the module level in admin/menu.py) menu = Menu(register_hook_name='register_menu_item') with hooks.register_temporarily([ ('register_menu_item', menu_item_hook("Pages", '/pages/')), ('register_menu_item', menu_item_hook("Images", '/images/')), ]): rendered = menu.render_component(self.request) self.assertIsInstance(rendered, list) self.assertListEqual(rendered, [ sidebar.LinkMenuItem('pages', "Pages", '/pages/'), sidebar.LinkMenuItem('images', "Images", '/images/'), ])
def test_menu_with_construct_hook(self): menu = Menu(register_hook_name='register_menu_item', construct_hook_name='construct_menu') def remove_images(request, items): items[:] = [item for item in items if not item.name == 'images'] with hooks.register_temporarily([ ('register_menu_item', menu_item_hook("Pages", '/pages/')), ('register_menu_item', menu_item_hook("Images", '/images/')), ('construct_menu', remove_images), ]): rendered = menu.render_component(self.request) self.assertEqual(rendered, [ sidebar.LinkMenuItem('pages', "Pages", '/pages/'), ])
def SubMenu(items): warn( "wagtail.contrib.modeladmin.menus.SubMenu is deprecated. Use wagtail.admin.menus.Menu and " "pass the list of menu items as the 'items' keyword argument instead", category=RemovedInWagtail50Warning, stacklevel=2, ) return Menu(items=items)
def get_menu_item(self): """ Utilised by Wagtail's 'register_menu_item' hook to create a menu for this group with a submenu linking to listing pages for any associated ModelAdmin instances """ if self.modeladmin_instances: submenu = Menu(items=self.get_submenu_items()) return GroupMenuItem(self, self.get_menu_order(), submenu)
def test_menu_with_construct_hook(self): menu = Menu( register_hook_name="register_menu_item", construct_hook_name="construct_menu", ) def remove_images(request, items): items[:] = [item for item in items if not item.name == "images"] with hooks.register_temporarily([ ("register_menu_item", menu_item_hook("Pages", "/pages/")), ("register_menu_item", menu_item_hook("Images", "/images/")), ("construct_menu", remove_images), ]): rendered = menu.render_component(self.request) self.assertEqual( rendered, [ sidebar.LinkMenuItem("pages", "Pages", "/pages/"), ], )
def test_admin_only_menuitem(self): menu = Menu(register_hook_name='register_menu_item') with hooks.register_temporarily([ ('register_menu_item', menu_item_hook("Pages", '/pages/')), ('register_menu_item', menu_item_hook("Secret pages", '/pages/secret/', cls=AdminOnlyMenuItem)), ]): rendered = menu.render_component(self.request) self.request.user = self.create_user(username='******') rendered_non_admin = menu.render_component(self.request) self.assertListEqual(rendered, [ sidebar.LinkMenuItem('pages', "Pages", '/pages/'), sidebar.LinkMenuItem('secret-pages', "Secret pages", '/pages/secret/'), ]) self.assertListEqual(rendered_non_admin, [ sidebar.LinkMenuItem('pages', "Pages", '/pages/'), ])