1
from itertools import chain, imap
3
__all__ = ['fill_tree', 'annotate_tree_properties', ]
5
def _mark_as_root_path(comment):
7
Mark on comment as Being added to fill the tree.
9
setattr(comment, 'added_path', True)
12
def fill_tree(comments):
14
Insert extra comments in the comments list, so that the root path of the first comment is always visible.
15
Use this in comments' pagination to fill in the tree information.
17
The inserted comments have an ``added_path`` attribute.
24
extra_path_items = imap(_mark_as_root_path, first.root_path)
25
return chain(extra_path_items, [first], it)
27
def annotate_tree_properties(comments):
29
iterate through nodes and adds some magic properties to each of them
30
representing opening list of children and closing it
37
# get the first item, this will fail if no items !
40
# first item starts a new thread
44
# if this comment has a parent, store its last child for future reference
46
last.add(old.last_child_id)
48
# this is the last child, mark it
53
if c.depth > old.depth:
56
else: # c.depth <= old.depth
58
old.close = range(old.depth - c.depth)
61
if old.root_id != c.root_id:
62
# close even the top depth
63
old.close.append(len(old.close))
64
# and start a new thread
72
old.close = range(old.depth)