<s> import logging <EOL> from iktomi . forms import convs , widgets , Field , FieldSet , FileField <EOL> from iktomi . utils import cached_property <EOL> logger = logging . getLogger ( __name__ ) <EOL> class FileFieldSetConv ( convs . Converter ) : <EOL> error_inner = '<STR_LIT>' <EOL> error_lost = '<STR_LIT>' <EOL> error_hacking = '<STR_LIT>' <EOL> def from_python ( self , value ) : <EOL> is_transient = value and value . mode == '<STR_LIT>' <EOL> return { '<STR_LIT>' : value . name if is_transient else None , <EOL> '<STR_LIT>' : value . name if is_transient else None , <EOL> '<STR_LIT:file>' : value , <EOL> '<STR_LIT>' : value . mode if value is not None else '<STR_LIT>' } <EOL> @ cached_property <EOL> def file_manager ( self ) : <EOL> if hasattr ( self . env , '<STR_LIT>' ) and hasattr ( self . env . db , '<STR_LIT>' ) : <EOL> return self . env . db . find_file_manager ( self . field . form . model ) <EOL> return self . env . file_manager <EOL> def _to_python ( self , file = None , mode = None , <EOL> transient_name = None , original_name = None ) : <EOL> file_manager = self . file_manager <EOL> if not self . _is_empty ( file ) : <EOL> file = file_manager . create_transient ( file . file , file . filename ) <EOL> else : <EOL> file = None <EOL> if mode == '<STR_LIT>' : <EOL> if not file and self . required : <EOL> raise convs . ValidationError ( self . error_required ) <EOL> elif mode == '<STR_LIT>' : <EOL> if not original_name : <EOL> logger . warning ( '<STR_LIT>' ) <EOL> if not transient_name : <EOL> logger . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> raise convs . ValidationError ( self . error_hacking ) <EOL> try : <EOL> transient_file = file_manager . get_transient ( transient_name ) <EOL> except OSError : <EOL> raise convs . ValidationError ( self . error_lost ) <EOL> if file : <EOL> file_manager . delete ( transient_file ) <EOL> else : <EOL> file = transient_file <EOL> elif mode == '<STR_LIT>' : <EOL> if not file : <EOL> return self . _existing_value <EOL> return file <EOL> def to_python ( self , value ) : <EOL> value = self . _to_python ( ** value ) <EOL> self . field . set_raw_value ( self . field . form . raw_data , <EOL> self . from_python ( value ) ) <EOL> return value <EOL> def check_file_path ( conv , value ) : <EOL> if value and ( '<STR_LIT:/>' in value or '<STR_LIT:\\>' in value or value [ <NUM_LIT:0> ] in '<STR_LIT>' ) : <EOL> logger . warning ( '<STR_LIT>' '<STR_LIT>' ) <EOL> raise convs . ValidationError ( '<STR_LIT>' ) <EOL> return value <EOL> class FileFieldSet ( FieldSet ) : <EOL> '''<STR_LIT>''' <EOL> fields = [ <EOL> FileField ( '<STR_LIT:file>' , <EOL> conv = convs . SimpleFile ( ) , <EOL> widget = widgets . FileInput ( ) ) , <EOL> Field ( '<STR_LIT>' , <EOL> conv = convs . EnumChoice ( choices = [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] , <EOL> required = True ) , <EOL> widget = widgets . HiddenInput ) , <EOL> Field ( '<STR_LIT>' , <EOL> conv = convs . Char ( check_file_path , required = False ) , <EOL> widget = widgets . HiddenInput ) , <EOL> Field ( '<STR_LIT>' , <EOL> conv = convs . Char ( check_file_path , required = False ) , <EOL> widget = widgets . HiddenInput ) , <EOL> ] <EOL> conv = FileFieldSetConv <EOL> def __init__ ( self , name , ** kwargs ) : <EOL> kwargs . setdefault ( '<STR_LIT>' , self . fields ) <EOL> kwargs . setdefault ( '<STR_LIT>' , self . conv ) <EOL> FieldSet . __init__ ( self , name , ** kwargs ) <EOL> def get_initial ( self ) : <EOL> return None </s>
