<s> from django . conf import settings <EOL> from django . contrib . messages . storage . base import BaseStorage , Message <EOL> from django . http import SimpleCookie <EOL> from django . utils import simplejson as json <EOL> from django . utils . crypto import salted_hmac , constant_time_compare <EOL> class MessageEncoder ( json . JSONEncoder ) : <EOL> """<STR_LIT>""" <EOL> message_key = '<STR_LIT>' <EOL> def default ( self , obj ) : <EOL> if isinstance ( obj , Message ) : <EOL> message = [ self . message_key , obj . level , obj . message ] <EOL> if obj . extra_tags : <EOL> message . append ( obj . extra_tags ) <EOL> return message <EOL> return super ( MessageEncoder , self ) . default ( obj ) <EOL> class MessageDecoder ( json . JSONDecoder ) : <EOL> """<STR_LIT>""" <EOL> def process_messages ( self , obj ) : <EOL> if isinstance ( obj , list ) and obj : <EOL> if obj [ <NUM_LIT:0> ] == MessageEncoder . message_key : <EOL> return Message ( * obj [ <NUM_LIT:1> : ] ) <EOL> return [ self . process_messages ( item ) for item in obj ] <EOL> if isinstance ( obj , dict ) : <EOL> return dict ( [ ( key , self . process_messages ( value ) ) <EOL> for key , value in obj . iteritems ( ) ] ) <EOL> return obj <EOL> def decode ( self , s , ** kwargs ) : <EOL> decoded = super ( MessageDecoder , self ) . decode ( s , ** kwargs ) <EOL> return self . process_messages ( decoded ) <EOL> class CookieStorage ( BaseStorage ) : <EOL> """<STR_LIT>""" <EOL> cookie_name = '<STR_LIT>' <EOL> max_cookie_size = <NUM_LIT> <EOL> not_finished = '<STR_LIT>' <EOL> def _get ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> data = self . request . COOKIES . get ( self . cookie_name ) <EOL> messages = self . _decode ( data ) <EOL> all_retrieved = not ( messages and messages [ - <NUM_LIT:1> ] == self . not_finished ) <EOL> if messages and not all_retrieved : <EOL> messages . pop ( ) <EOL> return messages , all_retrieved <EOL> def _update_cookie ( self , encoded_data , response ) : <EOL> """<STR_LIT>""" <EOL> if encoded_data : <EOL> response . set_cookie ( self . cookie_name , encoded_data , <EOL> domain = settings . SESSION_COOKIE_DOMAIN ) <EOL> else : <EOL> response . delete_cookie ( self . cookie_name , <EOL> domain = settings . SESSION_COOKIE_DOMAIN ) <EOL> def _store ( self , messages , response , remove_oldest = True , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> unstored_messages = [ ] <EOL> encoded_data = self . _encode ( messages ) <EOL> if self . max_cookie_size : <EOL> cookie = SimpleCookie ( ) <EOL> def stored_length ( val ) : <EOL> return len ( cookie . value_encode ( val ) [ <NUM_LIT:1> ] ) <EOL> while encoded_data and stored_length ( encoded_data ) > self . max_cookie_size : <EOL> if remove_oldest : <EOL> unstored_messages . append ( messages . pop ( <NUM_LIT:0> ) ) <EOL> else : <EOL> unstored_messages . insert ( <NUM_LIT:0> , messages . pop ( ) ) <EOL> encoded_data = self . _encode ( messages + [ self . not_finished ] , <EOL> encode_empty = unstored_messages ) <EOL> self . _update_cookie ( encoded_data , response ) <EOL> return unstored_messages <EOL> def _hash ( self , value ) : <EOL> """<STR_LIT>""" <EOL> key_salt = '<STR_LIT>' <EOL> return salted_hmac ( key_salt , value ) . hexdigest ( ) <EOL> def _encode ( self , messages , encode_empty = False ) : <EOL> """<STR_LIT>""" <EOL> if messages or encode_empty : <EOL> encoder = MessageEncoder ( separators = ( '<STR_LIT:U+002C>' , '<STR_LIT::>' ) ) <EOL> value = encoder . encode ( messages ) <EOL> return '<STR_LIT>' % ( self . _hash ( value ) , value ) <EOL> def _decode ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if not data : <EOL> return None <EOL> bits = data . split ( '<STR_LIT:$>' , <NUM_LIT:1> ) <EOL> if len ( bits ) == <NUM_LIT:2> : <EOL> hash , value = bits <EOL> if constant_time_compare ( hash , self . _hash ( value ) ) : <EOL> try : <EOL> return json . loads ( value , cls = MessageDecoder ) <EOL> except ValueError : <EOL> pass <EOL> self . used = True <EOL> return None </s>
