def test_invalid_reverse_in_node_renders_empty_menu(self): menu_pool.register_menu(InvalidURLMenu) context = self.get_context() tpl = Template("{% load menu_tags %}{% show_menu %}") tpl.render(context) nodes = context['nodes'] self.assertEqual(len(nodes), 0)
def setUp(self): menu_pool.discovered = False self.old_menu = menu_pool.menus menu_pool.menus = {} menu_pool.discover_menus() menu_pool.register_menu(StaticMenu) menu_pool.register_menu(StaticMenu2)
def test_menu_for_anonymous(self): menu_pool.register_menu(StaticMenu) context = self.get_context() tpl = Template("{% load menu_tags %}{% show_menu %}") tpl.render(context) nodes = context['nodes'] self.assertEqual(len(nodes), 2) self.assertEqual(nodes[0].title, 'B') self.assertEqual(nodes[1].title, 'C')
def test_menu_for_authenticated(self): menu_pool.register_menu(StaticMenu) user = self.create_user() context = self.get_context(user=user) tpl = Template("{% load menu_tags %}{% show_menu %}") tpl.render(context) nodes = context['nodes'] self.assertEqual(len(nodes), 2) self.assertEqual(nodes[0].title, 'A') self.assertEqual(nodes[1].title, 'C')
def test_node_visibility(self): menu_pool.register_menu(VisibilityMenu) context = self.get_context() tpl = Template("{% load menu_tags %}{% show_menu %}") tpl.render(context) nodes = context['nodes'] self.assertEqual(len(nodes), 4) self.assertEqual(nodes[0].title, 'A') self.assertEqual(nodes[1].title, 'B') self.assertEqual(nodes[2].title, 'C') self.assertEqual(nodes[3].title, 'F')
def test_menu_from_single_namespace(self): menu_pool.register_menu(StaticMenu) menu_pool.register_menu(StaticMenu2) context = self.get_context() tpl = Template('{% load menu_tags %}{% show_menu "workmate/menu/menu.html" "StaticMenu" %}') tpl.render(context) nodes = context['nodes'] self.assertEqual(nodes[0].title, 'A') self.assertEqual(nodes[1].title, 'B') self.assertEqual(nodes[2].title, 'C') self.assertEqual(nodes[3].title, 'D') self.assertEqual(nodes[4].title, 'E')
def test_menu_nodes_from_multiple_menus(self): menu_pool.register_menu(StaticMenu) menu_pool.register_menu(StaticMenu2) context = self.get_context() tpl = Template("{% load menu_tags %}{% show_menu %}") tpl.render(context) nodes = context['nodes'] self.assertEqual(nodes[0].title, 'A') self.assertEqual(nodes[1].title, 'B') self.assertEqual(nodes[2].title, 'C') self.assertEqual(nodes[3].title, 'D') self.assertEqual(nodes[4].title, 'E') self.assertEqual(nodes[5].title, 'F')
def test_appending_nodes_to_existing_namespace(self): menu_pool.register_menu(StaticMenu) menu_pool.register_menu(StaticExtendedMenu) context = self.get_context() tpl = Template("{% load menu_tags %}{% show_menu %}") tpl.render(context) nodes = context['nodes'] self.assertEqual(nodes[0].title, 'A') self.assertEqual(nodes[1].title, 'B') self.assertEqual(nodes[2].title, 'C') self.assertEqual(nodes[2].menu_title, 'Static Menu') self.assertEqual(nodes[3].title, 'C EX') # ensure the appended node has the correct title self.assertEqual(nodes[3].menu_title, 'Static Menu') self.assertEqual(nodes[4].title, 'D') self.assertEqual(nodes[5].title, 'E') self.assertEqual(nodes[6].title, 'F')
def test_menu_nodes(self): menu_pool.register_menu(StaticMenu) context = self.get_context() tpl = Template("{% load menu_tags %}{% show_menu %}") tpl.render(context) nodes = context['nodes'] self.assertEqual(nodes[0].title, 'A') self.assertEqual(nodes[0].get_absolute_url(), '/A/') self.assertEqual(nodes[0].menu_title, 'Static Menu') self.assertEqual(nodes[1].title, 'B') self.assertEqual(nodes[1].get_absolute_url(), '/B/') self.assertEqual(nodes[1].menu_title, 'Static Menu') self.assertEqual(nodes[2].title, 'C') self.assertEqual(nodes[2].get_absolute_url(), '/C/') self.assertEqual(nodes[2].menu_title, 'Static Menu') self.assertEqual(nodes[3].title, 'D') self.assertEqual(nodes[3].get_absolute_url(), '/D/') self.assertEqual(nodes[3].menu_title, 'Static Menu') self.assertEqual(nodes[4].title, 'E') self.assertEqual(nodes[4].get_absolute_url(), '/E/') self.assertEqual(nodes[4].menu_title, 'Static Menu')
# -*- coding: utf-8 -*- from django.utils.translation import ugettext_lazy as _ from workmate.menus.base import Menu, NavigationNode from workmate.menus.menu_pool import menu_pool class WorkmatePluginMenu(Menu): title = _('Plugins') def get_nodes(self, request): node1 = NavigationNode(_('Example Plugin'), '/', 1, icon='plug') nodes = [node1] return nodes menu_pool.register_menu(WorkmatePluginMenu)
node1 = NavigationNode(_('Home'), '/', 1, icon='home', sort_order=1) node2 = NavigationNode(_('Agile'), reverse('agile-index'), 2, icon='repeat', sort_order=2) node3 = NavigationNode(_('Contacts'), reverse('contact-list'), 2, icon='users', sort_order=3) nodes = [node1, node2, node3] return nodes menu_pool.register_menu(MainMenu) class AdminMenu(Menu): title = _('Administration') sort_order = 99 def get_nodes(self, request): node1 = NavigationNode(_('Site Administration'), reverse('admin:index'), 1, icon='cogs', attr={'staff_only': True}) nodes = [node1] return nodes
from workmate.menus.menu_pool import menu_pool class MainMenu(Menu): title = _('Main Menu') sort_order = -1 def get_nodes(self, request): node1 = NavigationNode(_('Home'), '/', 1, icon='home', sort_order=1) node2 = NavigationNode(_('Contacts'), reverse('contact-list'), 2, icon='users', sort_order=2) nodes = [node1, node2] return nodes menu_pool.register_menu(MainMenu) class AdminMenu(Menu): title = _('Administration') sort_order = 99 def get_nodes(self, request): node1 = NavigationNode(_('Site Administration'), reverse('admin:index'), 1, icon='cogs', attr={'staff_only': True}) nodes = [node1] return nodes menu_pool.register_menu(AdminMenu)
def test_duplicate_menu_class_raises_exception(self): with self.assertRaises(NamespaceAlreadyRegistered): menu_pool.register_menu(StaticMenu) menu_pool._expand_menus()
def test_invalid_menu_class_raises_exception(self): with self.assertRaises(AssertionError): menu_pool.register_menu(InvalidMenuClass)