Created
March 16, 2015 18:09
-
-
Save sirex/58679fe8dbaa08e7e924 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test_get_top_menu_page(self): | |
def render_wholetree(tree, parent, level=0): | |
yield '%s<ul>' % (' ' * level) | |
for child in tree[parent]: | |
yield '%s<li>%s</li>' % (' ' * (level+1), child) | |
if child in tree: | |
yield from render_wholetree(tree, child, level+1) | |
yield '%s</ul>' % (' ' * level) | |
def render_tree(pages, parent): | |
tree = collections.defaultdict(list) | |
for page in pages: | |
tree[page.get_parent().id].append(page.id) | |
import pprint; pprint.pprint(dict(tree)) | |
return '\n'.join(render_wholetree(tree, parent.id)) | |
root = Page.objects.get(url_path='/') | |
pages_tree = root.get_descendants() | |
tree_html = render_tree(pages_tree, root) | |
print(tree_html) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment