
 XzL                 @   sq  d  d l  Z  d  d l Z 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 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 Gd d   d e	  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z  Gd d   d e e  Z! Gd d   d e  Z" d S)    N)forms)checks)File)	ImageFile)default_storage)signals)Field)six)RemovedInDjango20Warning)	force_str
force_text)ugettext_lazyc                   sH  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	 d d   Z
 e e e	 e
  Z d d   Z e e  Z d d   Z e e  Z d d   Z e e  Z d d d  Z d e _ d d d  Z d e _ d d d  Z d e _ d d    Z e e  Z d! d"   Z d# d$   Z   S)%	FieldFilec                sD   t  t |   j d  |  | |  _ | |  _ | j |  _ d |  _ d  S)NT)superr   __init__instancefieldstorage
_committed)selfr   r   name)	__class__ K/home/ubuntu/projects/ifolica/build/django/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   |  j  |  S)N)r   )r   r   r   r   r   __ne__"   s    zFieldFile.__ne__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   n  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   sN   |  j    t |  d  s) |  j d  k rG |  j j |  j d  |  _ n  |  j S)N_filerb)r!   r   r"   r   openr   )r   r   r   r   	_get_file0   s    
zFieldFile._get_filec             C   s   | |  _  d  S)N)r"   )r   filer   r   r   	_set_file6   s    zFieldFile._set_filec             C   s
   |  `  d  S)N)r"   )r   r   r   r   	_del_file9   s    zFieldFile._del_filec             C   s   |  j    |  j j |  j  S)N)r!   r   pathr   )r   r   r   r   	_get_path>   s    
zFieldFile._get_pathc             C   s   |  j    |  j j |  j  S)N)r!   r   urlr   )r   r   r   r   _get_urlC   s    
zFieldFile._get_urlc             C   s0   |  j    |  j s |  j j S|  j j |  j  S)N)r!   r   r&   sizer   r   )r   r   r   r   	_get_sizeH   s    
	
zFieldFile._get_sizer#   c             C   s   |  j    |  j j |  d  S)N)r!   r&   r$   )r   moder   r   r   r$   O   s    
zFieldFile.openTc             C   s{   |  j  j |  j |  } |  j j | | d |  j  j |  _ t |  j |  j  j |  j  d |  _ | rw |  j j   n  d  S)N
max_lengthT)	r   generate_filenamer   r   saver0   r   setattrr   )r   r   contentr2   r   r   r   r2   Y   s    $	zFieldFile.savec             C   s   |  s
 d  St  |  d  r, |  j   |  ` n  |  j j |  j  d  |  _ t |  j |  j j |  j  d |  _	 | r |  j j
   n  d  S)Nr"   F)r   closer&   r   deleter   r3   r   r   r   r2   )r   r2   r   r   r   r6   d   s    
			zFieldFile.deletec             C   s%   t  |  d d   } | d  k p$ | j S)Nr"   )getattrclosed)r   r&   r   r   r   _get_closedw   s    zFieldFile._get_closedc             C   s/   t  |  d d   } | d  k	 r+ | j   n  d  S)Nr"   )r7   r5   )r   r&   r   r   r   r5   |   s    zFieldFile.closec             C   s#   i |  j  d 6d d 6d d 6d  d 6S)Nr   Fr8   Tr   r"   )r   )r   r   r   r   __getstate__   s    zFieldFile.__getstate__)__name__
__module____qualname__r   r   r   r   r!   r%   r'   r(   propertyr&   r*   r)   r,   r+   r.   r-   r$   Zalters_datar2   r6   r9   r8   r5   r:   r   r   )r   r   r      s2   				r   c               @   s=   e  Z d  Z d Z d d   Z d d d  Z d d   Z d S)	FileDescriptora  
    The descriptor for the file attribute on the model instance. Returns a
    FieldFile when accessed so you can do stuff like::

        >>> from myapp.models import MyModel
        >>> instance = MyModel.objects.get(pk=1)
        >>> instance.file.size

    Assigns a file object on assignment so you can do::

        >>> with open('/path/to/hello.world', 'r') as f:
        ...     instance.file = File(f)
    c             C   s   | |  _  d  S)N)r   )r   r   r   r   r   r      s    zFileDescriptor.__init__Nc             C   s  | d  k r |  S|  j  j | j k r; | j |  j  j } n. | j d |  j  j g  t | |  j  j  } t | t j  s | d  k r |  j  j | |  j  |  } | | j |  j  j <n t | t	  rt | t
  r|  j  j | |  j  | j  } | | _ d | _ | | j |  j  j <np t | t
  rct | d  rc| | _ |  j  | _  |  j  j | _ n* t | t
  r| | j k	 r| | _ n  | j |  j  j S)NfieldsFr   )r   r   __dict__Zrefresh_from_dbr7   
