def send_sms(self, from_tel, to_tel, message, message_type="Unknown", message_tracker=None): message = strip_accents(message) if is_empty(from_tel): raise NoSMSCException() organization_setting = OrganizationFinder().find_organization_setting( from_tel) is_there_orgnization_with_to_number = OrganizationFinder( ).find_organization_setting(to_tel) != None if (is_there_orgnization_with_to_number): return False smsc = None if organization_setting is not None and organization_setting.outgoing_number is not None: smsc = organization_setting.outgoing_number.smsc if smsc is None: raise NoSMSCException() socket.setdefaulttimeout(120) logger.debug("Posting sms to %s" % settings.VUMI_API_URL) if settings.USE_NEW_VUMI: client = VumiApiClient( connection=Connection(smsc.vumi_username, smsc.vumi_username, base_url=settings.VUMI_API_URL)) sms_response = client.send_sms(to_addr=to_tel, from_addr=from_tel, content=message.encode('utf-8'), transport_name=smsc.vumi_username) result = sms_response[0] else: try: client = VumiClient(None, None, connection=Connection( smsc.vumi_username, smsc.vumi_username, base_url=settings.VUMI_API_URL)) resp = client.send_sms(to_msisdn=to_tel, from_msisdn=from_tel, message=message.encode('utf-8')) response_object = json.loads(resp.content) if response_object: log_sms(to_tel, from_tel, message, organization_setting.organization, response_object[0].get('id'), smsc.vumi_username, message_type) result = True except (URLError, VumiInvalidDestinationNumberException) as err: logger.exception('Unable to send sms. %s' % err) result = False except socket.timeout: logger.exception( 'Request timed-out. Organization: %s, From: %s, To: %s.' % (organization_setting, from_tel, to_tel)) result = False if result and message_tracker is not None: increment_dict = {'send_message_count': 1} if smsc.vumi_username in settings.SMSC_WITHOUT_STATUS_REPORT: increment_dict.update({couter_map.get(message_type): 1}) message_tracker.increment_message_count_for(**increment_dict) return result
def _get_organization(request): _from, _to = _get_from_and_to_numbers(request) return OrganizationFinder().find(_from, _to)
def test_should_return_error_when_organization_doesnot_exists(self): organization, error = OrganizationFinder().find("123", "678") # this organization is created in test data self.assertEqual(u'No organization found for telephone number 678', error)
def test_should_return_organization(self): organization, error = OrganizationFinder().find( "123", DEFAULT_TEST_ORG_TEL_NO) # this organization is created in test data self.assertEqual(DEFAULT_TEST_ORG_ID, organization.org_id)
def test_should_find_organization_for_trial_account(self): organization, error = OrganizationFinder().find( TRIAL_ACCOUNT_DATA_SENDER_MOBILE_NO, self.trial_number) # this organization is created in test data self.assertEqual(TRIAL_ACCOUNT_ORGANIZATION_ID, organization.org_id)