def get_context(self, context, levels, root_level, nephews, template): # Django 1.4 doesn't accept 'None' as a tag value and resolve to '' # So we need to force it to None again if not root_level and root_level != 0: root_level = None try: # If there's an exception (500), default context_processors may not # be called. request = context['request'] except KeyError: return {'template': 'menu/empty.html'} nodes = menu_pool.get_nodes(request) children = [] # adjust root_level so we cut before the specified level, not after include_root = False if root_level is not None and root_level > 0: root_level -= 1 elif root_level is not None and root_level == 0: include_root = True for node in nodes: if root_level is None: if node.selected: # if no root_level specified, set it to the selected nodes # level root_level = node.level # is this the ancestor of current selected node at the root # level? is_root_ancestor = (node.ancestor and node.level == root_level) # is a node selected on the root_level specified root_selected = (node.selected and node.level == root_level) if is_root_ancestor or root_selected: cut_after(node, levels, []) children = node.children for child in children: child.parent = None if child.sibling: cut_after(child, nephews, []) # if root_level was 0 we need to give the menu the # entire tree # not just the children if include_root: children = menu_pool.apply_modifiers( [node], request, post_cut=True ) else: children = menu_pool.apply_modifiers( children, request, post_cut=True ) context.update({ 'children': children, 'template': template, 'from_level': 0, 'to_level': 0, 'extra_inactive': 0, 'extra_active': 0 }) return context
def _get_nodes(self, path='/'): node1 = NavigationNode('1', '/1/', 1) node2 = NavigationNode('2', '/2/', 2, 1) node3 = NavigationNode('3', '/3/', 3, 2) node4 = NavigationNode('4', '/4/', 4, 2) node5 = NavigationNode('5', '/5/', 5) nodes = [node1, node2, node3, node4, node5] tree = _build_nodes_inner_for_one_menu([n for n in nodes], "test") request = self.get_request(path) menu_pool.apply_modifiers(tree, request) return tree, nodes
def get_context(self, context, levels, root_level, nephews, template): # Django 1.4 doesn't accept 'None' as a tag value and resolve to '' # So we need to force it to None again if not root_level and root_level != 0: root_level = None try: # If there's an exception (500), default context_processors may not be called. request = context['request'] except KeyError: return {'template': 'menu/empty.html'} nodes = menu_pool.get_nodes(request) children = [] # adjust root_level so we cut before the specified level, not after include_root = False if root_level is not None and root_level > 0: root_level -= 1 elif root_level is not None and root_level == 0: include_root = True for node in nodes: if root_level is None: if node.selected: # if no root_level specified, set it to the selected nodes level root_level = node.level # is this the ancestor of current selected node at the root level? is_root_ancestor = (node.ancestor and node.level == root_level) # is a node selected on the root_level specified root_selected = (node.selected and node.level == root_level) if is_root_ancestor or root_selected: cut_after(node, levels, []) children = node.children for child in children: child.parent = None if child.sibling: cut_after(child, nephews, []) # if root_level was 0 we need to give the menu the entire tree # not just the children if include_root: children = menu_pool.apply_modifiers([node], request, post_cut=True) else: children = menu_pool.apply_modifiers(children, request, post_cut=True) context.update({ 'children': children, 'template': template, 'from_level': 0, 'to_level': 0, 'extra_inactive': 0, 'extra_active': 0 }) return context
def get_context(self, context, levels, template): try: # If there's an exception (500), default context_processors may not be called. request = context['request'] except KeyError: return {'template': 'menu/empty.html'} nodes = menu_pool.get_nodes(request) children = [] for node in nodes: if node.selected: cut_after(node, levels, []) children = node.children for child in children: child.parent = None children = menu_pool.apply_modifiers(children, request, post_cut=True) context.update({ 'children': children, 'template': template, 'from_level': 0, 'to_level': 0, 'extra_inactive': 0, 'extra_active': 0 }) return context
def get_context(self, context, template, namespace, root_id, offset, limit, embody_root): try: # If there's an exception (500), default context_processors may not be called. request = context['request'] except KeyError: return {'template': 'menu/empty.html'} start_level = 0 nodes = menu_pool.get_nodes(request, namespace, root_id) if root_id: # find the root id and cut the nodes id_nodes = menu_pool.get_nodes_by_attribute(nodes, "reverse_id", root_id) if id_nodes: node = id_nodes[0] nodes = node.children for remove_parent in nodes: remove_parent.parent = None start_level = node.level + 1 nodes = flatten(nodes) if embody_root: node.level = start_level nodes.insert(0, node) else: nodes = [] children = cut_levels(nodes, start_level) children = menu_pool.apply_modifiers(children, request, namespace, root_id, post_cut=True) children = children[offset:offset + limit] context.update({'children': children, 'template': template}) return context
def show_sub_menu(context, levels=100, template="menu/sub_menu.html"): """ show the sub menu of the current nav-node. -levels: how many levels deep -temlplate: template used to render the navigation """ try: # If there's an exception (500), default context_processors may not be called. request = context['request'] except KeyError: return {'template': 'menu/empty.html'} nodes = menu_pool.get_nodes(request) children = [] for node in nodes: if node.selected: cut_after(node, levels, []) children = node.children for child in children: child.parent = None children = menu_pool.apply_modifiers(children, request, post_cut=True) context.update({ 'children': children, 'template': template, 'from_level': 0, 'to_level': 0, 'extra_inactive': 0, 'extra_active': 0 }) return context
def show_sub_menu(context, levels=100, template="menu/sub_menu.html"): """ show the sub menu of the current nav-node. -levels: how many levels deep -temlplate: template used to render the navigation """ try: # If there's an exception (500), default context_processors may not be called. request = context['request'] except KeyError: return {'template': 'menu/empty.html'} nodes = menu_pool.get_nodes(request) children = [] for node in nodes: if node.selected: cut_after(node, levels, []) children = node.children for child in children: child.parent = None children = menu_pool.apply_modifiers(children, request, post_cut=True) context.update({'children':children, 'template':template, 'from_level':0, 'to_level':0, 'extra_inactive':0, 'extra_active':0 }) return context
def get_context(self, context, levels, template): try: # If there's an exception (500), default context_processors may not be called. request = context["request"] except KeyError: return {"template": "menu/empty.html"} nodes = menu_pool.get_nodes(request) children = [] for node in nodes: if node.selected: cut_after(node, levels, []) children = node.children for child in children: child.parent = None children = menu_pool.apply_modifiers(children, request, post_cut=True) context.update( { "children": children, "template": template, "from_level": 0, "to_level": 0, "extra_inactive": 0, "extra_active": 0, } ) return context
def _get_context_internal(self, context, from_level, to_level, extra_inactive, extra_active, max_count, filter_opt, template, namespace, root_id, next_page): trim_children = (filter_opt & FILTER_TRIM_CHILDREN != 0) try: # If there's an exception (500), default context_processors may not be called. request = context['request'] except KeyError: return {'template': 'menu/empty.html'} has_root_node = False if next_page: children = next_page.children else: #new menu... get all the data so we can save a lot of queries nodes = menu_pool.get_nodes(request, namespace, root_id) if root_id: # find the root id and cut the nodes id_nodes = menu_pool.get_nodes_by_attribute(nodes, "reverse_id", root_id) if id_nodes: root_node = node = id_nodes[0] has_root_node = True new_nodes = node.children for n in new_nodes: n.parent = None from_level += node.level + 1 to_level += node.level + 1 else: new_nodes = [] nodes = new_nodes children = cut_levels(nodes, from_level, to_level, extra_inactive, extra_active, trim_children) children = menu_pool.apply_modifiers(children, request, namespace, root_id, post_cut=True) if filter_opt & FILTER_INTER: children = [node for node in children if node.is_leaf_node] elif filter_opt & FILTER_LEAF: children = [node for node in children if not node.is_leaf_node] # only return the top ``max_count`` ones if specified if max_count != -1: children = children[:max_count] try: context.update({'children':children, 'template':template, 'from_level':from_level, 'to_level':to_level, 'extra_inactive':extra_inactive, 'extra_active':extra_active, 'max_count':max_count, 'filter_opt':filter_opt, 'namespace':namespace}) if has_root_node: context['root_node'] = root_node except: context = {"template":template} return context
def _navigation_preview(self, request, nav_node): if not request or not nav_node: return '' nodes = menu_pool.get_nodes(request, None, None) nodes = cut_levels(nodes, 0, 1, 1, 100) nodes = menu_pool.apply_modifiers( nodes, request, None, None, post_cut=True) output = [] self._get_nodes(request, nodes, nav_node.menu_id, output) html_preview = ''.join(output) if 'current-node' not in html_preview: return "" return html_preview
def show_menu(context, from_level=0, to_level=100, extra_inactive=0, extra_active=100, template="menu/menu.html", namespace=None, root_id=None, next_page=None, ): """ render a nested list of all children of the pages - from_level: starting level - to_level: max level - extra_inactive: how many levels should be rendered of the not active tree? - extra_active: how deep should the children of the active node be rendered? - namespace: the namespace of the menu. if empty will use all namespaces - root_id: the id of the root node - template: template used to render the menu """ try: # If there's an exception (500), default context_processors may not be called. request = context['request'] except KeyError: return {'template': 'menu/empty.html'} if next_page: children = next_page.children else: #new menu... get all the data so we can save a lot of queries nodes = menu_pool.get_nodes(request, namespace, root_id) if root_id: # find the root id and cut the nodes id_nodes = menu_pool.get_nodes_by_attribute(nodes, "reverse_id", root_id) if id_nodes: node = id_nodes[0] new_nodes = node.children for n in new_nodes: n.parent = None from_level += node.level + 1 to_level += node.level + 1 else: new_nodes = [] nodes = new_nodes children = cut_levels(nodes, from_level, to_level, extra_inactive, extra_active) children = menu_pool.apply_modifiers(children, request, namespace, root_id, post_cut=True) try: context.update({'children':children, 'template':template, 'from_level':from_level, 'to_level':to_level, 'extra_inactive':extra_inactive, 'extra_active':extra_active, 'namespace':namespace}) except: context = {"template":template} return context
def get_context(self, context, from_level, to_level, extra_inactive, extra_active, template, namespace, root_id, next_page): try: # If there's an exception (500), default context_processors may not be called. request = context['request'] except KeyError: return {'template': 'menu/empty.html'} if next_page: children = next_page.children else: #new menu... get all the data so we can save a lot of queries nodes = menu_pool.get_nodes(request, namespace, root_id) if root_id: # find the root id and cut the nodes id_nodes = menu_pool.get_nodes_by_attribute( nodes, "reverse_id", root_id) if id_nodes: node = id_nodes[0] nodes = node.children for remove_parent in nodes: remove_parent.parent = None from_level += node.level + 1 to_level += node.level + 1 nodes = flatten(nodes) else: nodes = [] children = cut_levels(nodes, from_level, to_level, extra_inactive, extra_active) children = menu_pool.apply_modifiers(children, request, namespace, root_id, post_cut=True) try: context.update({ 'children': children, 'template': template, 'from_level': from_level, 'to_level': to_level, 'extra_inactive': extra_inactive, 'extra_active': extra_active, 'namespace': namespace }) except: context = {"template": template} return context
def get_context( self, context, from_level, to_level, extra_inactive, extra_active, template, namespace, root_id, next_page ): try: # If there's an exception (500), default context_processors may not be called. request = context["request"] except KeyError: return {"template": "menu/empty.html"} if next_page: children = next_page.children else: # new menu... get all the data so we can save a lot of queries nodes = menu_pool.get_nodes(request, namespace, root_id) if root_id: # find the root id and cut the nodes id_nodes = menu_pool.get_nodes_by_attribute(nodes, "reverse_id", root_id) if id_nodes: node = id_nodes[0] new_nodes = node.children for n in new_nodes: n.parent = None from_level += node.level + 1 to_level += node.level + 1 else: new_nodes = [] nodes = new_nodes children = cut_levels(nodes, from_level, to_level, extra_inactive, extra_active) children = menu_pool.apply_modifiers(children, request, namespace, root_id, post_cut=True) try: context.update( { "children": children, "template": template, "from_level": from_level, "to_level": to_level, "extra_inactive": extra_inactive, "extra_active": extra_active, "namespace": namespace, } ) except: context = {"template": template} return context
def get_context(self, context, from_level, to_level, extra_inactive, extra_active, template, namespace, root_id, next_page): try: # If there's an exception (500), default context_processors may not be called. request = context['request'] except KeyError: return {'template': 'menu/empty.html'} if next_page: children = next_page.children else: #new menu... get all the data so we can save a lot of queries nodes = menu_pool.get_nodes(request, namespace, root_id) if root_id: # find the root id and cut the nodes id_nodes = menu_pool.get_nodes_by_attribute(nodes, "reverse_id", root_id) if id_nodes: node = id_nodes[0] nodes = node.children for remove_parent in nodes: remove_parent.parent = None from_level += node.level + 1 to_level += node.level + 1 nodes = flatten(nodes) else: nodes = [] children = cut_levels(nodes, from_level, to_level, extra_inactive, extra_active) children = menu_pool.apply_modifiers(children, request, namespace, root_id, post_cut=True) try: context.update({'children': children, 'template': template, 'from_level': from_level, 'to_level': to_level, 'extra_inactive': extra_inactive, 'extra_active': extra_active, 'namespace': namespace}) except: context = {"template": template} return context
def get_context(self, context, from_level, to_level, extra_inactive, extra_active, template, namespace, root_id, next_page): try: # If there's an exception (500), default context_processors may not be called. request = context['request'] except KeyError: return {'template': 'menu/empty.html'} activeul = '' activehref = '' menu_renderer = context.get('cms_menu_renderer') if not menu_renderer: menu_renderer = menu_pool.get_renderer(request) if next_page: children = next_page.children else: # new menu... get all the data so we can save a lot of queries nodes = menu_renderer.get_nodes(request, namespace, root_id) if root_id: # find the root id and cut the nodes id_nodes = menu_renderer.get_nodes_by_attribute( nodes, "reverse_id", root_id) if id_nodes: node = id_nodes[0] nodes = node.children for remove_parent in nodes: remove_parent.parent = None from_level += node.level + 1 to_level += node.level + 1 nodes = flatten(nodes) else: nodes = [] nodes = menu_renderer.get_nodes(request, namespace, root_id) isleafnode = False for n in nodes: if n.selected: if n.is_leaf_node: isleafnode = True ancestor = "/".join( n.get_absolute_url().split('/')[0:-3]) + '/' directancestor = "/".join( n.get_absolute_url().split('/')[0:-2]) + '/' from_level = n.level - 2 else: ancestor = "/".join( n.get_absolute_url().split('/')[0:-2]) + '/' directancestor = "/".join( n.get_absolute_url().split('/')[0:-1]) + '/' from_level = n.level - 1 activehref = n # print "ancestor",n.get_absolute_url(),ancestor,directancestor,from_level continue for n in nodes: # print n.get_absolute_url() == directancestor,n.get_absolute_url(),directancestor if n.get_absolute_url() == directancestor: activeul = n continue to_level = from_level + 2 if next_page: to_level = 100 children = next_page.children else: children = cut_levels(nodes, from_level, to_level, extra_inactive, extra_active) children = menu_pool.apply_modifiers(children, request, namespace, root_id, post_cut=True) #print "activeul",activeul.get_absolute_url() #print "activehref",activehref.get_absolute_url() #print "ancestor",ancestor #print "directancestor",directancestor #print "BEFORE children",children newChildren = [] for i in children: #print i.level,i.get_absolute_url(), if i.get_absolute_url().startswith(ancestor[:-1]): #print "kept", newChildren.append(i) #print "" try: #print "AFTER children",newChildren context.update({ 'children': newChildren, 'activehref': activehref, 'activeul': activeul, 'ancestor': ancestor, 'template': template, 'from_level': from_level, 'to_level': to_level, 'extra_inactive': extra_inactive, 'extra_active': extra_active, 'namespace': namespace }) except: context = {"template": template} return context
def show_menu( context, from_level=0, to_level=100, extra_inactive=0, extra_active=100, template="menu/menu.html", namespace=None, root_id=None, next_page=None, ): """ render a nested list of all children of the pages - from_level: starting level - to_level: max level - extra_inactive: how many levels should be rendered of the not active tree? - extra_active: how deep should the children of the active node be rendered? - namespace: the namespace of the menu. if empty will use all namespaces - root_id: the id of the root node - template: template used to render the menu """ try: # If there's an exception (500), default context_processors may not be called. request = context['request'] except KeyError: return {'template': 'menu/empty.html'} if next_page: children = next_page.children else: #new menu... get all the data so we can save a lot of queries nodes = menu_pool.get_nodes(request, namespace, root_id) if root_id: # find the root id and cut the nodes id_nodes = menu_pool.get_nodes_by_attribute( nodes, "reverse_id", root_id) if id_nodes: node = id_nodes[0] new_nodes = node.children for n in new_nodes: n.parent = None from_level += node.level + 1 to_level += node.level + 1 else: new_nodes = [] nodes = new_nodes children = cut_levels(nodes, from_level, to_level, extra_inactive, extra_active) children = menu_pool.apply_modifiers(children, request, namespace, root_id, post_cut=True) try: context.update({ 'children': children, 'template': template, 'from_level': from_level, 'to_level': to_level, 'extra_inactive': extra_inactive, 'extra_active': extra_active, 'namespace': namespace }) except: context = {"template": template} return context