o aG@sddlZddlZddlmZddlmZddlmZddlm Z ddl m Z m Z ddl mZddlmZdd lmZdd lmZdd lmZGd d d eZGdddeZGdddeZGdddeZGddde eZGdddeZdS)N)forms)checks)File) ImageFile)Storagedefault_storage)validate_file_name)signals)Field)DeferredAttribute) gettext_lazycseZdZfddZddZddZddZd d Zd d Zd dZ e eee Z e ddZ e ddZ e ddZd%ddZde_d&ddZde_d&ddZde_e ddZdd Zd!d"Zd#d$ZZS)' FieldFilecs,td|||_||_|j|_d|_dS)NT)super__init__instancefieldstorage _committed)selfrrname __class__?/usr/lib/python3/dist-packages/django/db/models/fields/files.pyrs  zFieldFile.__init__cCs t|dr |j|jkS|j|kS)Nr)hasattrr)rotherrrr__eq__s   zFieldFile.__eq__cCs t|jSN)hashrrrrr__hash__ zFieldFile.__hash__cCs|s td|jjdS)Nz2The '%s' attribute has no file associated with it.) ValueErrorrrrrrr _require_file&szFieldFile._require_filecCs0|t|dddur|j|jd|_|jS)N_filerb)r#getattrropenrr$rrrr _get_file*szFieldFile._get_filecCs ||_dSrr$rfilerrr _set_file0r!zFieldFile._set_filecCs|`dSrr)rrrr _del_file3szFieldFile._del_filecC||j|jSr)r#rpathrrrrrr/8zFieldFile.pathcCr.r)r#rurlrrrrrr1=r0z FieldFile.urlcCs$||js |jjS|j|jSr)r#rr+sizerrrrrrr2BszFieldFile.sizer%cCs>|t|dddur|j|j||_|S|j||SNr$)r#r&rr'rr+)rmoderrrr'Is  zFieldFile.openTcCsX|j|j|}|jj|||jjd|_t|j|jj|jd|_ |r*|jdSdS)N) max_lengthT) rgenerate_filenamerrsaver5rsetattrattnamer)rrcontentr7rrrr7WszFieldFile.savecCsb|sdSt|dr||`|j|jd|_t|j|jj |jd|_ |r/|j dSdS)Nr$F) rcloser+rdeleterr8rrr9rr7rr7rrrr<bs zFieldFile.deletecCst|dd}|dup |jSr3)r&closedr*rrrr>us zFieldFile.closedcCs$t|dd}|dur|dSdSr3)r&r;r*rrrr;zs  zFieldFile.closecCs|jddd|j|jdS)NFT)rr>rr$rr)rrrrrrr __getstate__szFieldFile.__getstate__cCs|j||jj|_dSr)__dict__updaterr)rstaterrr __setstate__s zFieldFile.__setstate__)r%T)__name__ __module__ __qualname__rrr r#r(r,r-propertyr+r/r1r2r' alters_datar7r<r>r;r?rC __classcell__rrrrr s4          r cs*eZdZdZdfdd ZddZZS)FileDescriptora The descriptor for the file attribute on the model instance. Return a FieldFile when accessed so you can write code like:: >>> from myapp.models import MyModel >>> instance = MyModel.objects.get(pk=1) >>> instance.file.size Assign a file object on assignment so you can do:: >>> with open('/path/to/hello.world') as f: ... instance.file = File(f) Ncs|dur|St||}t|ts|dur'|j||j|}||j|jj<nFt|trIt|t sI|j||j|j }||_ d|_ ||j|jj<n$t|t r`t |ds`||_|j|_|jj|_n t|t rm||jurm||_|j|jjS)NFr)r__get__ isinstancestrr attr_classr@r9rr rr+rrrr)rrclsr+attr file_copyrrrrLs$  zFileDescriptor.__get__cCs||j|jj<dSr)r@rr9)rrvaluerrr__set__szFileDescriptor.__set__r)rErFrG__doc__rLrTrJrrrrrKs 6rKcseZdZeZeZedZdfdd Z fddZ dd Z d d Z fd d Z ddZfddZfddZfddZddZddZfddZZS) FileFieldrNc sd|v|_|pt|_t|jr+|j|_||_t|jts+td|jj tj tj f||_ | ddt j||fi|dS)N primary_keyz/%s.storage must be a subclass/instance of %s.%sr5d)_primary_key_set_explicitlyrrcallable_storage_callablerMr TypeErrorrrGrF upload_to setdefaultrr)r verbose_namerr^rkwargsrrrrs      zFileField.__init__c s&gtjdi|||SNr)rcheck_check_primary_key_check_upload_torrarrrrcszFileField.checkcCs$|jrtjd|jj|ddgSgS)Nz/'primary_key' is not a valid argument for a %s.z fields.E201)objid)rZrErrorrrErrrrrds zFileField._check_primary_keycCs8t|jtr|jdrtjd|jj|dddgSgS)N/zH%s's 'upload_to' argument must be a relative path, not an absolute path.z fields.E202zRemove the leading slash.)rgrhhint)rMr^rN startswithrrirrErrrrre s zFileField._check_upload_tocsXt\}}}}|ddkr|d=|j|d<|jtur&t|d|j|d<||||fS)Nr5rYr^r\r)r deconstructgetr^rrr&rrr/argsrarrrrms   zFileField.deconstructcCsdS)NrVrrrrrget_internal_type szFileField.get_internal_typecs t|}|dur dSt|Sr)rget_prep_valuerN)rrSrrrrr#s zFileField.get_prep_valuecs0t||}|r|js|j|j|jdd|S)NF)r7)rpre_saverr7rr+)rmodel_instanceaddr+rrrrs*s zFileField.pre_savec s.tj||fi|t||j||dSr)rcontribute_to_classr8r9descriptor_classrrPrrarrrrv1szFileField.contribute_to_classcCsRt|jr |||}ntjt|j}t||}t|dd}|j |S)a Apply (if callable) or prepend (if a string) upload_to to the filename, then delegate further processing of the name to the storage backend. Until the storage layer, all file paths are expected to be Unix style (with forward slashes). T)allow_relative_path) r[r^datetimenowstrftimerN posixpathjoinrrr6)rrfilenamedirnamerrrr65s    zFileField.generate_filenamecCs"|durt||j|p ddSdS)NrW)r8r)rrdatarrrsave_form_dataDszFileField.save_form_datac s tjditj|jd|S)N) form_classr5r)r formfieldrrVr5rfrrrrNs zFileField.formfield)NNrWN)rErFrGr rOrKrw_ descriptionrrcrdrermrqrrrsrvr6rrrJrrrrrVs       rVcs eZdZdZfddZZS)ImageFileDescriptorz Just like the FileDescriptor, but for ImageFields. The only difference is assigning the width/height to the width_field/height_field, if appropriate. cs>|j|jj}t|||dur|jj|dddSdS)NT)force)r@rnrr9rrTupdate_dimension_fields)rrrS previous_filerrrrT[s  zImageFileDescriptor.__set__)rErFrGrUrTrJrrrrrVsrcseZdZdfdd ZZS)ImageFieldFileTcst|dr|`t|dS)N_dimensions_cache)rrrr<r=rrrr<ms zImageFieldFile.deleterD)rErFrGr<rJrrrrrlsrcspeZdZeZeZedZdfdd Z fddZ ddZ fd d Z fd d Z dddZfddZZS) ImageFieldImageNc s(|||_|_tj||fi|dSr) width_field height_fieldrr)rr`rrrrarrrryszImageField.__init__c sgtjdi||Srb)rrc_check_image_library_installedrfrrrrc}s zImageField.checkc Cs:z ddlm}WgStytjdd|ddgYSw)Nr)rz6Cannot use ImageField because Pillow is not installed.z]Get Pillow at https://pypi.org/project/Pillow/ or run command "python -m pip install Pillow".z fields.E210)rkrgrh)PILr ImportErrorrri)rrrrrrs z)ImageField._check_image_library_installedcs>t\}}}}|jr|j|d<|jr|j|d<||||fS)Nrr)rrmrrrorrrrms    zImageField.deconstructc s8tj||fi||jjstjj|j|ddSdS)N)sender)rrv_metaabstractr post_initconnectrrxrrrrvszImageField.contribute_to_classFc Os|jp|j}|r|j|jvrdSt||j}|s|sdS|jr&t||j p/|jo/t||j }|r7|s7dS|r@|j}|j} nd}d} |jrNt||j||jrZt||j| dSdS)a Update field's width and height fields, if defined. This method is hooked up to model's post_init signal to update dimensions after instantiating a model instance. However, dimensions won't be updated if the dimensions fields are already populated. This avoids unnecessary recalculation when loading an object from the database. Dimensions can be forced to update with force=True, which is how ImageFileDescriptor.__set__ calls this method. N)rrr9r@r&widthheightr8) rrrrprahas_dimension_fieldsr+dimension_fields_filledrrrrrrs*   z"ImageField.update_dimension_fieldsc stjdidtji|S)Nrr)rrrrrfrrrrs zImageField.formfield)NNNN)F)rErFrGrrOrrwrrrrcrrmrvrrrJrrrrrts    9r)rzr}djangor django.corerdjango.core.files.baserdjango.core.files.imagesrdjango.core.files.storagerrdjango.core.files.utilsrdjango.db.modelsr django.db.models.fieldsr django.db.models.query_utilsr django.utils.translationr rr rKrVrrrrrrrs&         H}