o
    {,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ZG dd dZG dd deZdd Zdd 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.
    __json_messagec                    sV   t |tr%t |jtrdnd}| j||j|jg}|jd ur#||j |S t 	|S )N   r   )

isinstancer   messager   message_keylevel
extra_tagsappendsuperdefault)selfobjis_safedatar   	__class__ \/var/www/html/rh/venv/lib/python3.10/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r+|r+|d tjkr"|d rt|d |d< t|dd   S  fdd|D S t |tr; 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i |}| |S N)r   decoder$   )r   skwargsdecodedr   r   r   r1   -   s   
zMessageDecoder.decode)r   r   r   r   r$   r1   r   r   r   r   r   r       s    r    c                   @      e Zd Zdd ZdS )MessagePartSerializerc                 C   s   dd |D S )Nc                 S   s   g | ]
}t j|d tdqS )),:)
separatorscls)jsondumpsr	   )r%   or   r   r   r(   4   s    z/MessagePartSerializer.dumps.<locals>.<listcomp>r   r/   r   r   r   r<   3   s   zMessagePartSerializer.dumpsNr   r   r   r<   r   r   r   r   r6   2       r6   c                   @   r5   )MessagePartGatherSerializerc                 C   s   dd | d dS )z
        The parameter is an already serialized list of Message objects. No need
        to serialize it again, only join the list together and encode it.
        [r7   ]latin-1)joinencoder/   r   r   r   r<   ?   s   z!MessagePartGatherSerializer.dumpsNr>   r   r   r   r   r@   >   r?   r@   c                   @   r5   )MessageSerializerc                 C   s   t j|dtdS )NrC   )r:   )r;   loadsr1   r    r   datar   r   r   rG   H   s   zMessageSerializer.loadsN)r   r   r   rG   r   r   r   r   rF   G   r?   rF   c                       sp   e Zd ZdZdZdZdZedZ	dZ
 fddZdd	 Zd
d ZdddZdddZdddZdd Z  ZS )CookieStoragez%
    Store messages in a cookie.
    messagesi   __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   argsr3   r   r   r   rN   Z   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   rR   r3   rI   rK   all_retrievedr   r   r   _get^   s   
zCookieStorage._getc                 C   sJ   |r|j | j|tjtjpdtjpdtjd dS |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)r]   r`   )
set_cookierW   r   SESSION_COOKIE_DOMAINSESSION_COOKIE_SECURESESSION_COOKIE_HTTPONLYSESSION_COOKIE_SAMESITEdelete_cookie)r   encoded_dataresponser   r   r   _update_cookiem   s   
	
zCookieStorage._update_cookieTc           
         s   g }t  |}|}jrYt fddfdd |rY|r@t| fddd}	|d|	 } ||	d }nt| fd	dd}	||	d } |d|	 }|| |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   | ot  | d jkS )Nr   )lenvalue_encodemax_cookie_size)rI   )cookier   r   r   is_too_large_for_cookie   s   z5CookieStorage._store.<locals>.is_too_large_for_cookiec                    s    j |  jg ddS )NTencode_empty)_encode_partsnot_finished_json)some_serialized_msgr'   r   r   compute_msg   s   
z)CookieStorage._store.<locals>.compute_msgc                        | S r0   r   mrt   rn   r   r   <lambda>       z&CookieStorage._store.<locals>.<lambda>)fnNc                    ru   r0   r   rv   rx   r   r   ry      rz   )r6   r<   rq   rl   r   bisect_keep_rightbisect_keep_leftri   )
r   rK   rh   remove_oldestrR   r3   unstored_messagesserialized_messagesrg   idxr   )rt   rm   rn   r   r   _store   s.   	
zCookieStorage._storeFc                 C   s   |s|r| j j|tddS dS )a  
        Return an encoded version of the serialized 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)rQ   sign_objectr@   )r   rK   rp   r   r   r   rq      s
   zCookieStorage._encode_partsc                 C   s   t  |}| j||dS )z
        Return an encoded version of the messages list which can be stored as
        plain text.

        Proxies MessagePartSerializer.dumps and _encoded_parts.
        ro   )r6   r<   rq   )r   rK   rp   r   r   r   r   _encode   s   zCookieStorage._encodec              
   C   sD   |sdS z	| j j|tdW S  tjtjtjfy   Y nw 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   T)
rQ   unsign_objectrF   r   BadSignaturebinasciiErrorr;   JSONDecodeErrorusedrH   r   r   r   rX      s   zCookieStorage._decode)T)F)r   r   r   r   rW   rl   rY   r;   r<   rr   rP   rN   r\   ri   r   rq   r   rX   r   r   r   r   r   rJ   L   s    


-

rJ   c                 C   sN   d}t | }||k r%|| d }|| d|d  r|}n|d }||k s
|S )z
    Find the index of the first element from the start of the array that
    verifies the given condition.
    The function is applied from the start of the array to the pivot.
    r   r"   Nr   rj   ar{   lohimidr   r   r   r}      s   r}   c                 C   sJ   d}t | }||k r#|| d }|| |d r|d }n|}||k s
|S )z
    Find the index of the first element from the end of the array that verifies
    the given condition.
    The function is applied from the pivot to the end of array.
    r   r"   Nr   r   r   r   r   r   r|      s   
r|   )r   r;   django.confr   $django.contrib.messages.storage.baser   r   django.corer   django.httpr   django.utils.safestringr   r   JSONEncoderr	   JSONDecoderr    r6   r@   rF   rJ   r}   r|   r   r   r   r   <module>   s     	 