#NEWLINE# def get_tree( self , content_object , root = None , sort = 'date_submitted' ) : #NEWLINE# #INDENT# '\n        Runs a depth-first search on all comments related to the given content_object.\n        This depth-first search adds a ``depth`` attribute to the comment which\n        signifies how how deeply nested the comment is away from the original object.\n        \n        If root is specified, it will start the tree from that comment\'s ID.\n        \n        Ideally, one would use this ``depth`` attribute in the display of the comment to\n        offset that comment by some specified length.\n        \n        The following is a (VERY) simple example of how the depth property might be used in a template:\n        \n            {% for comment in comment_tree %}\n                <p style="margin-left: {{ comment.depth }}em">{{ comment.comment }}</p>\n            {% endfor %}\n        ' #NEWLINE# content_type = ContentType . objects . get_for_model ( content_object ) #NEWLINE# children = list ( self . get_query_set ( ) . filter ( content_type = content_type , object_id = getattr ( content_object , 'pk' , getattr ( content_object , 'id' ) ) ) . select_related ( ) . order_by ( sort ) ) #NEWLINE# to_return = [ ] #NEWLINE# if child : #NEWLINE# #INDENT# if isinstance ( root , int ) : #NEWLINE# #INDENT# root_id = root #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# root_id = root . id #NEWLINE# #UNINDENT# to_return = [ c for c in children if ( c . id == root_id ) ] #NEWLINE# if to_return : #NEWLINE# #INDENT# to_return [ 0 ] . depth = 0 #NEWLINE# for child in children : #NEWLINE# #INDENT# if ( child . parent_id == root_id ) : #NEWLINE# #INDENT# to_return . extend ( dfs ( child , children , 1 ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# for child in children : #NEWLINE# #INDENT# if ( not child . parent ) : #NEWLINE# #INDENT# to_return . extend ( dfs ( child , children , 0 ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return to_return