<s> from django . contrib . messages . storage . fallback import FallbackStorage <EOL> from . settings import stored_messages_settings <EOL> from . backends . exceptions import MessageTypeNotSupported <EOL> class StorageMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> self . user = request . user <EOL> BackendClass = stored_messages_settings . STORAGE_BACKEND <EOL> self . backend = BackendClass ( ) <EOL> super ( StorageMixin , self ) . __init__ ( request , * args , ** kwargs ) <EOL> def _get ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> messages , all_retrieved = super ( StorageMixin , self ) . _get ( * args , ** kwargs ) <EOL> if self . user . is_authenticated ( ) : <EOL> inbox_messages = self . backend . inbox_list ( self . user ) <EOL> else : <EOL> inbox_messages = [ ] <EOL> return messages + inbox_messages , all_retrieved <EOL> def add ( self , level , message , extra_tags = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if not message : <EOL> return <EOL> level = int ( level ) <EOL> if level < self . level : <EOL> return <EOL> if level not in stored_messages_settings . STORE_LEVELS or self . user . is_anonymous ( ) : <EOL> return super ( StorageMixin , self ) . add ( level , message , extra_tags ) <EOL> self . added_new = True <EOL> m = self . backend . create_message ( level , message , extra_tags ) <EOL> self . backend . archive_store ( [ self . user ] , m ) <EOL> self . _queued_messages . append ( m ) <EOL> def _store ( self , messages , response , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> contrib_messages = [ ] <EOL> if self . user . is_authenticated ( ) : <EOL> if not messages : <EOL> self . backend . inbox_purge ( self . user ) <EOL> else : <EOL> for m in messages : <EOL> try : <EOL> self . backend . inbox_store ( [ self . user ] , m ) <EOL> except MessageTypeNotSupported : <EOL> contrib_messages . append ( m ) <EOL> super ( StorageMixin , self ) . _store ( contrib_messages , response , * args , ** kwargs ) <EOL> def _prepare_messages ( self , messages ) : <EOL> """<STR_LIT>""" <EOL> for message in messages : <EOL> if not self . backend . can_handle ( message ) : <EOL> message . _prepare ( ) <EOL> class PersistentStorage ( StorageMixin , FallbackStorage ) : <EOL> """<STR_LIT>""" <EOL> pass </s>
