a
    ¦Q•hÈ  ã                   @   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 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)Ú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                    sV   t |tƒrJt |jtƒrdnd}| j||j|jg}|jd urF| |j¡ |S tƒ  	|¡S )Né   r   )
Ú
isinstancer   Úmessager   Úmessage_keyÚlevelÚ
extra_tagsÚappendÚsuperÚdefault)ÚselfÚobjZis_safedatar   ©Ú	__class__© ú_/var/www/sistema_ama/venv/lib/python3.9/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„| ¡ D ƒS |S )	Nr   r
   é   é   c                    s   g | ]}ˆ   |¡‘qS r   ©Úprocess_messages)Ú.0Úitem©r   r   r   Ú
<listcomp>(   ó    z3MessageDecoder.process_messages.<locals>.<listcomp>c                    s   i | ]\}}|ˆ   |¡“qS r   r!   )r#   ÚkeyÚvaluer%   r   r   Ú
<dictcomp>*   r'   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i |¤Ž}|  |¡S )N)r   Údecoder"   )r   ÚsÚkwargsÚdecodedr   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� d¡S )N)ú,ú:)Ú
separatorsÚclsúlatin-1)ÚjsonÚdumpsr	   Úencoder.   r   r   r   r:   3   s    ýüzMessageSerializer.dumpsc                 C   s   t j| d¡td�S )Nr8   )r7   )r9   Úloadsr/   r   ©r   Údatar   r   r   r<   :   s    zMessageSerializer.loadsN)r   r   r   r:   r<   r   r   r   r   r3   2   s   r3   c                       s\   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d„Zdd„ Z‡  ZS )ÚCookieStoragez%
    Store messages in a cookie.
    Úmessagesi   Z__messagesnotfinished__zdjango.contrib.messagesc                    s&   t ƒ j|i |¤Ž tj| jd�| _d S )N)Úsalt)r   Ú__init__r   Úget_cookie_signerÚkey_saltÚsigner)r   Úargsr1   r   r   r   rB   K   s    zCookieStorage.__init__c                 O   sF   | j j | j¡}|  |¡}|o*|d | jk }|r>|s>| ¡  ||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.
        éÿÿÿÿ)ÚrequestÚCOOKIESÚgetÚcookie_nameÚ_decodeÚnot_finishedÚpop)r   rF   r1   r>   r@   Ú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)rQ   rT   )Ú
set_cookierK   r   ÚSESSION_COOKIE_DOMAINÚSESSION_COOKIE_SECUREÚSESSION_COOKIE_HTTPONLYÚSESSION_COOKIE_SAMESITEÚdelete_cookie)r   Úencoded_dataÚresponser   r   r   Ú_update_cookie^   s    ú	ýzCookieStorage._update_cookieTc           	         s†   g }|   |¡}| jrvtƒ ‰ ‡ fdd„}|rv||ƒ| jkrv|rN| | d¡¡ n| d| ¡ ¡ | j || jg |d�}q&|  ||¡ |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 ˆ  | ¡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   rN   ÚinsertrM   r]   )	r   r@   r\   Úremove_oldestrF   r1   Úunstored_messagesr[   rc   r   ra   r   Ú_stores   s    	
ÿzCookieStorage._storeFc                 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)rE   Úsign_objectr3   )r   r@   rd   r   r   r   re   ‘   s    ÿzCookieStorage._encodec              
   C   sD   |sdS z| j j|td�W S  tjtjtjfy8   Y n0 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)rk   T)
rE   Úunsign_objectr3   r   ÚBadSignatureÚbinasciiÚErrorr9   ÚJSONDecodeErrorÚusedr=   r   r   r   rL   ž   s    zCookieStorage._decode)T)F)r   r   r   r   rK   rf   rM   rD   rB   rP   r]   rj   re   rL   r   r   r   r   r   r?   >   s   

r?   )rp   r9   Údjango.confr   Ú$django.contrib.messages.storage.baser   r   Zdjango.corer   Údjango.httpr   Údjango.utils.safestringr   r   ÚJSONEncoderr	   ÚJSONDecoderr   r3   r?   r   r   r   r   Ú<module>   s   