3
g                 @   s   d dl Z 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
 d dlmZmZ d dlmZmZ G dd	 d	ejZG d
d dejZG dd dZG dd deZdS )    N)settings)BaseStorageMessage)signing)SimpleCookie)constant_time_comparesalted_hmac)SafeData	mark_safec                   s$   e Zd ZdZdZ fddZ  ZS )MessageEncoderzI
    Compactly serialize instances of the ``Message`` class as JSON.
    Z__json_messagec                sR   t |trFt |jtrdnd}| j||j|jg}|jrB|j|j |S t j	|S )N   r   )

isinstancer   messager	   message_keylevelZ
extra_tagsappendsuperdefault)selfobjZis_safedatar   )	__class__ d/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django/contrib/messages/storage/cookie.pyr      s    
zMessageEncoder.default)__name__
__module____qualname____doc__r   r   __classcell__r   r   )r   r   r      s   r   c                   s(   e Zd ZdZdd Z fddZ  ZS )MessageDecoderzE
    Decode JSON that includes serialized ``Message`` instances.
    c                sz   t |trV|rV|d tjkrD|d r4t|d |d< t|dd   S  fdd|D S t |trv fdd|j D S |S )	Nr   r         c                s   g | ]} j |qS r   )process_messages).0item)r   r   r   
<listcomp>(   s    z3MessageDecoder.process_messages.<locals>.<listcomp>c                s   i | ]\}} j ||qS r   )r!   )r"   keyvalue)r   r   r   
<dictcomp>*   s   z3MessageDecoder.process_messages.<locals>.<dictcomp>)r   listr   r   r
   r   dictitems)r   r   r   )r   r   r!   "   s    

zMessageDecoder.process_messagesc                s   t  j|f|}| 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   r      s   r   c               @   s   e Zd Zdd Zdd ZdS )MessageSerializerc             C   s   t j|dtdjdS )N,:)
separatorsclszlatin-1)r0   r1   )jsondumpsr   encode)r   r   r   r   r   r5   4   s
    zMessageSerializer.dumpsc             C   s   t j|jdtdS )Nzlatin-1)r3   )r4   loadsr+   r   )r   datar   r   r   r7   ;   s    zMessageSerializer.loadsN)r   r   r   r5   r7   r   r   r   r   r/   3   s   r/   c                   sl   e Zd ZdZdZdZdZdZ fddZdd	 Z	d
d Z
dddZdd ZdddZdd Zdd Z  ZS )CookieStoragez%
    Store messages in a cookie.
    messagesi   Z__messagesnotfinished__zdjango.contrib.messagesc                s"   t  j|| tj| jd| _d S )N)Zsalt)r   __init__r   Zget_cookie_signerkey_saltsigner)r   argsr-   )r   r   r   r;   K   s    zCookieStorage.__init__c             O   sH   | j jj| j}| j|}|o*|d | jk }|r@| r@|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   r>   r-   r8   r:   Zall_retrievedr   r   r   _getO   s    

zCookieStorage._getc             C   sH   |r.|j | j|tjtjpdtjp"dtjd n|j| jtjtjd dS )zz
        Either set the cookie with the encoded data if there is any data to
        store, or delete the cookie.
        N)domainsecurehttponlysamesite)rG   rJ   )
set_cookierB   r   ZSESSION_COOKIE_DOMAINZSESSION_COOKIE_SECUREZSESSION_COOKIE_HTTPONLYZSESSION_COOKIE_SAMESITEZdelete_cookie)r   encoded_dataresponser   r   r   _update_cookie^   s    zCookieStorage._update_cookieTc       	         s   g }| j |}| jrzt   fdd}xR|rx||| jkrx|rP|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   )lenvalue_encode)val)cookier   r   stored_length   s    z+CookieStorage._store.<locals>.stored_lengthr   )encode_empty)_encodemax_cookie_sizer   r   rE   insertrD   rN   )	r   r:   rM   Zremove_oldestr>   r-   Zunstored_messagesrL   rS   r   )rR   r   _storer   s    	
zCookieStorage._storec             C   s   d}t ||j S )z
        # RemovedInDjango40Warning: pre-Django 3.1 hashes will be invalid.
        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&   r<   r   r   r   _legacy_hash   s    	zCookieStorage._legacy_hashFc             C   s   |s|r| j j|tddS d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.
        T)
serializercompressN)r=   Zsign_objectr/   )r   r:   rT   r   r   r   rU      s    zCookieStorage._encodec             C   s   |sdS y| j j|tdS  tjk
r8   | j|}Y n( tjtj	fk
r^   | j j
|}Y nX |rytj|tdS  tj	k
r   Y nX d| _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[   )r3   T)r=   Zunsign_objectr/   r   ZBadSignature_legacy_decodebinasciiErrorr4   JSONDecodeErrorZunsignr7   r   used)r   r8   r.   r   r   r   rC      s    zCookieStorage._decodec             C   s8   |j dd}t|dkr4|\}}t|| j|r4|S d S )N$r   r    )splitrO   r   rZ   )r   r8   bitshash_r&   r   r   r   r]      s    zCookieStorage._legacy_decode)T)F)r   r   r   r   rB   rV   rD   r<   r;   rF   rN   rX   rZ   rU   rC   r]   r   r   r   )r   r   r9   ?   s   

"r9   )r^   r4   Zdjango.confr   Z$django.contrib.messages.storage.baser   r   Zdjango.corer   Zdjango.httpr   Zdjango.utils.cryptor   r   Zdjango.utils.safestringr	   r
   JSONEncoderr   JSONDecoderr   r/   r9   r   r   r   r   <module>   s   