3
gG                 @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ G dd deZG dd deZG dd deZG dd deZG dd de	eZG dd deZdS )    N)forms)checks)File)	ImageFile)Storagedefault_storage)validate_file_name)signals)Field)DeferredAttribute)gettext_lazyc                   s   e Zd Z fddZdd Zdd Zdd Zd	d
 Zdd Zdd Z	e
eee	Ze
dd Ze
dd Ze
dd Zd%ddZde_d&ddZde_d'ddZde_e
dd Zdd  Zd!d" Zd#d$ Z  ZS )(	FieldFilec                s,   t  jd | || _|| _|j| _d| _d S )NT)super__init__instancefieldstorage
_committed)selfr   r   name)	__class__ [/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django/db/models/fields/files.pyr      s
    zFieldFile.__init__c             C   s    t |dr| j|jkS | j|kS )Nr   )hasattrr   )r   otherr   r   r   __eq__   s    
zFieldFile.__eq__c             C   s
   t | jS )N)hashr   )r   r   r   r   __hash__   s    zFieldFile.__hash__c             C   s   | st d| jj d S )Nz2The '%s' attribute has no file associated with it.)
ValueErrorr   r   )r   r   r   r   _require_file&   s    zFieldFile._require_filec             C   s0   | j   t| dd d kr*| jj| jd| _| jS )N_filerb)r   getattrr   openr   r    )r   r   r   r   	_get_file*   s    zFieldFile._get_filec             C   s
   || _ d S )N)r    )r   filer   r   r   	_set_file0   s    zFieldFile._set_filec             C   s   | ` d S )N)r    )r   r   r   r   	_del_file3   s    zFieldFile._del_filec             C   s   | j   | jj| jS )N)r   r   pathr   )r   r   r   r   r(   8   s    zFieldFile.pathc             C   s   | j   | jj| jS )N)r   r   urlr   )r   r   r   r   r)   =   s    zFieldFile.urlc             C   s$   | j   | js| jjS | jj| jS )N)r   r   r%   sizer   r   )r   r   r   r   r*   B   s    zFieldFile.sizer!   c             C   s<   | j   t| dd d kr,| jj| j|| _n| jj| | S )Nr    )r   r"   r   r#   r   r%   )r   moder   r   r   r#   I   s
    zFieldFile.openTc             C   sT   | j j| j|}| jj||| j jd| _t| j| j j| j d| _	|rP| jj  d S )N)
max_lengthT)
r   generate_filenamer   r   saver,   r   setattrattnamer   )r   r   contentr.   r   r   r   r.   W   s    zFieldFile.savec             C   s^   | sd S t | dr| j  | `| jj| j d | _t| j| jj	| j d| _
|rZ| jj  d S )Nr    F)r   closer%   r   deleter   r/   r   r   r0   r   r.   )r   r.   r   r   r   r3   b   s    
zFieldFile.deletec             C   s   t | dd }|d kp|jS )Nr    )r"   closed)r   r%   r   r   r   r4   u   s    zFieldFile.closedc             C   s    t | dd }|d k	r|j  d S )Nr    )r"   r2   )r   r%   r   r   r   r2   z   s    zFieldFile.closec             C   s   | j ddd | j| jdS )NFT)r   r4   r   r    r   r   )r   r   r   )r   r   r   r   __getstate__   s    zFieldFile.__getstate__c             C   s   | j j| | jj| _d S )N)__dict__updater   r   )r   stater   r   r   __setstate__   s    zFieldFile.__setstate__)r!   )T)T)__name__
__module____qualname__r   r   r   r   r$   r&   r'   propertyr%   r(   r)   r*   r#   Zalters_datar.   r3   r4   r2   r5   r9   __classcell__r   r   )r   r   r      s*   

	
r   c                   s*   e Zd ZdZd fdd	Zdd Z  ZS )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)
    Nc                s   |d kr| S t  j||}t|ts,|d krN| jj|| j|}||j| jj< nt|trt|t	 r| jj|| j|j
}||_d|_||j| jj< nJt|t	rt|d r||_| j|_| jj|_nt|t	r||jk	r||_|j| jj S )NFr   )r   __get__
isinstancestrr   
attr_classr6   r0   r   r   r   r%   r   r   r   r   )r   r   clsr%   attrZ	file_copy)r   r   r   r@      s$    	zFileDescriptor.__get__c             C   s   ||j | jj< d S )N)r6   r   r0   )r   r   valuer   r   r   __set__   s    zFileDescriptor.__set__)N)r:   r;   r<   __doc__r@   rG   r>   r   r   )r   r   r?      s   6r?   c                   s   e Zd ZeZeZedZd fdd	Z	 fddZ
dd	 Zd
d Z fddZdd Z fddZ fddZ fddZdd Zdd Z fddZ  ZS )	FileFieldr   N c                s~   d|k| _ |pt| _t| jrV| j| _| j | _t| jtsVtd| jj	tj
tj	f || _|jdd t j||f| d S )NZprimary_keyz/%s.storage must be a subclass/instance of %s.%sr,   d   )_primary_key_set_explicitlyr   r   callable_storage_callablerA   r   	TypeErrorr   r<   r;   	upload_to
setdefaultr   r   )r   verbose_namer   rP   r   kwargs)r   r   r   r      s    



zFileField.__init__c                s   t  jf || j | j S )N)r   check_check_primary_key_check_upload_to)r   rS   )r   r   r   rT      s    zFileField.checkc             C   s(   | j r tjd| jj | ddgS g S d S )Nz/'primary_key' is not a valid argument for a %s.zfields.E201)objid)rL   r   Errorr   r:   )r   r   r   r   rU      s    

