ó
®â0_c           @   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 d e	 f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ  d e e f d „  ƒ  YZ! d e f d „  ƒ  YZ" d S(   iÿÿÿÿN(   t   forms(   t   checks(   t   File(   t	   ImageFile(   t   default_storage(   t   signals(   t   Field(   t   six(   t   RemovedInDjango20Warning(   t	   force_strt
   force_text(   t   ugettext_lazyt	   FieldFilec           B   sï   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 e
 e e e	 ƒ Z e
 d „  ƒ Z e
 d	 „  ƒ Z e
 d
 „  ƒ Z d d „ Z e e _ e d „ Z e e _ e d „ Z e e _ e
 d „  ƒ Z d „  Z d „  Z RS(   c         C   sD   t  t |  ƒ j d  | ƒ | |  _ | |  _ | j |  _ t |  _ d  S(   N(	   t   superR   t   __init__t   Nonet   instancet   fieldt   storaget   Truet
   _committed(   t   selfR   R   t   name(    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR      s
    		c         C   s,   t  | d ƒ r |  j | j k S|  j | k S(   NR   (   t   hasattrR   (   R   t   other(    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyt   __eq__   s    c         C   s   |  j  | ƒ S(   N(   R   (   R   R   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyt   __ne__"   s    c         C   s   t  |  j ƒ S(   N(   t   hashR   (   R   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyt   __hash__%   s    c         C   s#   |  s t  d |  j j ƒ ‚ n  d  S(   Ns2   The '%s' attribute has no file associated with it.(   t
   ValueErrorR   R   (   R   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyt   _require_file,   s    c         C   sN   |  j  ƒ  t |  d ƒ s) |  j d  k rG |  j j |  j d ƒ |  _ n  |  j S(   Nt   _filet   rb(   R   R   R   R   R   t   openR   (   R   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyt	   _get_file0   s    
c         C   s   | |  _  d  S(   N(   R   (   R   t   file(    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyt	   _set_file6   s    c         C   s
   |  `  d  S(   N(   R   (   R   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyt	   _del_file9   s    c         C   s   |  j  ƒ  |  j j |  j ƒ S(   N(   R   R   t   pathR   (   R   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR&   >   s    
c         C   s   |  j  ƒ  |  j j |  j ƒ S(   N(   R   R   t   urlR   (   R   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR'   C   s    
c         C   s0   |  j  ƒ  |  j s |  j j S|  j j |  j ƒ S(   N(   R   R   R#   t   sizeR   R   (   R   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR(   H   s    
	
R    c         C   sZ   |  j  ƒ  t |  d ƒ r; |  j d  k	 r; |  j j | ƒ n |  j j |  j | ƒ |  _ d  S(   NR   (   R   R   R   R   R#   R!   R   R   (   R   t   mode(    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR!   O   s    
c         C   s{   |  j  j |  j | ƒ } |  j j | | d |  j  j ƒ|  _ t |  j |  j  j |  j ƒ t |  _	 | rw |  j j ƒ  n  d  S(   Nt
   max_length(
   R   t   generate_filenameR   R   t   saveR*   R   t   setattrR   R   (   R   R   t   contentR,   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR,   \   s    $	c         C   s‡   |  s
 d  St  |  d ƒ r, |  j ƒ  |  ` n  |  j j |  j ƒ d  |  _ t |  j |  j	 j |  j ƒ t
 |  _ | rƒ |  j j ƒ  n  d  S(   NR   (   R   t   closeR#   R   t   deleteR   R   R-   R   R   t   FalseR   R,   (   R   R,   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR0   g   s    
			c         C   s%   t  |  d d  ƒ } | d  k p$ | j S(   NR   (   t   getattrR   t   closed(   R   R#   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR3   z   s    c         C   s/   t  |  d d  ƒ } | d  k	 r+ | j ƒ  n  d  S(   NR   (   R2   R   R/   (   R   R#   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR/      s    c         C   s#   i |  j  d 6t d 6t d 6d  d 6S(   NR   R3   R   R   (   R   R1   R   R   (   R   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyt   __getstate__„   s    (   t   __name__t
   __module__R   R   R   R   R   R"   R$   R%   t   propertyR#   R&   R'   R(   R!   R   t   alters_dataR,   R0   R3   R/   R4   (    (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR      s*   													t   FileDescriptorc           B   s,   e  Z d  Z d „  Z d d „ Z d „  Z RS(   s   
    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   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR   š   s    c         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 ƒ } | | _ t | _ | | 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(   Nt   fieldsR   (   R   R   R   t   __dict__t   refresh_from_dbR2   t
   isinstanceR   t   string_typest
   attr_classR   R   R#   R1   R   R   R   R   (   R   R   t   clsR#   t   attrt	   file_copy(    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyt   __get__   s*    				c         C   s   | | j  |  j j <d  S(   N(   R;   R   R   (   R   R   t   value(    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyt   __set__×   s    N(   R5   R6   t   __doc__R   R   RC   RE   (    (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR9   Œ   s   	:t	   FileFieldc           B   sª   e  Z e Z e Z e d  ƒ Z d d d d d „ Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   R   t    c         K   s]   d | k |  _  | p t |  _ | |  _ | j d d ƒ | d <t t |  ƒ j | | |  d  S(   Nt   primary_keyR*   id   (   t   _primary_key_set_explicitlyR   R   t	   upload_tot   getR   RG   R   (   R   t   verbose_nameR   RK   R   t   kwargs(    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR   æ   s
    	c         K   sB   t  t |  ƒ j |   } | j |  j ƒ  ƒ | j |  j ƒ  ƒ | S(   N(   R   RG   t   checkt   extendt   _check_primary_keyt   _check_upload_to(   R   RN   t   errors(    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyRO   ï   s    c         C   s7   |  j  r/ t j d |  j j d |  d d ƒg Sg  Sd  S(   Ns/   'primary_key' is not a valid argument for a %s.t   objt   ids   fields.E201(   RJ   R   t   Errort	   __class__R5   (   R   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyRQ   õ   s    	
c         C   s[   t  |  j t j ƒ rS |  j j d ƒ rS t j d |  j j d |  d d d d ƒg Sg  Sd  S(   Nt   /sH   %s's 'upload_to' argument must be a relative path, not an absolute path.RT   RU   s   fields.E202t   hints   Remove the leading slash.(	   R=   RK   R   R>   t
   startswithR   RV   RW   R5   (   R   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyRR     s    '
c         C   s|   t  t |  ƒ j ƒ  \ } } } } | j d ƒ d k r@ | d =n  |  j | d <|  j t k	 rl |  j | d <n  | | | | f S(   NR*   id   RK   R   (   R   RG   t   deconstructRL   RK   R   R   (   R   R   R&   t   argsRN   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR[     s    !
c         C   s   d S(   NRG   (    (   R   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyt   get_internal_type  s    c         C   s5   t  t |  ƒ j | ƒ } | d k r( d St j | ƒ S(   s:   Returns field's value prepared for saving into a database.N(   R   RG   t   get_prep_valueR   R   t	   text_type(   R   RD   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR^     s    c         C   sN   t  t |  ƒ j | | ƒ } | rJ | j rJ | j | j | j d t ƒn  | S(   s)   Returns field's value just before saving.R,   (   R   RG   t   pre_saveR   R,   R   R#   R1   (   R   t   model_instancet   addR#   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR`   #  s    c         K   s<   t  t |  ƒ j | | |  t | |  j |  j |  ƒ ƒ d  S(   N(   R   RG   t   contribute_to_classR-   R   t   descriptor_class(   R   R@   R   RN   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyRc   +  s    c         C   sG   t  j d t d d ƒt j j t t j j ƒ  j	 t
 |  j ƒ ƒ ƒ ƒ S(   Ns{   FileField now delegates file name and folder processing to the storage. get_directory_name() will be removed in Django 2.0.t
   stackleveli   (   t   warningst   warnR   t   osR&   t   normpathR
   t   datetimet   nowt   strftimeR	   RK   (   R   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyt   get_directory_name/  s    c         C   s>   t  j d t d d ƒt j j |  j j t j j | ƒ ƒ ƒ S(   Nsu   FileField now delegates file name and folder processing to the storage. get_filename() will be removed in Django 2.0.Re   i   (	   Rf   Rg   R   Rh   R&   Ri   R   t   get_valid_namet   basename(   R   t   filename(    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyt   get_filename7  s    c         C   sm   t  |  j ƒ r$ |  j | | ƒ } n9 t t j j ƒ  j t |  j ƒ ƒ ƒ } t j | | ƒ } |  j	 j
 | ƒ S(   s  
        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   callableRK   R
   Rj   Rk   Rl   R	   t	   posixpatht   joinR   R+   (   R   R   Rp   t   dirname(    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR+   ?  s
    'c         C   s5   | d  k	 r1 | s d } n  t | |  j | ƒ n  d  S(   NRH   (   R   R-   R   (   R   R   t   data(    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyt   save_form_dataM  s    	c         K   sV   i t  j d 6|  j d 6} d | k r3 t | d <n  | j | ƒ t t |  ƒ j |   S(   Nt
   form_classR*   t   initialt   required(   R    RG   R*   R1   t   updateR   t	   formfield(   R   RN   t   defaults(    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR|   Y  s
    N(   R5   R6   R   R?   R9   Rd   t   _t   descriptionR   R   RO   RQ   RR   R[   R]   R^   R`   Rc   Rm   Rq   R+   Rw   R|   (    (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyRG   Û   s"   														t   ImageFileDescriptorc           B   s   e  Z d  Z d „  Z RS(   s£   
    Just like the FileDescriptor, but for ImageFields. The only difference is
    assigning the width/height to the width_field/height_field, if appropriate.
    c         C   sZ   | j  j |  j j ƒ } t t |  ƒ j | | ƒ | d  k	 rV |  j j | d t	 ƒn  d  S(   Nt   force(
   R;   RL   R   R   R   R€   RE   R   t   update_dimension_fieldsR   (   R   R   RD   t   previous_file(    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyRE   k  s    (   R5   R6   RF   RE   (    (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR€   f  s   t   ImageFieldFilec           B   s   e  Z e d  „ Z RS(   c         C   s2   t  |  d ƒ r |  ` n  t t |  ƒ j | ƒ d  S(   Nt   _dimensions_cache(   R   R…   R   R„   R0   (   R   R,   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR0   }  s    	(   R5   R6   R   R0   (    (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR„   |  s   t
   ImageFieldc           B   sn   e  Z e Z e Z e d  ƒ Z d d d d d „ Z	 d „  Z
 d „  Z d „  Z d „  Z e d „ Z d „  Z RS(	   t   Imagec         K   s3   | | |  _  |  _ t t |  ƒ j | | |  d  S(   N(   t   width_fieldt   height_fieldR   R†   R   (   R   RM   R   Rˆ   R‰   RN   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR   ‰  s    c         K   s/   t  t |  ƒ j |   } | j |  j ƒ  ƒ | S(   N(   R   R†   RO   RP   t   _check_image_library_installed(   R   RN   RS   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyRO     s    c         C   sO   y d d l  m } Wn0 t k
 rF t j d d d d |  d d ƒg SXg  Sd  S(	   Niÿÿÿÿ(   R‡   s6   Cannot use ImageField because Pillow is not installed.RY   sV   Get Pillow at https://pypi.python.org/pypi/Pillow or run command "pip install Pillow".RT   RU   s   fields.E210(   t   PILR‡   t   ImportErrorR   RV   (   R   R‡   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyRŠ   ’  s    c         C   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\   RN   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR[   ¢  s    !		c         K   sH   t  t |  ƒ j | | |  | j j sD t j j |  j d | ƒn  d  S(   Nt   sender(	   R   R†   Rc   t   _metat   abstractR   t	   post_initt   connectR‚   (   R   R@   R   RN   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyRc   ª  s    c   
      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(   s  
        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‰   t   attnameR;   R2   t   widtht   heightR   R-   (
   R   R   R   R\   RN   t   has_dimension_fieldsR#   t   dimension_fields_filledR“   R”   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR‚   ³  s&    				c         K   s3   i t  j d 6} | j | ƒ t t |  ƒ j |   S(   NRx   (   R    R†   R{   R   R|   (   R   RN   R}   (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR|   ì  s    N(   R5   R6   R„   R?   R€   Rd   R~   R   R   R   RO   RŠ   R[   Rc   R1   R‚   R|   (    (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyR†   „  s   					9(#   Rj   Rh   Rs   Rf   t   djangoR    t   django.coreR   t   django.core.files.baseR   t   django.core.files.imagesR   t   django.core.files.storageR   t   django.db.modelsR   t   django.db.models.fieldsR   t   django.utilsR   t   django.utils.deprecationR   t   django.utils.encodingR	   R
   t   django.utils.translationR   R~   R   t   objectR9   RG   R€   R„   R†   (    (    (    s?   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/fields/files.pyt   <module>   s(   yO‹