(user, root)
return root, comment_tree, answers, thread
Populates a tree that contains all posts in the thread.

    Answers sorted before comments.
Populates a tree that contains all posts in the thread.
Populates a tree that contains all posts in the thread .
def post_tree(user, root):
    """
    Populates a tree that contains all posts in the thread.

    Answers sorted before comments.
    """

    # Get all posts that belong to post root.
    query = Post.objects.valid_posts(u=user, root=root).exclude(pk=root.id)

    # Filter spam/deleted comments or answers.
    if user.is_anonymous or not user.profile.is_moderator:
        query = query.exclude(Q(status=Post.DELETED) | Q(spam=Post.SPAM))

    query = query.select_related("lastedit_user__profile", "author__profile", "root__author__profile")

    # Apply the sort order to all posts in thread.
    thread = query.order_by("type", "-accept_count", "-vote_count", "creation_date")

    # Gather votes by the current user.
    votes = get_votes(user=user, root=root)

    # Shortcuts to each storage.
    bookmarks, upvotes = votes[Vote.BOOKMARK], votes[Vote.UP]

    # Build comments tree.
    comment_tree = dict()

    def decorate(post):
        # Mutates the elements! Not worth creating copies.
        if post.is_comment:
            comment_tree.setdefault(post.parent_id, []).append(post)
        post.has_bookmark = int(post.id in bookmarks)
        post.has_upvote = int(post.id in upvotes)
        if user.is_authenticated:
            post.can_accept = not post.is_toplevel and (user == post.root.author or user.profile.is_moderator)
            post.can_moderate = user.profile.is_moderator
            post.is_editable = (user == post.author or user.profile.is_moderator)
        else:
            post.can_accept = False
            post.is_editable = False
            post.can_moderate = False

        return post

    # Decorate the objects for easier access
    thread = list(map(decorate, thread))

    # Decorate the root post
    root = decorate(root)

    # Select the answers from the thread.
    answers = [p for p in thread if p.type == Post.ANSWER]

    return root, comment_tree, answers, thread
def post_tree ( user , root ) : # Get all posts that belong to post root. query = Post . objects . valid_posts ( u = user , root = root ) . exclude ( pk = root . id ) # Filter spam/deleted comments or answers. if user . is_anonymous or not user . profile . is_moderator : query = query . exclude ( Q ( status = Post . DELETED ) | Q ( spam = Post . SPAM ) ) query = query . select_related ( "lastedit_user__profile" , "author__profile" , "root__author__profile" ) # Apply the sort order to all posts in thread. thread = query . order_by ( "type" , "-accept_count" , "-vote_count" , "creation_date" ) # Gather votes by the current user. votes = get_votes ( user = user , root = root ) # Shortcuts to each storage. bookmarks , upvotes = votes [ Vote . BOOKMARK ] , votes [ Vote . UP ] # Build comments tree. comment_tree = dict ( ) def decorate ( post ) : # Mutates the elements! Not worth creating copies. if post . is_comment : comment_tree . setdefault ( post . parent_id , [ ] ) . append ( post ) post . has_bookmark = int ( post . id in bookmarks ) post . has_upvote = int ( post . id in upvotes ) if user . is_authenticated : post . can_accept = not post . is_toplevel and ( user == post . root . author or user . profile . is_moderator ) post . can_moderate = user . profile . is_moderator post . is_editable = ( user == post . author or user . profile . is_moderator ) else : post . can_accept = False post . is_editable = False post . can_moderate = False return post # Decorate the objects for easier access thread = list ( map ( decorate , thread ) ) # Decorate the root post root = decorate ( root ) # Select the answers from the thread. answers = [ p for p in thread if p . type == Post . ANSWER ] return root , comment_tree , answers , thread