def test_menuitem_is_none_because_not_published(self): MenuItem.add_root(uri='/a/b/', title='found', site=Site.objects.get_current(), is_published=False) req = self.rf.get('/a/b/') self.mw.process_request(req) with self.assertNumQueries(1): with self.assertRaises(AttributeError): self.assertIsNone(req.menuitem.uri)
def test_menuitem_is_published(self): MenuItem.add_root(uri='/a/b/', title='found', site=Site.objects.get_current(), is_published=True) req = self.rf.get('/a/b/') self.mw.process_request(req) with self.assertNumQueries(1): self.assertEqual(req.menuitem.uri, '/a/b/') self.assertEqual(req.menuitem.title, 'found')
def render(self, name, value, attrs=None, choices=()): if value is None: value = '' value = force_text(value) widget_attrs = self.build_attrs(attrs, name=name) options = MenuItem.get_published_annotated_list() return render_to_string('menuhin/widget.html', { 'widget_attrs': widget_attrs, 'options': options, 'value': value, 'empty_label': BLANK_CHOICE_DASH[0][1] })
def get_context(self, context, menu_slug, from_depth, to_depth, template, **kwargs): site = Site.objects.get_current() # allow passing through None or "" ... if not from_depth: from_depth = 0 # allow passing through None or "" ... if not to_depth: to_depth = 100 base = { 'to_depth': to_depth, 'from_depth': from_depth, 'site': site, 'template': template or self.template, } if 'request' in context: base.update(request=context['request']) # try to go by PK, or if there's no invalid characters (eg: /:_ ...) # by menu_slug, otherwise falling back to assuming the input is # request.path or whatevers. fetched_menuitem = self.get_menuitem(context, menu_slug, site) menu_root = fetched_menuitem.obj base.update(query=fetched_menuitem.query) if menu_root is None: return base menu_root.is_active = True depth_filtered_menu = MenuItem.get_published_annotated_list( parent=menu_root, from_depth=from_depth, to_depth=to_depth) if 'request' in context: marked_annotated_menu = marked_annotated_list( request=context['request'], tree=depth_filtered_menu) else: # pragma: no cover logger.info("Cannot calculate position in tree without a request") marked_annotated_menu = depth_filtered_menu base.update(menu_root=menu_root, menu_nodes=marked_annotated_menu) return base
def test_cleaning(self): obj = MenuItem(title='x', uri='/a/b/c/') self.assertEqual(obj.menu_slug, '') obj.clean() self.assertEqual(obj.menu_slug, 'a-b-c')
def test_parsed_unbalanced(self): obj = MenuItem(title='{ yay, :}}}') self.assertEqual('{ yay, :}}}', obj.parsed_title({'a': 4}))
def test_parsed_nothing_to_do(self): obj = MenuItem(title='yay, 3!') self.assertEqual('yay, 3!', obj.parsed_title({'a': 1}))
def test_parsed_template(self): obj = MenuItem(title='yay, {{ a }}!') self.assertEqual('yay, 2!', obj.parsed_title({'a': 2}))
def test_parsed_format(self): obj = MenuItem(title='yay, {a!s}!') self.assertEqual('yay, 1!', obj.parsed_title({'a': 1}))
def test_title_doesnt_need_parsing(self): obj = MenuItem(title='yay, :}}}') self.assertFalse(obj.title_needs_parsing())
def test_balanced_format(self): obj = MenuItem(title='{a}') self.assertTrue(obj.title_has_balanced_format_params())
def test_balanced_template(self): obj = MenuItem(title='{{ a }}') self.assertTrue(obj.title_has_balanced_template_params())
def setUp(self): MenuItem.load_bulk(get_bulk_data()) self.rf = RequestFactory()
def setUp(self): self.rf = RequestFactory() self.mw = RequestTreeMiddleware() MenuItem.load_bulk(get_bulk_data())
def test_get_absolute_url(self): obj = MenuItem(title='x', uri='/a/b/c/') self.assertEqual(obj.get_absolute_url(), '/a/b/c/')
def setUp(self): MenuItem.load_bulk(get_bulk_data())
def test_title_needs_parsing(self): obj = MenuItem(title='{a}') self.assertTrue(obj.title_needs_parsing()) obj2 = MenuItem(title='{{a}}') self.assertTrue(obj2.title_needs_parsing())