ó
TR¹Nc           @   s¾   d  d l  Z  d  d l m 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 d e j f d	 „  ƒ  YZ d
 e j f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   settings(   t	   constants(   t   BaseStoraget   Message(   t   CompatCookie(   t
   simplejson(   t   sha_hmact   MessageEncoderc           B   s   e  Z d  Z d Z d „  Z RS(   sJ   
    Compactly serializes instances of the ``Message`` class as JSON.
    t   __json_messagec         C   s]   t  | t ƒ rG |  j | j | j g } | j rC | j | j ƒ n  | St t |  ƒ j	 | ƒ S(   N(
   t
   isinstanceR   t   message_keyt   levelt   messaget
   extra_tagst   appendt   superR   t   default(   t   selft   objR   (    (    sS   /home/panlixing/Python_Projects/gaeseries/django/contrib/messages/storage/cookie.pyR      s    	(   t   __name__t
   __module__t   __doc__R
   R   (    (    (    sS   /home/panlixing/Python_Projects/gaeseries/django/contrib/messages/storage/cookie.pyR      s   t   MessageDecoderc           B   s    e  Z d  Z d „  Z d „  Z RS(   sF   
    Decodes JSON that includes serialized ``Message`` instances.
    c         C   s¡   t  | t ƒ rV | rV | d t j k r6 t | d Œ  Sg  | D] } |  j | ƒ ^ q= St  | t ƒ r t g  | j ƒ  D]! \ } } | |  j | ƒ f ^ qu ƒ S| S(   Ni    i   (   R	   t   listR   R
   R   t   process_messagest   dictt	   iteritems(   R   R   t   itemt   keyt   value(    (    sS   /home/panlixing/Python_Projects/gaeseries/django/contrib/messages/storage/cookie.pyR      s     2c         K   s(   t  t |  ƒ j | |  } |  j | ƒ S(   N(   R   R   t   decodeR   (   R   t   st   kwargst   decoded(    (    sS   /home/panlixing/Python_Projects/gaeseries/django/contrib/messages/storage/cookie.pyR   )   s    (   R   R   R   R   R   (    (    (    sS   /home/panlixing/Python_Projects/gaeseries/django/contrib/messages/storage/cookie.pyR      s   	
t   CookieStoragec           B   s\   e  Z d  Z d Z d Z d Z d „  Z d „  Z e d „ Z	 d „  Z
 e d „ Z d	 „  Z RS(
   s&   
    Stores messages in a cookie.
    t   messagesi   t   __messagesnotfinished__c         O   se   |  j  j j |  j ƒ } |  j | ƒ } | o= | d |  j k } | r[ | r[ | j ƒ  n  | | f S(   s  
        Retrieves 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.
        iÿÿÿÿ(   t   requestt   COOKIESt   gett   cookie_namet   _decodet   not_finishedt   pop(   R   t   argsR    t   dataR#   t   all_retrieved(    (    sS   /home/panlixing/Python_Projects/gaeseries/django/contrib/messages/storage/cookie.pyt   _get8   s    c         C   s0   | r | j  |  j | ƒ n | j |  j ƒ d S(   s|   
        Either sets the cookie with the encoded data if there is any data to
        store, or deletes the cookie.
        N(   t
   set_cookieR(   t   delete_cookie(   R   t   encoded_datat   response(    (    sS   /home/panlixing/Python_Projects/gaeseries/django/contrib/messages/storage/cookie.pyt   _update_cookieG   s    c   	         sÃ   g  } |  j  | ƒ } |  j r¯ t ƒ  ‰  ‡  f d †  } xv | r« | | ƒ |  j k r« | rs | j | j d ƒ ƒ n | j d | j ƒ  ƒ |  j  | |  j g d | ƒ} q9 Wn  |  j | | ƒ | S(   sV  
        Stores the messages to a cookie, returning a list of any messages which
        could not be stored.

        If the encoded data is larger than ``max_cookie_size``, removes
        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(   Ni   (   t   lent   value_encode(   t   val(   t   cookie(    sS   /home/panlixing/Python_Projects/gaeseries/django/contrib/messages/storage/cookie.pyt   stored_length`   s    i    t   encode_empty(   t   _encodet   max_cookie_sizeR   R   R+   t   insertR*   R4   (	   R   R#   R3   t   remove_oldestR,   R    t   unstored_messagesR2   R9   (    (   R8   sS   /home/panlixing/Python_Projects/gaeseries/django/contrib/messages/storage/cookie.pyt   _storeQ   s    			c         C   s&   d t  j } t j | | t ƒ j ƒ  S(   s    
        Creates an HMAC/SHA1 hash based on the value and the project setting's
        SECRET_KEY, modified to make it unique for the present purpose.
        s   django.contrib.messages(   R    t
   SECRET_KEYt   hmact   newR   t	   hexdigest(   R   R   R   (    (    sS   /home/panlixing/Python_Projects/gaeseries/django/contrib/messages/storage/cookie.pyt   _hashm   s    c         C   sE   | s | rA t  d d ƒ } | j | ƒ } d |  j | ƒ | f Sd S(   s	  
        Returns 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
   separatorst   ,t   :s   %s$%sN(   RG   RH   (   R   t   encodeRE   (   R   R#   R:   t   encoderR   (    (    sS   /home/panlixing/Python_Projects/gaeseries/django/contrib/messages/storage/cookie.pyR;   u   s    c         C   s   | s
 d S| j d d ƒ } t | ƒ d k r€ | \ } } | |  j | ƒ k r€ y t j | d t ƒSWq} t k
 ry q} Xq€ n  t |  _	 d S(   sÍ   
        Safely decodes a encoded text stream back into a list of messages.

        If the encoded text stream contained an invalid hash or was in an
        invalid format, ``None`` is returned.
        t   $i   i   t   clsN(
   t   Nonet   splitR5   RE   t   jsont   loadsR   t
   ValueErrort   Truet   used(   R   R-   t   bitst   hashR   (    (    sS   /home/panlixing/Python_Projects/gaeseries/django/contrib/messages/storage/cookie.pyR)   ‚   s    
	(   R   R   R   R(   R<   R*   R/   R4   RR   R@   RE   t   FalseR;   R)   (    (    (    sS   /home/panlixing/Python_Projects/gaeseries/django/contrib/messages/storage/cookie.pyR"   -   s   		
	(   RB   t   django.confR    t   django.contrib.messagesR   t$   django.contrib.messages.storage.baseR   R   t   django.httpR   t   django.utilsR   RO   t   django.utils.hashcompatR   t   JSONEncoderR   t   JSONDecoderR   R"   (    (    (    sS   /home/panlixing/Python_Projects/gaeseries/django/contrib/messages/storage/cookie.pyt   <module>   s   