
v^                 @   s   d  d l  Z  d  d l m Z d  d l m Z m Z d  d l m Z d  d l m	 Z	 m
 Z
 d  d l m Z m Z Gd d   d e  j  Z Gd	 d
   d
 e  j  Z Gd d   d e  Z d S)    N)settings)BaseStorageMessage)SimpleCookie)constant_time_comparesalted_hmac)SafeData	mark_safec                   s.   e  Z d  Z d Z d Z   f d d   Z   S)MessageEncoderzI
    Compactly serialize instances of the ``Message`` class as JSON.
    Z__json_messagec                su   t  | t  re t  | j t  r' d n d } |  j | | j | j g } | j ra | j | j  | St   j	 |  S)N   r   )

isinstancer   messager   message_keylevelZ
extra_tagsappendsuperdefault)selfobjZis_safedatar   )	__class__ H/tmp/pip-build-8lau8j11/django/django/contrib/messages/storage/cookie.pyr      s    	zMessageEncoder.default)__name__
__module____qualname____doc__r   r   r   r   )r   r   r
   
   s   r
   c                   s4   e  Z d  Z d Z d d   Z   f d d   Z   S)MessageDecoderzE
    Decode JSON that includes serialized ``Message`` instances.
    c                s   t  | t  r | r | d t j k r t |  d k rN t | d d     S| d rl t | d  | d <t | d d     S  f d d   | D St  | t  r   f d d   | j   D S| S)	Nr      r      c                s   g  |  ] }   j  |   q Sr   )process_messages).0item)r   r   r   
<listcomp>)   s   	 z3MessageDecoder.process_messages.<locals>.<listcomp>c                s(   i  |  ] \ } }   j  |  |  q Sr   )r   )r    keyvalue)r   r   r   
<dictcomp>+   s   	z3MessageDecoder.process_messages.<locals>.<dictcomp>)	r   listr
   r   lenr   r	   dictitems)r   r   r   )r   r   r       s    
zMessageDecoder.process_messagesc                s"   t    j | |  } |  j |  S)N)r   decoder   )r   skwargsdecoded)r   r   r   r*   /   s    zMessageDecoder.decode)r   r   r   r   r   r*   r   r   )r   r   r      s   r   c               @   sv   e  Z d  Z d Z d Z d Z d Z d d   Z d d   Z d	 d
 d  Z	 d d   Z
 d d d  Z d d   Z d S)CookieStoragez%
    Store messages in a cookie.
    messagesi   Z__messagesnotfinished__c             O   sb   |  j  j j |  j  } |  j |  } | o= | d |  j k } | rX | rX | j   | | f S)a
  
        Retrieve a list of messages from the messages cookie. If the
        not_finished sentinel value is found at the end of the message list,
        remove it and return a result indicating that not all messages were
        retrieved by this storage.
        r   )requestZCOOKIESgetcookie_name_decodenot_finishedpop)r   argsr,   datar/   Zall_retrievedr   r   r   _get?   s    
zCookieStorage._getc             C   si   | rL | j  |  j | d t j d t j p- d d t j p< d d t j n | j |  j d t j d S)zz
        Either set the cookie with the encoded data if there is any data to
        store, or delete the cookie.
        domainsecureNhttponlyZsamesite)
set_cookier3   r   ZSESSION_COOKIE_DOMAINZSESSION_COOKIE_SECUREZSESSION_COOKIE_HTTPONLYZSESSION_COOKIE_SAMESITEZdelete_cookie)r   encoded_dataresponser   r   r   _update_cookieN   s    	zCookieStorage._update_cookieTc       	         s   g  } |  j  |  } |  j r t       f d d   } xs | r | |  |  j k r | rv | j | j d   n | j d | j    |  j  | |  j g d | } q< W|  j | |  | S)aT  
        Store the messages to a cookie and return a list of any messages which
        could not be stored.

        If the encoded data is larger than ``max_cookie_size``, remove
        messages until the data fits (these are the messages which are
        returned), and add the not_finished sentinel value to indicate as much.
        c                s   t    j |   d  S)Nr   )r'   value_encode)val)cookier   r   stored_lengthn   s    z+CookieStorage._store.<locals>.stored_lengthr   encode_empty)_encodemax_cookie_sizer   r   r6   insertr5   r@   )	r   r/   r?   Zremove_oldestr7   r,   Zunstored_messagesr>   rD   r   )rC   r   _store^   s    			zCookieStorage._storec             C   s   d } t  | |  j   S)z
        Create an HMAC/SHA1 hash based on the value and the project setting's
        SECRET_KEY, modified to make it unique for the present purpose.
        zdjango.contrib.messages)r   	hexdigest)r   r$   Zkey_saltr   r   r   _hash{   s    zCookieStorage._hashFc             C   sE   | s | rA t  d d  } | j |  } d |  j |  | f Sd S)a  
        Return an encoded version of the messages list which can be stored as
        plain text.

        Since the data will be retrieved from the client-side, the encoded data
        also contains a hash to ensure that the data was not tampered with.
        
separators,:z%s$%sN)rM   rN   )r
   encoderK   )r   r/   rE   encoderr$   r   r   r   rF      s    zCookieStorage._encodec             C   s   | s
 d S| j  d d  } t |  d k r | \ } } t | |  j |   r y t j | d t SWn t j k
 r Yn Xd |  _ d S)z
        Safely decode an encoded text stream back into a list of messages.

        If the encoded text stream contained an invalid hash or was in an
        invalid format, return None.
        N$r   r   clsT)	splitr'   r   rK   jsonloadsr   JSONDecodeErrorused)r   r8   bitshashr$   r   r   r   r4      s    	zCookieStorage._decodeN)r   r   r   r   r3   rG   r5   r9   r@   rI   rK   rF   r4   r   r   r   r   r.   4   s   r.   )rT   Zdjango.confr   Z$django.contrib.messages.storage.baser   r   Zdjango.httpr   Zdjango.utils.cryptor   r   Zdjango.utils.safestringr   r	   JSONEncoderr
   JSONDecoderr   r.   r   r   r   r   <module>   s   