<s> import urllib <EOL> from django . conf import settings <EOL> import urllib2 <EOL> from corehq . apps . sms . models import SQLSMSBackend , SMS <EOL> from dimagi . ext . couchdbkit import * <EOL> from corehq . messaging . smsbackends . mach . forms import MachBackendForm <EOL> MACH_URL = "<STR_LIT>" <EOL> class SyniverseException ( Exception ) : <EOL> pass <EOL> class SQLMachBackend ( SQLSMSBackend ) : <EOL> class Meta : <EOL> app_label = '<STR_LIT>' <EOL> proxy = True <EOL> @ classmethod <EOL> def get_available_extra_fields ( cls ) : <EOL> return [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:password>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> @ classmethod <EOL> def get_api_id ( cls ) : <EOL> return '<STR_LIT>' <EOL> @ classmethod <EOL> def get_generic_name ( cls ) : <EOL> return "<STR_LIT>" <EOL> @ classmethod <EOL> def get_form_class ( cls ) : <EOL> return MachBackendForm <EOL> def get_sms_rate_limit ( self ) : <EOL> return self . config . max_sms_per_second * <NUM_LIT> <EOL> def handle_error_response ( self , msg , response ) : <EOL> words = response . split ( ) <EOL> error_code = '<STR_LIT>' <EOL> if len ( words ) > <NUM_LIT:1> : <EOL> error_code = words [ <NUM_LIT:1> ] <EOL> config_error = { <EOL> '<STR_LIT>' : SMS . ERROR_INVALID_DESTINATION_NUMBER , <EOL> '<STR_LIT>' : SMS . ERROR_MESSAGE_TOO_LONG , <EOL> } . get ( error_code ) <EOL> if config_error : <EOL> msg . set_system_error ( config_error ) <EOL> return <EOL> raise SyniverseException ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( error_code , self . pk ) <EOL> ) <EOL> def handle_response ( self , msg , response ) : <EOL> if not isinstance ( response , basestring ) : <EOL> raise SyniverseException ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % self . pk <EOL> ) <EOL> response = response . strip ( ) . upper ( ) <EOL> if response . startswith ( '<STR_LIT>' ) : <EOL> return <EOL> elif response . startswith ( '<STR_LIT>' ) : <EOL> self . handle_error_response ( msg , response ) <EOL> else : <EOL> raise SyniverseException ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % self . pk <EOL> ) <EOL> def send ( self , msg , * args , ** kwargs ) : <EOL> config = self . config <EOL> params = { <EOL> '<STR_LIT:id>' : config . account_id , <EOL> '<STR_LIT>' : config . password , <EOL> '<STR_LIT>' : config . sender_id , <EOL> '<STR_LIT>' : msg . phone_number , <EOL> } <EOL> try : <EOL> text = msg . text . encode ( '<STR_LIT>' ) <EOL> params [ '<STR_LIT>' ] = text <EOL> except UnicodeEncodeError : <EOL> params [ '<STR_LIT>' ] = msg . text . encode ( '<STR_LIT>' ) . encode ( '<STR_LIT>' ) <EOL> params [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> url = '<STR_LIT>' % ( MACH_URL , urllib . urlencode ( params ) ) <EOL> resp = urllib2 . urlopen ( url , timeout = settings . SMS_GATEWAY_TIMEOUT ) . read ( ) <EOL> self . handle_response ( msg , resp ) <EOL> return resp </s>
