ó
¸9—^c           @   s  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 y d  d l m Z Wn e k
 r§ e d ƒ ‚ n Xd	 e f d
 „  ƒ  YZ d e	 j f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   forms(   t   settings(   t   ImproperlyConfigured(   t   models(   t   RemovedInNextVersionWarning(   t   keyczarsj   Using an encrypted field requires the Keyczar module. You can obtain Keyczar from http://www.keyczar.org/.t   EncryptionWarningc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/db/fields/encrypted.pyR      s   t   BaseEncryptedFieldc           B   sk   e  Z d  Z d „  Z d „  Z d „  Z d „  Z e j d
 k  rK d „  Z	 n	 d „  Z	 e
 d „ Z d	 „  Z RS(   s
   enc_str:::c         O   sª   t  j d t d d ƒt t d d  ƒ s7 t d ƒ ‚ n  |  j ƒ  } | j t j	 ƒ |  _
 | j d d  ƒ |  _ } | r |  j | ƒ | d <n  t t |  ƒ j | | Ž  d  S(   NsY  Please do not use these fields. Since Keyczar is deprecated and authors deleted all code we highly recommend to stop using potentially unsafe abandonware directly these versions of encrypted fields will be removed soon but it's highly likely their names will be reused. Please see github issues/1359 for discussion on possible replacement fieldst
   stackleveli   t   ENCRYPTED_FIELD_KEYS_DIRsZ   You must set the settings.ENCRYPTED_FIELD_KEYS_DIR setting to your Keyczar keys directory.t
   max_length(   t   warningst   warnR   t   getattrR   t   NoneR   t   get_crypt_classt   ReadR   t   cryptt   gett   unencrypted_lengtht   calculate_crypt_max_lengtht   superR	   t   __init__(   t   selft   argst   kwargst   crypt_classR   (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/db/fields/encrypted.pyR      s    c         C   s'   t  |  j ƒ t  |  j j d | ƒ ƒ S(   Nt   x(   t   lent   prefixR   t   Encrypt(   R   R   (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/db/fields/encrypted.pyR   2   s    c         C   sY   t  t d d ƒ } | d k r' d } n% | d k r< d } n t d | ƒ ‚ t  t | ƒ S(   s  
        Get the Keyczar class to use.

        The class can be customized with the ENCRYPTED_FIELD_MODE setting. By default,
        this setting is DECRYPT_AND_ENCRYPT. Set this to ENCRYPT to disable decryption.
        This is necessary if you are only providing public keys to Keyczar.

        Returns:
            keyczar.Encrypter if ENCRYPTED_FIELD_MODE is ENCRYPT.
            keyczar.Crypter if ENCRYPTED_FIELD_MODE is DECRYPT_AND_ENCRYPT.

        Override this method to customize the type of Keyczar class returned.
        t   ENCRYPTED_FIELD_MODEt   DECRYPT_AND_ENCRYPTt   ENCRYPTt	   Encryptert   CryptersK   ENCRYPTED_FIELD_MODE must be either DECRYPT_AND_ENCRYPT or ENCRYPT, not %s.(   R   R   R   R   (   R   t
   crypt_typet   crypt_class_name(    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/db/fields/encrypted.pyR   9   s    		
c         C   s¤   t  |  j j t j j ƒ r$ | } n| | rš | j |  j ƒ rš t |  j d ƒ r‘ |  j j	 | t
 |  j ƒ ƒ } t j r— | r— | j d ƒ } q— q  | } n | } | S(   Nt   Decrypts   utf-8(   t
   isinstanceR   t   primary_keyR   t   keyst   RsaPublicKeyt
   startswithR   t   hasattrR(   R   t   sixt   PY2t   decode(   R   t   valuet   retval(    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/db/fields/encrypted.pyt	   to_pythonR   s    		i   c         C   s   |  j  | ƒ S(   N(   R4   (   R   R2   t
   expressiont
   connectiont   context(    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/db/fields/encrypted.pyt   from_db_valuea   s    c         C   s   |  j  | ƒ S(   N(   R4   (   R   R2   R5   R6   (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/db/fields/encrypted.pyR8   d   s    c         C   sÃ   | r¿ | j  |  j ƒ r¿ t j rO t | ƒ t j j k rO | j d ƒ } qO n  |  j } | r£ t	 | ƒ | k r£ t
 j d |  j t	 | ƒ | f t ƒ | |  } n  |  j |  j j | ƒ } n  | S(   Ns   utf-8s'   Truncating field %s from %d to %d bytes(   R-   R   R/   R0   t   typet   typest   UnicodeTypet   encodeR   R   R   R   t   nameR   R   R    (   R   R2   R6   t   preparedR   (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/db/fields/encrypted.pyt   get_db_prep_valueg   s    			c         C   s>   t  t |  ƒ j ƒ  \ } } } } |  j | d <| | | | f S(   NR   (   R   R	   t   deconstructR   (   R   R=   t   pathR   R   (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/db/fields/encrypted.pyR@   z   s    !(   i   (   R   R   R   R   R   R   R4   t   djangot   VERSIONR8   t   FalseR?   R@   (    (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/db/fields/encrypted.pyR	      s   					t   EncryptedTextFieldc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   d S(   Nt	   TextField(    (   R   (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/db/fields/encrypted.pyt   get_internal_type   s    c         K   s3   i t  j d 6} | j | ƒ t t |  ƒ j |   S(   Nt   widget(   R    t   Textareat   updateR   RE   t	   formfield(   R   R   t   defaults(    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/db/fields/encrypted.pyRK   „   s    (   R   R   RG   RK   (    (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/db/fields/encrypted.pyRE   €   s   	t   EncryptedCharFieldc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         O   s   t  t |  ƒ j | | Ž  d  S(   N(   R   RM   R   (   R   R   R   (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/db/fields/encrypted.pyR   ‹   s    c         C   s   d S(   Nt	   CharField(    (   R   (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/db/fields/encrypted.pyRG   Ž   s    c         K   s3   i |  j  d 6} | j | ƒ t t |  ƒ j |   S(   NR   (   R   RJ   R   RM   RK   (   R   R   RL   (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/db/fields/encrypted.pyRK   ‘   s    (   R   R   R   RG   RK   (    (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/db/fields/encrypted.pyRM   Š   s   		(   R   RB   R/   R    t   django.confR   t   django.core.exceptionsR   t	   django.dbR   t   django.utils.deprecationR   R   t   ImportErrort   RuntimeWarningR   t   FieldR	   RE   RM   (    (    (    sG   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/db/fields/encrypted.pyt   <module>   s   i
