#NEWLINE# def construct_instance( form , instance , fields = None , exclude = None ) : #NEWLINE# #INDENT# "\n    Constructs and returns a document instance from the bound ``form``'s\n    ``cleaned_data``, but does not save the returned instance to the\n    database.\n    " #NEWLINE# cleaned_data = form . cleaned_data #NEWLINE# file_field_list = [ ] #NEWLINE# if isinstance ( instance , type ) : #NEWLINE# #INDENT# instance = instance ( ) #NEWLINE# #UNINDENT# for f in instance . _fields . values ( ) : #NEWLINE# #INDENT# if isinstance ( f , ObjectIdField ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( f . name not in cleaned_data ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( ( fields is not None ) and ( f . name not in fields ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( exclude and ( f . name in exclude ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( isinstance ( f , FileField ) or ( isinstance ( f , ( MapField , ListField ) ) and isinstance ( f . field , FileField ) ) ) : #NEWLINE# #INDENT# file_field_list . append ( f ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# setattr ( instance , f . name , cleaned_data . get ( f . name ) ) #NEWLINE# #UNINDENT# #UNINDENT# for f in file_field_list : #NEWLINE# #INDENT# if isinstance ( f , MapField ) : #NEWLINE# #INDENT# map_field = getattr ( instance , f . name ) #NEWLINE# uploads = cleaned_data [ f . name ] #NEWLINE# for ( key , uploaded_file ) in uploads . items ( ) : #NEWLINE# #INDENT# if ( uploaded_file is None ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# file_data = map_field . get ( key , None ) #NEWLINE# map_field [ key ] = _save_iterator_file ( f , instance , uploaded_file , file_data ) #NEWLINE# #UNINDENT# setattr ( map_field , f . name , map_field ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( f , ListField ) : #NEWLINE# #INDENT# list_field = getattr ( instance , f . name ) #NEWLINE# uploads = cleaned_data [ f . name ] #NEWLINE# for ( i , uploaded_file ) in enumerate ( uploads ) : #NEWLINE# #INDENT# if ( uploaded_file is None ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# file_data = list_field [ i ] #NEWLINE# #UNINDENT# except IndexError : #NEWLINE# #INDENT# file_data = None #NEWLINE# #UNINDENT# file_obj = _save_iterator_file ( f , instance , uploaded_file , file_data ) #NEWLINE# try : #NEWLINE# #INDENT# list_field [ i ] = file_obj #NEWLINE# #UNINDENT# except IndexError : #NEWLINE# #INDENT# list_field . append ( file_obj ) #NEWLINE# #UNINDENT# #UNINDENT# setattr ( instance , f . name , list_field ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# field = getattr ( instance , f . name ) #NEWLINE# upload = cleaned_data [ f . name ] #NEWLINE# if ( upload is None ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# upload . file . seek ( 0 ) #NEWLINE# if field . grid_id : #NEWLINE# #INDENT# field . delete ( ) #NEWLINE# #UNINDENT# filename = _get_unique_filename ( upload . name , f . db_alias , f . collection_name ) #NEWLINE# field . put ( upload , content_type = upload . content_type , filename = filename ) #NEWLINE# setattr ( instance , f . name , field ) #NEWLINE# #UNINDENT# except AttributeError : #NEWLINE# #INDENT# upload . get ( ) #NEWLINE# setattr ( instance , f . name , upload ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return instance