<s> import datetime <EOL> import os <EOL> from django import forms <EOL> from django . db . models . fields import Field <EOL> from django . core . files . base import File <EOL> from django . core . files . storage import default_storage <EOL> from django . core . files . images import ImageFile <EOL> from django . db . models import signals <EOL> from django . utils . encoding import force_str , force_text <EOL> from django . utils import six <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> class FieldFile ( File ) : <EOL> def __init__ ( self , instance , field , name ) : <EOL> super ( FieldFile , self ) . __init__ ( None , name ) <EOL> self . instance = instance <EOL> self . field = field <EOL> self . storage = field . storage <EOL> self . _committed = True <EOL> def __eq__ ( self , other ) : <EOL> if hasattr ( other , '<STR_LIT:name>' ) : <EOL> return self . name == other . name <EOL> return self . name == other <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . name ) <EOL> def _require_file ( self ) : <EOL> if not self : <EOL> raise ValueError ( "<STR_LIT>" % self . field . name ) <EOL> def _get_file ( self ) : <EOL> self . _require_file ( ) <EOL> if not hasattr ( self , '<STR_LIT>' ) or self . _file is None : <EOL> self . _file = self . storage . open ( self . name , '<STR_LIT:rb>' ) <EOL> return self . _file <EOL> def _set_file ( self , file ) : <EOL> self . _file = file <EOL> def _del_file ( self ) : <EOL> del self . _file <EOL> file = property ( _get_file , _set_file , _del_file ) <EOL> def _get_path ( self ) : <EOL> self . _require_file ( ) <EOL> return self . storage . path ( self . name ) <EOL> path = property ( _get_path ) <EOL> def _get_url ( self ) : <EOL> self . _require_file ( ) <EOL> return self . storage . url ( self . name ) <EOL> url = property ( _get_url ) <EOL> def _get_size ( self ) : <EOL> self . _require_file ( ) <EOL> if not self . _committed : <EOL> return self . file . size <EOL> return self . storage . size ( self . name ) <EOL> size = property ( _get_size ) <EOL> def open ( self , mode = '<STR_LIT:rb>' ) : <EOL> self . _require_file ( ) <EOL> self . file . open ( mode ) <EOL> open . alters_data = True <EOL> def save ( self , name , content , save = True ) : <EOL> name = self . field . generate_filename ( self . instance , name ) <EOL> self . name = self . storage . save ( name , content ) <EOL> setattr ( self . instance , self . field . name , self . name ) <EOL> self . _size = content . size <EOL> self . _committed = True <EOL> if save : <EOL> self . instance . save ( ) <EOL> save . alters_data = True <EOL> def delete ( self , save = True ) : <EOL> if not self : <EOL> return <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . close ( ) <EOL> del self . file <EOL> self . storage . delete ( self . name ) <EOL> self . name = None <EOL> setattr ( self . instance , self . field . name , self . name ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> del self . _size <EOL> self . _committed = False <EOL> if save : <EOL> self . instance . save ( ) <EOL> delete . alters_data = True <EOL> def _get_closed ( self ) : <EOL> file = getattr ( self , '<STR_LIT>' , None ) <EOL> return file is None or file . closed <EOL> closed = property ( _get_closed ) <EOL> def close ( self ) : <EOL> file = getattr ( self , '<STR_LIT>' , None ) <EOL> if file is not None : <EOL> file . close ( ) <EOL> def __getstate__ ( self ) : <EOL> return { '<STR_LIT:name>' : self . name , '<STR_LIT>' : False , '<STR_LIT>' : True , '<STR_LIT>' : None } <EOL> class FileDescriptor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , field ) : <EOL> self . field = field <EOL> def __get__ ( self , instance = None , owner = None ) : <EOL> if instance is None : <EOL> raise AttributeError ( <EOL> "<STR_LIT>" <EOL> % ( self . field . name , owner . __name__ ) ) <EOL> file = instance . __dict__ [ self . field . name ] <EOL> if isinstance ( file , six . string_types ) or file is None : <EOL> attr = self . field . attr_class ( instance , self . field , file ) <EOL> instance . __dict__ [ self . field . name ] = attr <EOL> elif isinstance ( file , File ) and not isinstance ( file , FieldFile ) : <EOL> file_copy = self . field . attr_class ( instance , self . field , file . name ) <EOL> file_copy . file = file <EOL> file_copy . _committed = False <EOL> instance . __dict__ [ self . field . name ] = file_copy <EOL> elif isinstance ( file , FieldFile ) and not hasattr ( file , '<STR_LIT>' ) : <EOL> file . instance = instance <EOL> file . field = self . field <EOL> file . storage = self . field . storage <EOL> return instance . __dict__ [ self . field . name ] <EOL> def __set__ ( self , instance , value ) : <EOL> instance . __dict__ [ self . field . name ] = value <EOL> class FileField ( Field ) : <EOL> attr_class = FieldFile <EOL> descriptor_class = FileDescriptor <EOL> description = _ ( "<STR_LIT>" ) <EOL> def __init__ ( self , verbose_name = None , name = None , upload_to = '<STR_LIT>' , storage = None , ** kwargs ) : <EOL> for arg in ( '<STR_LIT:primary_key>' , '<STR_LIT>' ) : <EOL> if arg in kwargs : <EOL> raise TypeError ( "<STR_LIT>" % ( arg , self . __class__ ) ) <EOL> self . storage = storage or default_storage <EOL> self . upload_to = upload_to <EOL> if callable ( upload_to ) : <EOL> self . generate_filename = upload_to <EOL> kwargs [ '<STR_LIT:max_length>' ] = kwargs . get ( '<STR_LIT:max_length>' , <NUM_LIT:100> ) <EOL> super ( FileField , self ) . __init__ ( verbose_name , name , ** kwargs ) <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def get_prep_lookup ( self , lookup_type , value ) : <EOL> if hasattr ( value , '<STR_LIT:name>' ) : <EOL> value = value . name <EOL> return super ( FileField , self ) . get_prep_lookup ( lookup_type , value ) <EOL> def get_prep_value ( self , value ) : <EOL> "<STR_LIT>" <EOL> if value is None : <EOL> return None <EOL> return six . text_type ( value ) <EOL> def pre_save ( self , model_instance , add ) : <EOL> "<STR_LIT>" <EOL> file = super ( FileField , self ) . pre_save ( model_instance , add ) <EOL> if file and not file . _committed : <EOL> file . save ( file . name , file , save = False ) <EOL> return file <EOL> def contribute_to_class ( self , cls , name ) : <EOL> super ( FileField , self ) . contribute_to_class ( cls , name ) <EOL> setattr ( cls , self . name , self . descriptor_class ( self ) ) <EOL> def get_directory_name ( self ) : <EOL> return os . path . normpath ( force_text ( datetime . datetime . now ( ) . strftime ( force_str ( self . upload_to ) ) ) ) <EOL> def get_filename ( self , filename ) : <EOL> return os . path . normpath ( self . storage . get_valid_name ( os . path . basename ( filename ) ) ) <EOL> def generate_filename ( self , instance , filename ) : <EOL> return os . path . join ( self . get_directory_name ( ) , self . get_filename ( filename ) ) <EOL> def save_form_data ( self , instance , data ) : <EOL> if data is not None : <EOL> if not data : <EOL> data = '<STR_LIT>' <EOL> setattr ( instance , self . name , data ) <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { '<STR_LIT>' : forms . FileField , '<STR_LIT:max_length>' : self . max_length } <EOL> if '<STR_LIT>' in kwargs : <EOL> defaults [ '<STR_LIT>' ] = False <EOL> defaults . update ( kwargs ) <EOL> return super ( FileField , self ) . formfield ( ** defaults ) <EOL> class ImageFileDescriptor ( FileDescriptor ) : <EOL> """<STR_LIT>""" <EOL> def __set__ ( self , instance , value ) : <EOL> previous_file = instance . __dict__ . get ( self . field . name ) <EOL> super ( ImageFileDescriptor , self ) . __set__ ( instance , value ) <EOL> if previous_file is not None : <EOL> self . field . update_dimension_fields ( instance , force = True ) <EOL> class ImageFieldFile ( ImageFile , FieldFile ) : <EOL> def delete ( self , save = True ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> del self . _dimensions_cache <EOL> super ( ImageFieldFile , self ) . delete ( save ) <EOL> class ImageField ( FileField ) : <EOL> attr_class = ImageFieldFile <EOL> descriptor_class = ImageFileDescriptor <EOL> description = _ ( "<STR_LIT>" ) <EOL> def __init__ ( self , verbose_name = None , name = None , width_field = None , <EOL> height_field = None , ** kwargs ) : <EOL> self . width_field , self . height_field = width_field , height_field <EOL> super ( ImageField , self ) . __init__ ( verbose_name , name , ** kwargs ) <EOL> def contribute_to_class ( self , cls , name ) : <EOL> super ( ImageField , self ) . contribute_to_class ( cls , name ) <EOL> signals . post_init . connect ( self . update_dimension_fields , sender = cls ) <EOL> def update_dimension_fields ( self , instance , force = False , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> has_dimension_fields = self . width_field or self . height_field <EOL> if not has_dimension_fields : <EOL> return <EOL> file = getattr ( instance , self . attname ) <EOL> if not file and not force : <EOL> return <EOL> dimension_fields_filled = not ( <EOL> ( self . width_field and not getattr ( instance , self . width_field ) ) <EOL> or ( self . height_field and not getattr ( instance , self . height_field ) ) <EOL> ) <EOL> if dimension_fields_filled and not force : <EOL> return <EOL> if file : <EOL> width = file . width <EOL> height = file . height <EOL> else : <EOL> width = None <EOL> height = None <EOL> if self . width_field : <EOL> setattr ( instance , self . width_field , width ) <EOL> if self . height_field : <EOL> setattr ( instance , self . height_field , height ) <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { '<STR_LIT>' : forms . ImageField } <EOL> defaults . update ( kwargs ) <EOL> return super ( ImageField , self ) . formfield ( ** defaults ) </s>
