#NEWLINE# def get_ordered_insertion_target( self , node , parent ) : #NEWLINE# #INDENT# "\n        Attempts to retrieve a suitable right sibling for ``node``\n        underneath ``parent`` (which may be ``None`` in the case of root\n        nodes) so that ordering by the fields specified by the node's class'\n        ``order_insertion_by`` option is maintained.\n\n        Returns ``None`` if no suitable sibling can be found.\n        " #NEWLINE# right_sibling = None #NEWLINE# if ( ( parent is None ) or ( parent . get_descendant_count ( ) > 0 ) ) : #NEWLINE# #INDENT# opts = node . _mptt_meta #NEWLINE# order_by = opts . order_insertion_by [ : ] #NEWLINE# filters = self . insertion_target_filters ( node , order_by ) #NEWLINE# if parent : #NEWLINE# #INDENT# filters = ( filters & Q ( ** { #NEWLINE# #INDENT# opts . parent_attr : parent , #NEWLINE# #UNINDENT# } ) ) #NEWLINE# order_by . append ( opts . left_attr ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# filters = ( filters & Q ( ** { #NEWLINE# #INDENT# opts . parent_attr : None , #NEWLINE# #UNINDENT# } ) ) #NEWLINE# order_by . append ( opts . tree_id_attr ) #NEWLINE# #UNINDENT# queryset = node . __class__ . _tree_manager . filter ( filters ) . order_by ( * order_by ) #NEWLINE# if node . pk : #NEWLINE# #INDENT# queryset = queryset . exclude ( pk = node . pk ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# right_sibling = queryset [ : 1 ] [ 0 ] #NEWLINE# #UNINDENT# except IndexError : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# #UNINDENT# return right_sibling