<s> from contextlib import contextmanager <EOL> from . . utils import Recipient , is_iterable , get_registered_message_type <EOL> from . . models import Dispatch <EOL> from . . exceptions import UnknownMessageTypeError <EOL> class MessengerBase ( object ) : <EOL> """<STR_LIT>""" <EOL> alias = None <EOL> title = None <EOL> allow_user_subscription = True <EOL> _st = None <EOL> @ classmethod <EOL> def get_alias ( cls ) : <EOL> """<STR_LIT>""" <EOL> if cls . alias is None : <EOL> cls . alias = cls . __name__ <EOL> return cls . alias <EOL> def __str__ ( self ) : <EOL> return self . __class__ . get_alias ( ) <EOL> @ contextmanager <EOL> def before_after_send_handling ( self ) : <EOL> """<STR_LIT>""" <EOL> self . before_send ( ) <EOL> yield <EOL> self . after_send ( ) <EOL> def send_test_message ( self , to , text ) : <EOL> """<STR_LIT>""" <EOL> with self . before_after_send_handling ( ) : <EOL> result = self . _test_message ( to , text ) <EOL> return result <EOL> def _test_message ( self , to , text ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( self . __class__ . __name__ + '<STR_LIT>' ) <EOL> @ classmethod <EOL> def get_address ( cls , recipient ) : <EOL> """<STR_LIT>""" <EOL> return recipient <EOL> @ classmethod <EOL> def _structure_recipients_data ( cls , recipients ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> from django . contrib . auth import get_user_model <EOL> USER_MODEL = get_user_model ( ) <EOL> except ImportError : <EOL> from django . contrib . auth . models import User as USER_MODEL <EOL> if not is_iterable ( recipients ) : <EOL> recipients = ( recipients , ) <EOL> objects = [ ] <EOL> for r in recipients : <EOL> user = None <EOL> if isinstance ( r , USER_MODEL ) : <EOL> user = r <EOL> address = cls . get_address ( r ) <EOL> objects . append ( Recipient ( cls . get_alias ( ) , user , address ) ) <EOL> return objects <EOL> def _init_delivery_statuses_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _st = { <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT:error>' : [ ] , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> def mark_pending ( self , dispatch ) : <EOL> """<STR_LIT>""" <EOL> self . _st [ '<STR_LIT>' ] . append ( dispatch ) <EOL> def mark_sent ( self , dispatch ) : <EOL> """<STR_LIT>""" <EOL> self . _st [ '<STR_LIT>' ] . append ( dispatch ) <EOL> def mark_error ( self , dispatch , error_log , message_cls ) : <EOL> """<STR_LIT>""" <EOL> if message_cls . send_retry_limit is not None and ( dispatch . retry_count + <NUM_LIT:1> ) >= message_cls . send_retry_limit : <EOL> self . mark_failed ( dispatch , error_log ) <EOL> else : <EOL> dispatch . error_log = error_log <EOL> self . _st [ '<STR_LIT:error>' ] . append ( dispatch ) <EOL> def mark_failed ( self , dispatch , error_log ) : <EOL> """<STR_LIT>""" <EOL> dispatch . error_log = error_log <EOL> self . _st [ '<STR_LIT>' ] . append ( dispatch ) <EOL> def before_send ( self ) : <EOL> """<STR_LIT>""" <EOL> def after_send ( self ) : <EOL> """<STR_LIT>""" <EOL> def _process_messages ( self , messages , ignore_unknown_message_types = False ) : <EOL> """<STR_LIT>""" <EOL> self . _init_delivery_statuses_dict ( ) <EOL> with self . before_after_send_handling ( ) : <EOL> for message_id , message_data in messages . items ( ) : <EOL> message_model , dispatch_models = message_data <EOL> try : <EOL> message_cls = get_registered_message_type ( message_model . cls ) <EOL> except UnknownMessageTypeError : <EOL> if ignore_unknown_message_types : <EOL> continue <EOL> raise <EOL> message_type_cache = None <EOL> for dispatch in dispatch_models : <EOL> if not dispatch . message_cache : <EOL> if message_type_cache is None and not message_cls . has_dynamic_context : <EOL> message_type_cache = message_cls . compile ( message_model , self , dispatch = dispatch ) <EOL> dispatch . message_cache = message_type_cache or message_cls . compile ( <EOL> message_model , self , dispatch = dispatch <EOL> ) <EOL> self . send ( message_cls , message_model , dispatch_models ) <EOL> self . _update_dispatches ( ) <EOL> def _update_dispatches ( self ) : <EOL> """<STR_LIT>""" <EOL> Dispatch . log_dispatches_errors ( self . _st [ '<STR_LIT:error>' ] + self . _st [ '<STR_LIT>' ] ) <EOL> Dispatch . set_dispatches_statuses ( ** self . _st ) <EOL> self . _init_delivery_statuses_dict ( ) <EOL> def send ( self , message_cls , message_model , dispatch_models ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( self . __class__ . __name__ + '<STR_LIT>' ) </s>
