<s> from django . conf import settings <EOL> from django . utils . encoding import force_unicode , StrAndUnicode <EOL> from django . contrib . messages import constants , utils <EOL> LEVEL_TAGS = utils . get_level_tags ( ) <EOL> class Message ( StrAndUnicode ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , level , message , extra_tags = None ) : <EOL> self . level = int ( level ) <EOL> self . message = message <EOL> self . extra_tags = extra_tags <EOL> def _prepare ( self ) : <EOL> """<STR_LIT>""" <EOL> self . message = force_unicode ( self . message , strings_only = True ) <EOL> self . extra_tags = force_unicode ( self . extra_tags , strings_only = True ) <EOL> def __eq__ ( self , other ) : <EOL> return isinstance ( other , Message ) and self . level == other . level and self . message == other . message <EOL> def __unicode__ ( self ) : <EOL> return force_unicode ( self . message ) <EOL> def _get_tags ( self ) : <EOL> label_tag = force_unicode ( LEVEL_TAGS . get ( self . level , '<STR_LIT>' ) , <EOL> strings_only = True ) <EOL> extra_tags = force_unicode ( self . extra_tags , strings_only = True ) <EOL> if extra_tags and label_tag : <EOL> return u'<STR_LIT:U+0020>' . join ( [ extra_tags , label_tag ] ) <EOL> elif extra_tags : <EOL> return extra_tags <EOL> elif label_tag : <EOL> return label_tag <EOL> return '<STR_LIT>' <EOL> tags = property ( _get_tags ) <EOL> class BaseStorage ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> self . request = request <EOL> self . _queued_messages = [ ] <EOL> self . used = False <EOL> self . added_new = False <EOL> super ( BaseStorage , self ) . __init__ ( * args , ** kwargs ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _loaded_messages ) + len ( self . _queued_messages ) <EOL> def __iter__ ( self ) : <EOL> self . used = True <EOL> if self . _queued_messages : <EOL> self . _loaded_messages . extend ( self . _queued_messages ) <EOL> self . _queued_messages = [ ] <EOL> return iter ( self . _loaded_messages ) <EOL> def __contains__ ( self , item ) : <EOL> return item in self . _loaded_messages or item in self . _queued_messages <EOL> @ property <EOL> def _loaded_messages ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> messages , all_retrieved = self . _get ( ) <EOL> self . _loaded_data = messages or [ ] <EOL> return self . _loaded_data <EOL> def _get ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def _store ( self , messages , response , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def _prepare_messages ( self , messages ) : <EOL> """<STR_LIT>""" <EOL> for message in messages : <EOL> message . _prepare ( ) <EOL> def update ( self , response ) : <EOL> """<STR_LIT>""" <EOL> self . _prepare_messages ( self . _queued_messages ) <EOL> if self . used : <EOL> return self . _store ( self . _queued_messages , response ) <EOL> elif self . added_new : <EOL> messages = self . _loaded_messages + self . _queued_messages <EOL> return self . _store ( messages , response ) <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> self . added_new = True <EOL> message = Message ( level , message , extra_tags = extra_tags ) <EOL> self . _queued_messages . append ( message ) <EOL> def _get_level ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _level = getattr ( settings , '<STR_LIT>' , constants . INFO ) <EOL> return self . _level <EOL> def _set_level ( self , value = None ) : <EOL> """<STR_LIT>""" <EOL> if value is None and hasattr ( self , '<STR_LIT>' ) : <EOL> del self . _level <EOL> else : <EOL> self . _level = int ( value ) <EOL> level = property ( _get_level , _set_level , _set_level ) </s>
