#NEWLINE# def to_tree( self ) : #NEWLINE# #INDENT# cache_key = ( 'menu-tree-%s' % self . slug ) #NEWLINE# root = cache . get ( cache_key ) #NEWLINE# if ( not root ) : #NEWLINE# #INDENT# item = root = Item ( self ) #NEWLINE# descendents = self . get_descendants ( ) #NEWLINE# for ( prev , curr , next ) in previous_current_next ( descendents ) : #NEWLINE# #INDENT# previous_item = item #NEWLINE# item = Item ( curr ) #NEWLINE# if ( ( not prev ) or ( prev . level < curr . level ) ) : #NEWLINE# #INDENT# previous_item . add_child ( item ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( prev and ( prev . level > curr . level ) ) : #NEWLINE# #INDENT# parent = previous_item #NEWLINE# while ( parent . node . level >= curr . level ) : #NEWLINE# #INDENT# parent = parent . parent #NEWLINE# #UNINDENT# parent . add_child ( item ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# previous_item . parent . add_child ( item ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# cache . set ( cache_key , root ) #NEWLINE# #UNINDENT# return root