(request, post, tree, template_name)
return html
Traverses the tree and generates the page
Traverses the tree and generates the page
Traverses the tree and generates the page
def traverse_comments(request, post, tree, template_name):
    "Traverses the tree and generates the page"

    body = template.loader.get_template(template_name)
    seen = set()

    def traverse(node, collect=[]):

        cont = {"post": node, 'user': request.user, 'request': request}
        html = body.render(cont)
        collect.append(f'<div class="indent" ><div>{html}</div>')

        for child in tree.get(node.id, []):
            if child in seen:
                raise Exception(f"circular tree {child.pk} {child.title}")
            seen.add(child)
            traverse(child, collect=collect)

        collect.append(f"</div>")

    # this collects the comments for the post
    collect = ['<div class="comment-list">']
    for node in tree[post.id]:
        traverse(node, collect=collect)
    collect.append("</div>")
    html = '\n'.join(collect)

    return html
def traverse_comments ( request , post , tree , template_name ) : body = template . loader . get_template ( template_name ) seen = set ( ) def traverse ( node , collect = [ ] ) : cont = { "post" : node , 'user' : request . user , 'request' : request } html = body . render ( cont ) collect . append ( f'<div class="indent" ><div>{html}</div>' ) for child in tree . get ( node . id , [ ] ) : if child in seen : raise Exception ( f"circular tree {child.pk} {child.title}" ) seen . add ( child ) traverse ( child , collect = collect ) collect . append ( f"</div>" ) # this collects the comments for the post collect = [ '<div class="comment-list">' ] for node in tree [ post . id ] : traverse ( node , collect = collect ) collect . append ( "</div>" ) html = '\n' . join ( collect ) return html