isinstancer	   string_types
attr_classr   r   r&   r   r   r   r   )r   r   clsr&   attrZ	file_copyr   r   r   __get__   s*    				zFileDescriptor.__get__c             C   s   | | j  |  j j <d  S)N)rA   r   r   )r   r   valuer   r   r   __set__   s    zFileDescriptor.__set__)r;   r<   r=   __doc__r   rG   rI   r   r   r   r   r?      s   :r?   c                   s  e  Z d  Z e Z e Z e d  Z d d d 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 d d   Z d d   Z   f d d   Z   S) 	FileFieldr   N c                sl   d | k |  _  d | k |  _ | p' t |  _ | |  _ | j d d  | d <t t |   j | | |  d  S)NZprimary_keyuniquer0   d   )	_primary_key_set_explicitly_unique_set_explicitlyr   r   	upload_togetr   rK   r   )r   verbose_namer   rQ   r   kwargs)r   r   r   r      s    	zFileField.__init__c                sB   t  t |   j |   } | j |  j    | j |  j    | S)N)r   rK   checkextend_check_unique_check_primary_key)r   rT   errors)r   r   r   rU      s    zFileField.checkc             C   s7   |  j  r/ t j d |  j j d |  d d g Sg  Sd  S)Nz*'unique' is not a valid argument for a %s.objidzfields.E200)rP   r   Errorr   r;   )r   r   r   r   rW      s    	
zFileField._check_uniquec             C   s7   |  j  r/ t j d |  j j d |  d d g Sg  Sd  S)Nz/'primary_key' is not a valid argument for a %s.rZ   r[   zfields.E201)rO   r   r\   r   r;   )r   r   r   r   rX      s    	
zFileField._check_primary_keyc                s|   t  t |   j   \ } } } } | j d  d k r@ | d =n  |  j | d <|  j t k	 rl |  j | d <n  | | | | f S)Nr0   rN   rQ   r   )r   rK   deconstructrR   rQ   r   r   )r   r   r)   argsrT   )r   r   r   r]     s    !
zFileField.deconstructc             C   s   d S)NrK   r   )r   r   r   r   get_internal_type  s    zFileField.get_internal_typec                s5   t  t |   j |  } | d k r( d St j |  S)z:Returns field's value prepared for saving into a database.N)r   rK   get_prep_valuer	   	text_type)r   rH   )r   r   r   r`     s    zFileField.get_prep_valuec                sK   t  t |   j | |  } | rG | j rG | j | j | d d n  | S)z)Returns field's value just before saving.r2   F)r   rK   pre_saver   r2   r   )r   Zmodel_instanceaddr&   )r   r   r   rb     s    zFileField.pre_savec                s<   t  t |   j | | |  t | |  j |  j |    d  S)N)r   rK   contribute_to_classr3   r   descriptor_class)r   rE   r   rT   )r   r   r   rd   '  s    zFileField.contribute_to_classc             C   sG   t  j d t d d t j j t t j j   j	 t
 |  j     S)Nz{FileField now delegates file name and folder processing to the storage. get_directory_name() will be removed in Django 2.0.
stacklevel   )warningswarnr
   osr)   normpathr   datetimenowstrftimer   rQ   )r   r   r   r   get_directory_name+  s    zFileField.get_directory_namec             C   s>   t  j d t d d t j j |  j j t j j |    S)NzuFileField now delegates file name and folder processing to the storage. get_filename() will be removed in Django 2.0.rf   rg   )	rh   ri   r
   rj   r)   rk   r   Zget_valid_namebasename)r   filenamer   r   r   get_filename3  s    zFileField.get_filenamec             C   sm   t  |  j  r$ |  j | |  } n9 t t j j   j t |  j    } t j | |  } |  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).
        )callablerQ   r   rl   rm   rn   r   	posixpathjoinr   r1   )r   r   rq   dirnamer   r   r   r1   ;  s
    'zFileField.generate_filenamec             C   s5   | d  k	 r1 | s d } n  t  | |  j |  n  d  S)NrL   )r3   r   )r   r   datar   r   r   save_form_dataI  s    	zFileField.save_form_datac                sV   i t  j d 6|  j d 6} d | k r3 d | d <n  | j |  t t |   j |   S)N
