U
    mf                     @   sD   d dl mZ d dlmZmZ e ZG dd dZG dd dZdS )    )settings)	constantsutilsc                   @   sJ   e Zd ZdZdddZdd Zdd Zd	d
 Zedd Z	edd Z
dS )Messagez
    Represent an actual message that can be stored in any of the supported
    storage classes (typically session- or cookie-based) and rendered in a view
    or template.
    Nc                 C   s   t || _|| _|| _d S N)intlevelmessage
extra_tagsselfr   r	   r
    r   H/tmp/pip-unpacked-wheel-siwebuq3/django/contrib/messages/storage/base.py__init__   s    
zMessage.__init__c                 C   s*   t | j| _| jdk	r t | jnd| _dS )z
        Prepare the message for serialization by forcing the ``message``
        and ``extra_tags`` to str in case they are lazy translations.
        N)strr	   r
   r   r   r   r   _prepare   s    zMessage._preparec                 C   s&   t |tstS | j|jko$| j|jkS r   )
isinstancer   NotImplementedr   r	   )r   otherr   r   r   __eq__   s    
zMessage.__eq__c                 C   s
   t | jS r   )r   r	   r   r   r   r   __str__    s    zMessage.__str__c                 C   s   d dd | j| jfD S )N c                 s   s   | ]}|r|V  qd S r   r   ).0tagr   r   r   	<genexpr>%   s      zMessage.tags.<locals>.<genexpr>)joinr
   	level_tagr   r   r   r   tags#   s    zMessage.tagsc                 C   s   t | jdS )N )
LEVEL_TAGSgetr   r   r   r   r   r   '   s    zMessage.level_tag)N)__name__
__module____qualname____doc__r   r   r   r   propertyr   r   r   r   r   r   r      s   

r   c                       s   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Ze	dd Z
dd Zdd Zdd Zdd ZdddZdd ZdddZe	eeeZ  ZS ) BaseStoragez
    This is the base backend for temporary message storage.

    This is not a complete class; to be a usable storage backend, it must be
    subclassed and the two methods ``_get`` and ``_store`` overridden.
    c                    s*   || _ g | _d| _d| _t j|| d S )NF)request_queued_messagesused	added_newsuperr   )r   r(   argskwargs	__class__r   r   r   4   s
    zBaseStorage.__init__c                 C   s   t | jt | j S r   )len_loaded_messagesr)   r   r   r   r   __len__;   s    zBaseStorage.__len__c                 C   s*   d| _ | jr | j| j g | _t| jS )NT)r*   r)   r2   extenditerr   r   r   r   __iter__>   s
    zBaseStorage.__iter__c                 C   s   || j kp|| jkS r   )r2   r)   )r   itemr   r   r   __contains__E   s    zBaseStorage.__contains__c                 C   s   d| j j d| jdS )N<z
: request=>)r0   r$   r(   r   r   r   r   __repr__H   s    zBaseStorage.__repr__c                 C   s&   t | ds |  \}}|pg | _| jS )zs
        Return a list of loaded messages, retrieving them first if they have
        not been loaded yet.
        _loaded_data)hasattr_getr<   )r   messagesZall_retrievedr   r   r   r2   K   s    

zBaseStorage._loaded_messagesc                 O   s   t ddS )a  
        Retrieve a list of stored messages. Return a tuple of the messages
        and a flag indicating whether or not all the messages originally
        intended to be stored in this storage were, in fact, stored and
        retrieved; e.g., ``(messages, all_retrieved)``.

        **This method must be implemented by a subclass.**

        If it is possible to tell if the backend was not used (as opposed to
        just containing no messages) then ``None`` should be returned in
        place of ``messages``.
        z6subclasses of BaseStorage must provide a _get() methodNNotImplementedError)r   r-   r.   r   r   r   r>   V   s    zBaseStorage._getc                 O   s   t ddS )z
        Store a list of messages and return a list of any messages which could
        not be stored.

        One type of object must be able to be stored, ``Message``.

        **This method must be implemented by a subclass.**
        z8subclasses of BaseStorage must provide a _store() methodNr@   )r   r?   responser-   r.   r   r   r   _storeg   s    	zBaseStorage._storec                 C   s   |D ]}|   qdS )z9
        Prepare a list of messages for storage.
        N)r   )r   r?   r	   r   r   r   _prepare_messagest   s    zBaseStorage._prepare_messagesc                 C   sB   |  | j | jr | | j|S | jr>| j| j }| ||S dS )z
        Store all unread messages.

        If the backend has yet to be iterated, store previously stored messages
        again. Otherwise, only store messages added after the last iteration.
        N)rD   r)   r*   rC   r+   r2   )r   rB   r?   r   r   r   update{   s    zBaseStorage.updater   c                 C   sB   |sdS t |}|| jk rdS d| _t|||d}| j| dS )z
        Queue a message to be stored.

        The message is only queued if it contained something and its level is
        not less than the recording level (``self.level``).
        NT)r
   )r   r   r+   r   r)   appendr   r   r   r   add   s    
zBaseStorage.addc                 C   s    t | dsttdtj| _| jS )z
        Return the minimum recorded level.

        The default level is the ``MESSAGE_LEVEL`` setting. If this is
        not found, the ``INFO`` level is used.
        _levelZMESSAGE_LEVEL)r=   getattrr   r   INFOrH   r   r   r   r   
_get_level   s    
zBaseStorage._get_levelNc                 C   s&   |dkrt | dr| `n
t|| _dS )z
        Set a custom minimum recorded level.

        If set to ``None``, the default level will be used (see the
        ``_get_level`` method).
        NrH   )r=   rH   r   )r   valuer   r   r   
_set_level   s    zBaseStorage._set_level)r   )N)r"   r#   r$   r%   r   r3   r6   r8   r;   r&   r2   r>   rC   rD   rE   rG   rK   rM   r   __classcell__r   r   r/   r   r'   ,   s    



r'   N)	Zdjango.confr   Zdjango.contrib.messagesr   r   Zget_level_tagsr    r   r'   r   r   r   r   <module>   s   %