#NEWLINE# def _canonicalize_query( self , query ) : #NEWLINE# #INDENT# '\n        Transform the query dictionary to replace e.g. documents with __ref__ fields.\n        ' #NEWLINE# def transform_query( q ) : #NEWLINE# #INDENT# for encoder in self . query_encoders : #NEWLINE# #INDENT# q = encoder . encode ( q , [ ] ) #NEWLINE# #UNINDENT# if isinstance ( q , dict ) : #NEWLINE# #INDENT# nq = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for ( key , value ) in q . items ( ) : #NEWLINE# #INDENT# new_key = key #NEWLINE# if ( isinstance ( value , dict ) and ( len ( value ) == 1 ) and list ( value . keys ( ) ) [ 0 ] . startswith ( '$' ) ) : #NEWLINE# #INDENT# if ( list ( value . keys ( ) ) [ 0 ] in ( '$all' , '$in' ) ) : #NEWLINE# #INDENT# if ( list ( value . values ( ) ) [ 0 ] and isinstance ( list ( value . values ( ) ) [ 0 ] [ 0 ] , Document ) ) : #NEWLINE# #INDENT# if self . _use_pk_based_refs : #NEWLINE# #INDENT# new_key += '.pk' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# new_key += '.__ref__' #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( value , Document ) : #NEWLINE# #INDENT# if self . _use_pk_based_refs : #NEWLINE# #INDENT# new_key += '.pk' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# new_key += '.__ref__' #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# nq [ new_key ] = transform_query ( value ) #NEWLINE# #UNINDENT# return nq #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( q , ( list , QuerySet , tuple ) ) : #NEWLINE# #INDENT# return [ transform_query ( x ) for x in q ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( q , Document ) : #NEWLINE# #INDENT# collection = self . get_collection_for_obj ( q ) #NEWLINE# if self . _use_pk_based_refs : #NEWLINE# #INDENT# return q . pk #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return ( '%s:%s' % ( collection , q . pk ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# return q #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return transform_query ( transform_query )