#NEWLINE# @ timeout #NEWLINE# @ defer . inlineCallbacks #NEWLINE# def insert( self , docs , safe = None , flags = 0 , **kwargs ) : #NEWLINE# #INDENT# if isinstance ( docs , dict ) : #NEWLINE# #INDENT# ids = docs . get ( '_id' , ObjectId ( ) ) #NEWLINE# docs [ '_id' ] = ids #NEWLINE# docs = [ docs ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( docs , list ) : #NEWLINE# #INDENT# ids = [ ] #NEWLINE# for doc in docs : #NEWLINE# #INDENT# if isinstance ( doc , dict ) : #NEWLINE# #INDENT# oid = doc . get ( '_id' , ObjectId ( ) ) #NEWLINE# ids . append ( oid ) #NEWLINE# doc [ '_id' ] = oid #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise TypeError ( 'TxMongo: insert takes a document or a list of documents.' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise TypeError ( 'TxMongo: insert takes a document or a list of documents.' ) #NEWLINE# #UNINDENT# #UNINDENT# docs = [ BSON . encode ( d ) for d in docs ] #NEWLINE# insert = Insert ( flags = flags , collection = str ( self ) , documents = docs ) #NEWLINE# proto = ( yield self . _database . connection . getprotocol ( ) ) #NEWLINE# check_deadline ( kwargs . pop ( '_deadline' , None ) ) #NEWLINE# proto . send_INSERT ( insert ) #NEWLINE# write_concern = self . _get_write_concern ( safe , ** kwargs ) #NEWLINE# if write_concern . acknowledged : #NEWLINE# #INDENT# ( yield proto . get_last_error ( str ( self . _database ) , ** write_concern . document ) ) #NEWLINE# #UNINDENT# defer . returnValue ( ids )