form_classr0   initialFrequired)r   rK   r0   updater   	formfield)r   rT   defaults)r   r   r   r}   U  s
    zFileField.formfield)r;   r<   r=   r   rD   r?   re   _descriptionr   rU   rW   rX   r]   r_   r`   rb   rd   ro   rr   r1   rx   r}   r   r   )r   r   rK      s"   
	rK   c                   s(   e  Z d  Z d Z   f d d   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                sZ   | j  j |  j j  } t t |   j | |  | d  k	 rV |  j j | d d n  d  S)NforceT)rA   rR   r   r   r   r   rI   update_dimension_fields)r   r   rH   Zprevious_file)r   r   r   rI   g  s    zImageFileDescriptor.__set__)r;   r<   r=   rJ   rI   r   r   )r   r   r   b  s   r   c                   s%   e  Z d  Z d   f d d  Z   S)ImageFieldFileTc                s2   t  |  d  r |  ` n  t t |   j |  d  S)N_dimensions_cache)r   r   r   r   r6   )r   r2   )r   r   r   r6   y  s    	zImageFieldFile.delete)r;   r<   r=   r6   r   r   )r   r   r   x  s   r   c                   s   e  Z d  Z e Z e Z e d  Z d d d 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   S)
ImageFieldImageNc                s3   | | |  _  |  _ t t |   j | | |  d  S)N)width_fieldheight_fieldr   r   r   )r   rS   r   r   r   rT   )r   r   r   r     s    zImageField.__init__c                s/   t  t |   j |   } | j |  j    | S)N)r   r   rU   rV   _check_image_library_installed)r   rT   rY   )r   r   r   rU     s    zImageField.checkc             C   sS   y d d l  m } Wn4 t k
 rJ t j d d d d |  d d g SYn Xg  Sd  S)	Nr   )r   z6Cannot use ImageField because Pillow is not installed.ZhintzVGet Pillow at https://pypi.python.org/pypi/Pillow or run command "pip install Pillow".rZ   r[   zfields.E210)ZPILr   ImportErrorr   r\   )r   r   r   r   r   r     s    z)ImageField._check_image_library_installedc                sc   t  t |   j   \ } } } } |  j r: |  j | d <n  |  j rS |  j | d <n  | | | | f S)Nr   r   )r   r   r]   r   r   )r   r   r)   r^   rT   )r   r   r   r]     s    !		zImageField.deconstructc                sH   t  t |   j | | |  | j j sD t j j |  j d | n  d  S)NZsender)	r   r   rd   Z_metaZabstractr   Z	post_initconnectr   )r   rE   r   rT   )r   r   r   rd     s    zImageField.contribute_to_classFc       
      O   s  |  j  p |  j } | s+ |  j | j k r/ d St | |  j  } | rS | rS d S|  j  ro t | |  j   p |  j o t | |  j  } | r | r d S| r | j } | j }	 n d } d }	 |  j  r t | |  j  |  n  |  j rt | |  j |	  n  d S)a  
        Updates 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)r   r   ZattnamerA   r7   widthheightr3   )
r   r   r   r^   rT   Zhas_dimension_fieldsr&   Zdimension_fields_filledr   r   r   r   r   r     s&    				z"ImageField.update_dimension_fieldsc                s3   i t  j d 6} | j |  t t |   j |   S)Nry   )r   r   r|   r   r}   )r   rT   r~   )r   r   r   r}     s    zImageField.formfield)r;   r<   r=   r   rD   r   re   r   r   r   rU   r   r]   rd   r   r}   r   r   )r   r   r     s   	9r   )#rl   rj   rt   rh   Zdjangor   Zdjango.corer   Zdjango.core.files.baser   Zdjango.core.files.imagesr   Zdjango.core.files.storager   Zdjango.db.modelsr   Zdjango.db.models.fieldsr   Zdjango.utilsr	   Zdjango.utils.deprecationr
   Zdjango.utils.encodingr   r   Zdjango.utils.translationr   r   r   objectr?   rK   r   r   r   r   r   r   r   <module>   s(   vO