<s> from django . core . urlresolvers import reverse , NoReverseMatch <EOL> from django . utils . crypto import salted_hmac <EOL> from django . utils . translation import ugettext as _ <EOL> from django . shortcuts import redirect <EOL> from django . template . loader import render_to_string <EOL> from django . utils . six import string_types <EOL> from . . utils import Recipient , recipients , get_site_url , get_registered_messenger_object <EOL> from . . models import Message , Dispatch , Subscription <EOL> from . . signals import sig_unsubscribe_success , sig_unsubscribe_failed , sig_mark_read_success , sig_mark_read_failed <EOL> from . . exceptions import UnknownMessengerError <EOL> APP_URLS_ATTACHED = None <EOL> class MessageBase ( object ) : <EOL> """<STR_LIT>""" <EOL> supported_messengers = [ ] <EOL> alias = None <EOL> title = _ ( '<STR_LIT>' ) <EOL> priority = None <EOL> has_dynamic_context = False <EOL> template_ext = '<STR_LIT>' <EOL> template = None <EOL> send_retry_limit = <NUM_LIT:10> <EOL> allow_user_subscription = True <EOL> _message_model = None <EOL> _dispatch_models = None <EOL> SIMPLE_TEXT_ID = '<STR_LIT>' <EOL> def __init__ ( self , context = None , template_path = None ) : <EOL> """<STR_LIT>""" <EOL> context_base = { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : False <EOL> } <EOL> if context is not None : <EOL> self . update_context ( context_base , context , template_path = template_path ) <EOL> self . context = context_base <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> def get_context ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . context <EOL> def schedule ( self , recipients = None , sender = None , priority = None ) : <EOL> """<STR_LIT>""" <EOL> if priority is None : <EOL> priority = self . priority <EOL> self . _message_model , self . _dispatch_models = Message . create ( <EOL> self . get_alias ( ) , self . get_context ( ) , recipients = recipients , sender = sender , priority = priority <EOL> ) <EOL> return self . _message_model , self . _dispatch_models <EOL> @ classmethod <EOL> def recipients ( cls , messenger , addresses ) : <EOL> """<STR_LIT>""" <EOL> return recipients ( messenger , addresses ) <EOL> @ classmethod <EOL> def get_subscribers ( cls ) : <EOL> """<STR_LIT>""" <EOL> subscribers_raw = Subscription . get_for_message_cls ( cls . alias ) <EOL> subscribers = [ ] <EOL> for subscriber in subscribers_raw : <EOL> messenger_cls = subscriber . messenger_cls <EOL> address = subscriber . address <EOL> recipient = subscriber . recipient <EOL> if address is None : <EOL> try : <EOL> address = get_registered_messenger_object ( messenger_cls ) . get_address ( recipient ) <EOL> except UnknownMessengerError : <EOL> pass <EOL> if address and isinstance ( address , string_types ) : <EOL> subscribers . append ( Recipient ( messenger_cls , recipient , address ) ) <EOL> return subscribers <EOL> @ classmethod <EOL> def get_dispatch_hash ( cls , dispatch_id , message_id ) : <EOL> """<STR_LIT>""" <EOL> return salted_hmac ( '<STR_LIT:%s>' % dispatch_id , '<STR_LIT>' % ( message_id , dispatch_id ) ) . hexdigest ( ) <EOL> @ classmethod <EOL> def get_mark_read_directive ( cls , message_model , dispatch_model ) : <EOL> """<STR_LIT>""" <EOL> return cls . _get_url ( '<STR_LIT>' , message_model , dispatch_model ) <EOL> @ classmethod <EOL> def get_unsubscribe_directive ( cls , message_model , dispatch_model ) : <EOL> """<STR_LIT>""" <EOL> return cls . _get_url ( '<STR_LIT>' , message_model , dispatch_model ) <EOL> @ classmethod <EOL> def _get_url ( cls , name , message_model , dispatch_model ) : <EOL> """<STR_LIT>""" <EOL> global APP_URLS_ATTACHED <EOL> url = '<STR_LIT>' <EOL> if dispatch_model is None : <EOL> return url <EOL> if APP_URLS_ATTACHED != False : <EOL> hashed = cls . get_dispatch_hash ( dispatch_model . id , message_model . id ) <EOL> try : <EOL> url = reverse ( name , args = [ message_model . id , dispatch_model . id , hashed ] ) <EOL> url = '<STR_LIT>' % ( get_site_url ( ) , url ) <EOL> except NoReverseMatch : <EOL> if APP_URLS_ATTACHED is None : <EOL> APP_URLS_ATTACHED = False <EOL> return url <EOL> @ classmethod <EOL> def handle_unsubscribe_request ( cls , request , message , dispatch , hash_is_valid , redirect_to ) : <EOL> """<STR_LIT>""" <EOL> if hash_is_valid : <EOL> Subscription . cancel ( <EOL> dispatch . recipient_id or dispatch . address , cls . alias , dispatch . messenger <EOL> ) <EOL> signal = sig_unsubscribe_success <EOL> else : <EOL> signal = sig_unsubscribe_failed <EOL> signal . send ( cls , request = request , message = message , dispatch = dispatch ) <EOL> return redirect ( redirect_to ) <EOL> @ classmethod <EOL> def handle_mark_read_request ( cls , request , message , dispatch , hash_is_valid , redirect_to ) : <EOL> """<STR_LIT>""" <EOL> if hash_is_valid : <EOL> dispatch . mark_read ( ) <EOL> dispatch . save ( ) <EOL> signal = sig_mark_read_success <EOL> else : <EOL> signal = sig_mark_read_failed <EOL> signal . send ( cls , request = request , message = message , dispatch = dispatch ) <EOL> return redirect ( redirect_to ) <EOL> @ classmethod <EOL> def get_template ( cls , message , messenger ) : <EOL> """<STR_LIT>""" <EOL> template = message . context . get ( '<STR_LIT>' , None ) <EOL> if template : <EOL> return template <EOL> if cls . template is None : <EOL> cls . template = '<STR_LIT>' % ( <EOL> cls . get_alias ( ) , messenger . get_alias ( ) , cls . template_ext <EOL> ) <EOL> return cls . template <EOL> @ classmethod <EOL> def compile ( cls , message , messenger , dispatch = None ) : <EOL> """<STR_LIT>""" <EOL> if message . context . get ( '<STR_LIT>' , False ) : <EOL> context = message . context <EOL> context . update ( { <EOL> '<STR_LIT>' : get_site_url ( ) , <EOL> '<STR_LIT>' : cls . get_unsubscribe_directive ( message , dispatch ) , <EOL> '<STR_LIT>' : cls . get_mark_read_directive ( message , dispatch ) , <EOL> '<STR_LIT>' : message , <EOL> '<STR_LIT>' : dispatch <EOL> } ) <EOL> context = cls . get_template_context ( context ) <EOL> return render_to_string ( cls . get_template ( message , messenger ) , context ) <EOL> return message . context [ cls . SIMPLE_TEXT_ID ] <EOL> @ classmethod <EOL> def get_template_context ( cls , context ) : <EOL> """<STR_LIT>""" <EOL> return context <EOL> @ classmethod <EOL> def update_context ( cls , base_context , str_or_dict , template_path = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( str_or_dict , dict ) : <EOL> base_context . update ( str_or_dict ) <EOL> base_context [ '<STR_LIT>' ] = True <EOL> else : <EOL> base_context [ cls . SIMPLE_TEXT_ID ] = str_or_dict <EOL> if cls . SIMPLE_TEXT_ID in str_or_dict : <EOL> base_context [ '<STR_LIT>' ] = False <EOL> base_context [ '<STR_LIT>' ] = template_path <EOL> @ classmethod <EOL> def prepare_dispatches ( cls , message , recipients = None ) : <EOL> """<STR_LIT>""" <EOL> return Dispatch . create ( message , recipients or cls . get_subscribers ( ) ) </s>
