<s> from os . path import join , abspath , dirname <EOL> import threading <EOL> from twilio . rest import TwilioRestClient <EOL> from jinja2 import Environment , FileSystemLoader <EOL> from amonone . utils . dates import dateformat_local <EOL> from amonone . sms . models import sms_model <EOL> def _send_sms_in_thread ( client = None , from_ = None , to = None , body = None ) : <EOL> try : <EOL> client . sms . messages . create ( to = to , <EOL> from_ = from_ , <EOL> body = body ) <EOL> except Exception , e : <EOL> raise e <EOL> logging . exception ( "<STR_LIT>" ) <EOL> def send_test_sms ( recepient = None ) : <EOL> details = sms_model . get ( ) <EOL> client = TwilioRestClient ( details [ '<STR_LIT>' ] , details [ '<STR_LIT>' ] ) <EOL> t = threading . Thread ( target = _send_sms_in_thread , <EOL> kwargs = { "<STR_LIT>" : client , <EOL> "<STR_LIT>" : details [ '<STR_LIT>' ] , <EOL> "<STR_LIT:to>" : recepient , <EOL> "<STR_LIT:body>" : "<STR_LIT>" <EOL> } ) <EOL> t . start ( ) <EOL> def render_template ( alert = None , template = None ) : <EOL> ROOT = abspath ( dirname ( __file__ ) ) <EOL> TEMPLATES_DIR = join ( ROOT , '<STR_LIT>' ) <EOL> env = Environment ( loader = FileSystemLoader ( TEMPLATES_DIR ) ) <EOL> env . filters [ '<STR_LIT>' ] = dateformat_local <EOL> template_file = env . get_template ( template ) <EOL> rendered_template = template_file . render ( alert = alert ) <EOL> return rendered_template <EOL> def send_sms ( alert = None , recepients = None , template = None ) : <EOL> details = sms_model . get ( ) <EOL> try : <EOL> client = TwilioRestClient ( details [ '<STR_LIT>' ] , details [ '<STR_LIT>' ] ) <EOL> except : <EOL> client = None <EOL> if client != None : <EOL> body = render_template ( alert = alert , template = template ) <EOL> for recepient in recepients : <EOL> t = threading . Thread ( target = _send_sms_in_thread , <EOL> kwargs = { "<STR_LIT>" : client , <EOL> "<STR_LIT>" : details [ '<STR_LIT>' ] , <EOL> "<STR_LIT:to>" : recepient [ '<STR_LIT>' ] , <EOL> "<STR_LIT:body>" : body <EOL> } <EOL> ) <EOL> t . start ( ) </s>
