<s> from django . core . files . uploadedfile import UploadedFile <EOL> from django . utils . datastructures import MultiValueDict <EOL> from django . utils . functional import lazy_property <EOL> from django . utils import six <EOL> from . exceptions import NoFileStorageConfigured <EOL> class BaseStorage ( object ) : <EOL> step_key = '<STR_LIT>' <EOL> step_data_key = '<STR_LIT>' <EOL> step_files_key = '<STR_LIT>' <EOL> extra_data_key = '<STR_LIT>' <EOL> def __init__ ( self , prefix , request = None , file_storage = None ) : <EOL> self . prefix = '<STR_LIT>' % prefix <EOL> self . request = request <EOL> self . file_storage = file_storage <EOL> self . _files = { } <EOL> self . _tmp_files = [ ] <EOL> def init_data ( self ) : <EOL> self . data = { <EOL> self . step_key : None , <EOL> self . step_data_key : { } , <EOL> self . step_files_key : { } , <EOL> self . extra_data_key : { } , <EOL> } <EOL> def reset ( self ) : <EOL> wizard_files = self . data [ self . step_files_key ] <EOL> for step_files in six . itervalues ( wizard_files ) : <EOL> for step_file in six . itervalues ( step_files ) : <EOL> self . _tmp_files . append ( step_file [ '<STR_LIT>' ] ) <EOL> self . init_data ( ) <EOL> def _get_current_step ( self ) : <EOL> return self . data [ self . step_key ] <EOL> def _set_current_step ( self , step ) : <EOL> self . data [ self . step_key ] = step <EOL> current_step = lazy_property ( _get_current_step , _set_current_step ) <EOL> def _get_extra_data ( self ) : <EOL> return self . data [ self . extra_data_key ] <EOL> def _set_extra_data ( self , extra_data ) : <EOL> self . data [ self . extra_data_key ] = extra_data <EOL> extra_data = lazy_property ( _get_extra_data , _set_extra_data ) <EOL> def get_step_data ( self , step ) : <EOL> values = self . data [ self . step_data_key ] . get ( step , None ) <EOL> if values is not None : <EOL> values = MultiValueDict ( values ) <EOL> return values <EOL> def set_step_data ( self , step , cleaned_data ) : <EOL> if isinstance ( cleaned_data , MultiValueDict ) : <EOL> cleaned_data = dict ( cleaned_data . lists ( ) ) <EOL> self . data [ self . step_data_key ] [ step ] = cleaned_data <EOL> @ property <EOL> def current_step_data ( self ) : <EOL> return self . get_step_data ( self . current_step ) <EOL> def get_step_files ( self , step ) : <EOL> wizard_files = self . data [ self . step_files_key ] . get ( step , { } ) <EOL> if wizard_files and not self . file_storage : <EOL> raise NoFileStorageConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> files = { } <EOL> for field , field_dict in six . iteritems ( wizard_files ) : <EOL> field_dict = field_dict . copy ( ) <EOL> tmp_name = field_dict . pop ( '<STR_LIT>' ) <EOL> if ( step , field ) not in self . _files : <EOL> self . _files [ ( step , field ) ] = UploadedFile ( <EOL> file = self . file_storage . open ( tmp_name ) , ** field_dict ) <EOL> files [ field ] = self . _files [ ( step , field ) ] <EOL> return files or None <EOL> def set_step_files ( self , step , files ) : <EOL> if files and not self . file_storage : <EOL> raise NoFileStorageConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if step not in self . data [ self . step_files_key ] : <EOL> self . data [ self . step_files_key ] [ step ] = { } <EOL> for field , field_file in six . iteritems ( files or { } ) : <EOL> tmp_filename = self . file_storage . save ( field_file . name , field_file ) <EOL> file_dict = { <EOL> '<STR_LIT>' : tmp_filename , <EOL> '<STR_LIT:name>' : field_file . name , <EOL> '<STR_LIT>' : field_file . content_type , <EOL> '<STR_LIT:size>' : field_file . size , <EOL> '<STR_LIT>' : field_file . charset <EOL> } <EOL> self . data [ self . step_files_key ] [ step ] [ field ] = file_dict <EOL> @ property <EOL> def current_step_files ( self ) : <EOL> return self . get_step_files ( self . current_step ) <EOL> def update_response ( self , response ) : <EOL> def post_render_callback ( response ) : <EOL> for file in self . _files . values ( ) : <EOL> if not file . closed : <EOL> file . close ( ) <EOL> for tmp_file in self . _tmp_files : <EOL> self . file_storage . delete ( tmp_file ) <EOL> if hasattr ( response , '<STR_LIT>' ) : <EOL> response . add_post_render_callback ( post_render_callback ) <EOL> else : <EOL> post_render_callback ( response ) </s>
