U
    ۼ|e                     @   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 k	rF||j |S t 	|S )N   r   )

isinstancer   messager   message_keylevel
extra_tagsappendsuperdefault)selfobjZis_safedatar   	__class__ c/var/www/website-v5/atlas_env/lib/python3.8/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>(   s     z3MessageDecoder.process_messages.<locals>.<listcomp>c                    s   i | ]\}}|  |qS r   r!   )r#   keyvaluer%   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|}| |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   r9   3   s    zMessageSerializer.dumpsc                 C   s   t j|dtdS )Nr7   )r6   )r8   loadsr.   r   r   datar   r   r   r;   :   s    zMessageSerializer.loadsN)r   r   r   r9   r;   r   r   r   r   r2   2   s   r2   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|| tj| jd| _d S )N)salt)r   __init__r   get_cookie_signerkey_saltsigner)r   argsr0   r   r   r   rA   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   rE   r0   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)rP   rS   )
set_cookierJ   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   rM   insertrL   r\   )	r   r?   r[   remove_oldestrE   r0   unstored_messagesrZ   rb   r   r`   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)rD   sign_objectr2   )r   r?   rc   r   r   r   rd      s      zCookieStorage._encodec              
   C   sF   |sdS z| j j|tdW S  tjtjtjf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)rj   T)
rD   unsign_objectr2   r   BadSignaturebinasciiErrorr8   JSONDecodeErrorusedr<   r   r   r   rK      s    zCookieStorage._decode)T)F)r   r   r   r   rJ   re   rL   rC   rA   rO   r\   ri   rd   rK   r   r   r   r   r   r>   >   s   

r>   )ro   r8   django.confr   $django.contrib.messages.storage.baser   r   Zdjango.corer   django.httpr   django.utils.safestringr   r   JSONEncoderr	   JSONDecoderr   r2   r>   r   r   r   r   <module>   s   