zFileField._check_primary_keyc             C   s<   t | jtr4| jjdr4tjd| jj | dddgS g S d S )N/zH%s's 'upload_to' argument must be a relative path, not an absolute path.zfields.E202zRemove the leading slash.)rW   rX   hint)rA   rP   rB   
startswithr   rY   r   r:   )r   r   r   r   rV   	  s    
zFileField._check_upload_toc                sX   t  j \}}}}|jddkr&|d= | j|d< | jtk	rLt| d| j|d< ||||fS )Nr,   rK   rP   rN   r   )r   deconstructgetrP   r   r   r"   )r   r   r(   argsrS   )r   r   r   r]     s    

zFileField.deconstructc             C   s   dS )NrI   r   )r   r   r   r   get_internal_type   s    zFileField.get_internal_typec                s    t  j|}|d krd S t|S )N)r   get_prep_valuerB   )r   rF   )r   r   r   ra   #  s    zFileField.get_prep_valuec                s2   t  j||}|r.|j r.|j|j|jdd |S )NF)r.   )r   pre_saver   r.   r   r%   )r   Zmodel_instanceaddr%   )r   r   r   rb   *  s    zFileField.pre_savec                s*   t  j||f| t|| j| j|  d S )N)r   contribute_to_classr/   r0   descriptor_class)r   rD   r   rS   )r   r   r   rd   1  s    zFileField.contribute_to_classc             C   sR   t | jr| j||}n"tjj jt| j}tj||}t|dd}| j	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)Zallow_relative_path)rM   rP   datetimenowstrftimerB   	posixpathjoinr   r   r-   )r   r   filenamedirnamer   r   r   r-   5  s    
zFileField.generate_filenamec             C   s   |d k	rt || j|pd d S )NrJ   )r/   r   )r   r   datar   r   r   save_form_dataD  s    zFileField.save_form_datac                s   t  jf tj| jd|S )N)
form_classr,   )r   	formfieldr   rI   r,   )r   rS   )r   r   r   rp   N  s    zFileField.formfield)NNrJ   N)r:   r;   r<   r   rC   r?   re   _descriptionr   rT   rU   rV   r]   r`   ra   rb   rd   r-   rn   rp   r>   r   r   )r   r   rI      s   	
rI   c                   s    e Zd ZdZ fddZ  ZS )ImageFileDescriptorz
    Just like the FileDescriptor, but for ImageFields. The only difference is
    assigning the width/height to the width_field/height_field, if appropriate.
    c                s:   |j j| jj}t j|| |d k	r6| jj|dd d S )NT)force)r6   r^   r   r0   r   rG   update_dimension_fields)r   r   rF   Zprevious_file)r   r   r   rG   [  s    zImageFileDescriptor.__set__)r:   r;   r<   rH   rG   r>   r   r   )r   r   rs   V  s   rs   c                   s   e Zd Zd fdd	Z  ZS )ImageFieldFileTc                s   t | dr| `t j| d S )N_dimensions_cache)r   rw   r   r3   )r   r.   )r   r   r   r3   m  s    
zImageFieldFile.delete)T)r:   r;   r<   r3   r>   r   r   )r   r   rv   l  s   rv   c                   sp   e Zd ZeZeZedZd fdd	Z	 fddZ
dd Z fd	d
Z fddZdddZ fddZ  ZS )
ImageFieldImageNc                s$   || | _ | _t j||f| d S )N)width_fieldheight_fieldr   r   )r   rR   r   rz   r{   rS   )r   r   r   r   y  s    zImageField.__init__c                s   t  jf || j S )N)r   rT   _check_image_library_installed)r   rS   )r   r   r   rT   }  s    zImageField.checkc             C   s>   yddl m} W n$ tk
r4   tjdd| ddgS X g S d S )Nr   )ry   z6Cannot 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)r[   rW   rX   )ZPILry   ImportErrorr   rY   )r   ry   r   r   r   r|     s    z)ImageField._check_image_library_installedc                s>   t  j \}}}}| jr"| j|d< | jr2| j|d< ||||fS )Nrz   r{   )r   r]   rz   r{   )r   r   r(   r_   rS   )r   r   r   r]     s    

zImageField.deconstructc                s0   t  j||f| |jjs,tjj| j|d d S )N)Zsender)r   rd   Z_metaZabstractr	   Z	post_initconnectru   )r   rD   r   rS   )r   r   r   rd     s    zImageField.contribute_to_classFc       
      O   s   | j p
| j}| s| j|jkr"dS t|| j}| r>| r>dS | j rRt|| j  pd| jodt|| j  }|rv| rvdS |r|j}|j}	nd}d}	| j rt|| j | | jrt|| j|	 dS )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)rz   r{   r0   r6   r"   widthheightr/   )
r   r   rt   r_   rS   Zhas_dimension_fieldsr%   Zdimension_fields_filledr   r   r   r   r   ru     s&    	
z"ImageField.update_dimension_fieldsc                s   t  jf dtji|S )Nro   )r   rp   r   rx   )r   rS   )r   r   r   rp     s    zImageField.formfield)NNNN)F)r:   r;   r<   rv   rC   rs   re   rq   rr   r   rT   r|   r]   rd   ru   rp   r>   r   r   )r   r   rx   t  s   	
9rx   )rf   ri   Zdjangor   Zdjango.corer   Zdjango.core.files.baser   Zdjango.core.files.imagesr   Zdjango.core.files.storager   r   Zdjango.core.files.utilsr   Zdjango.db.modelsr	   Zdjango.db.models.fieldsr
   Zdjango.db.models.query_utilsr   Zdjango.utils.translationr   rq   r   r?   rI   rs   rv   rx   r   r   r   r   <module>   s$    H}