def tearDown(self): SmsBillable.objects.all().delete() SmsUsageFee.objects.all().delete() SmsUsageFeeCriteria.objects.all().delete() SmsGatewayFee.objects.all().delete() self.currency_usd.delete() for api_id, backend_id in self.backend_ids.iteritems(): SQLMobileBackend.load(backend_id, is_couch_id=True).delete()
def tearDown(self): SmsBillable.objects.all().delete() SmsUsageFee.objects.all().delete() SmsUsageFeeCriteria.objects.all().delete() SmsGatewayFee.objects.all().delete() self.currency_usd.delete() for api_id, backend_id in six.iteritems(self.backend_ids): SQLMobileBackend.load(backend_id, is_couch_id=True).delete() FakeTwilioMessageFactory.backend_message_id_to_price = {} super(TestUsageFee, self).tearDown()
def tearDown(self): SmsBillable.objects.all().delete() SmsUsageFee.objects.all().delete() SmsUsageFeeCriteria.objects.all().delete() SmsGatewayFee.objects.all().delete() self.currency_usd.delete() for api_id, backend_id in six.iteritems(self.backend_ids): SQLMobileBackend.load(backend_id, is_couch_id=True).delete() FakeTwilioMessageFactory.backend_message_id_to_price = {} super(TestUsageFee, self).tearDown()
def tearDown(self): SmsBillable.objects.all().delete() SmsGatewayFee.objects.all().delete() SmsGatewayFeeCriteria.objects.all().delete() SmsUsageFee.objects.all().delete() SmsUsageFeeCriteria.objects.all().delete() self.currency_usd.delete() self.other_currency.delete() SMS.by_domain(generator.TEST_DOMAIN).delete() for api_id, backend_id in self.backend_ids.iteritems(): SQLMobileBackend.load(backend_id, is_couch_id=True).delete()
def get_inbound_phone_entry(msg): if msg.backend_id: backend = SQLMobileBackend.load(msg.backend_id, is_couch_id=True) if toggles.INBOUND_SMS_LENIENCY.enabled(backend.domain): p = None if toggles.ONE_PHONE_NUMBER_MULTIPLE_CONTACTS.enabled( backend.domain): running_session_info = XFormsSessionSynchronization.get_running_session_info_for_channel( SMSChannel(backend_id=msg.backend_id, phone_number=msg.phone_number)) contact_id = running_session_info.contact_id if contact_id: p = PhoneNumber.get_phone_number_for_owner( contact_id, msg.phone_number) if p is not None: return (p, True) elif running_session_info.session_id: # This would be very unusual, as it would mean the supposedly running form session # is linked to a phone number, contact pair that doesn't exist in the PhoneNumber table notify_error( "Contact from running session has no match in PhoneNumber table. " "Only known way for this to happen is if you " "unregister a phone number for a contact " "while they are in an active session.", details={'running_session_info': running_session_info}) if not backend.is_global: p = PhoneNumber.get_two_way_number_with_domain_scope( msg.phone_number, backend.domains_with_access) return (p, p is not None) return (PhoneNumber.get_reserved_number(msg.phone_number), False)
def general_fields(self): fields = [ crispy.Field('name', css_class='input-xxlarge'), crispy.Field('description', css_class='input-xxlarge', rows="3"), crispy.Field('reply_to_phone_number', css_class='input-xxlarge'), ] if not self.is_global_backend: fields.extend([ crispy.Field( twbscrispy.PrependedText( 'give_other_domains_access', '', data_bind="checked: share_backend")), crispy.Div( 'authorized_domains', data_bind="visible: showAuthorizedDomains", ), ]) if self.backend_id: backend = SQLMobileBackend.load(self.backend_id) if backend.show_inbound_api_key_during_edit: self.fields[ 'inbound_api_key'].initial = backend.inbound_api_key fields.append(crispy.Field('inbound_api_key')) return fields
def general_fields(self): fields = [ crispy.Field('name', css_class='input-xxlarge'), crispy.Field('description', css_class='input-xxlarge', rows="3"), crispy.Field('reply_to_phone_number', css_class='input-xxlarge'), ] if not self.is_global_backend: fields.extend([ crispy.Field( twbscrispy.PrependedText( 'give_other_domains_access', '', data_bind="checked: share_backend" ) ), crispy.Div( 'authorized_domains', data_bind="visible: showAuthorizedDomains", ), ]) if self._cchq_backend_id: backend = SQLMobileBackend.load(self._cchq_backend_id) if backend.show_inbound_api_key_during_edit: self.fields['inbound_api_key'].initial = backend.inbound_api_key fields.append(crispy.Field('inbound_api_key')) return fields
def get_infobip_message(backend_instance, backend_message_id): from corehq.messaging.smsbackends.infobip.models import InfobipBackend try: infobip_backend = SQLMobileBackend.load( backend_instance, api_id=InfobipBackend.get_api_id(), is_couch_id=True, include_deleted=True, ) config = infobip_backend.config api_channel = '/sms/1' api_suffix = '/reports' if config.scenario_key: api_channel = '/omni/1' headers = { 'Authorization': f'App {config.auth_token}', 'Content-Type': 'application/json', 'Accept': 'application/json' } parameters = {'messageId': backend_message_id} messages = _get_infobip_message_details(api_channel, api_suffix, config, headers, parameters) if not messages: api_suffix = '/logs' messages = _get_infobip_message_details(api_channel, api_suffix, config, headers, parameters) return messages[0] except Exception as e: raise RetryBillableTaskException(str(e))
def test_load(self): backend = SQLTestSMSBackend.objects.create( name='BACKEND', is_global=True, hq_api_id=SQLTestSMSBackend.get_api_id(), ) self.assertBackendsEqual( SQLMobileBackend.load(backend.pk), backend ) self.assertBackendsEqual( SQLMobileBackend.load(backend.pk, api_id=SQLTestSMSBackend.get_api_id()), backend ) self.assertBackendsEqual( SQLMobileBackend.load(backend.couch_id, is_couch_id=True), backend ) self.assertBackendsEqual( SQLMobileBackend.load( backend.couch_id, api_id=SQLTestSMSBackend.get_api_id(), is_couch_id=True ), backend ) backend.soft_delete() with self.assertRaises(SQLMobileBackend.DoesNotExist): SQLMobileBackend.load(backend.pk, api_id=SQLTestSMSBackend.get_api_id()) with self.assertRaises(SQLMobileBackend.DoesNotExist): SQLMobileBackend.load( backend.couch_id, api_id=SQLTestSMSBackend.get_api_id(), is_couch_id=True ) with self.assertRaises(BadSMSConfigException): SQLMobileBackend.load(backend.pk, api_id='this-api-id-does-not-exist') backend.delete()
def _get_twilio_client(backend_instance): from corehq.messaging.smsbackends.twilio.models import SQLTwilioBackend twilio_backend = SQLMobileBackend.load( backend_instance, api_id=SQLTwilioBackend.get_api_id(), is_couch_id=True, include_deleted=True ) config = twilio_backend.config return TwilioRestClient(config.account_sid, config.auth_token)
def tearDown(self): SmsBillable.objects.all().delete() SmsGatewayFee.objects.all().delete() SmsGatewayFeeCriteria.objects.all().delete() SmsUsageFee.objects.all().delete() SmsUsageFeeCriteria.objects.all().delete() self.currency_usd.delete() self.other_currency.delete() SMS.by_domain(generator.TEST_DOMAIN).delete() for api_id, backend_id in self.backend_ids.iteritems(): SQLMobileBackend.load(backend_id, is_couch_id=True).delete() FakeTwilioMessageFactory.backend_message_id_to_price = {} super(TestGatewayFee, self).tearDown()
def tearDown(self): SmsBillable.objects.all().delete() SmsGatewayFee.objects.all().delete() SmsGatewayFeeCriteria.objects.all().delete() SmsUsageFee.objects.all().delete() SmsUsageFeeCriteria.objects.all().delete() self.currency_usd.delete() self.other_currency.delete() SMS.by_domain(generator.TEST_DOMAIN).delete() for api_id, backend_id in self.backend_ids.iteritems(): SQLMobileBackend.load(backend_id, is_couch_id=True).delete() FakeTwilioMessageFactory.backend_message_id_to_price = {} super(TestGatewayFee, self).tearDown()
def get_inbound_phone_entry(msg): if msg.backend_id: backend = SQLMobileBackend.load(msg.backend_id, is_couch_id=True) if not backend.is_global and toggles.INBOUND_SMS_LENIENCY.enabled( backend.domain): p = PhoneNumber.get_two_way_number_with_domain_scope( msg.phone_number, backend.domains_with_access) return (p, p is not None) return (PhoneNumber.get_reserved_number(msg.phone_number), False)
def _get_twilio_client(backend_instance): from corehq.messaging.smsbackends.twilio.models import SQLTwilioBackend twilio_backend = SQLMobileBackend.load( backend_instance, api_id=SQLTwilioBackend.get_api_id(), is_couch_id=True, include_deleted=True, ) config = twilio_backend.config return Client(config.account_sid, config.auth_token)
def _get_gateway_fee(cls, backend_api_id, backend_id, phone_number, direction, couch_id, backend_message_id, domain): country_code, national_number = get_country_code_and_national_number(phone_number) backend_instance = None if backend_id is not None: backend_instance = SQLMobileBackend.load( backend_id, api_id=backend_api_id, is_couch_id=True, include_deleted=True, ) is_gateway_billable = ( backend_id is None or backend_instance.is_global or toggles.ENABLE_INCLUDE_SMS_GATEWAY_CHARGING.enabled(domain) ) direct_gateway_fee = gateway_fee = multipart_count = conversion_rate = None if is_gateway_billable: if backend_instance and backend_instance.using_api_to_get_fees: if backend_message_id: direct_gateway_fee, multipart_count = \ cls.get_charge_details_through_api(backend_instance, backend_message_id) gateway_fee = SmsGatewayFee.get_by_criteria( backend_api_id, direction, ) else: log_smsbillables_error( "Could not create gateway fee for message %s: no backend_message_id" % couch_id ) else: gateway_fee = SmsGatewayFee.get_by_criteria( backend_api_id, direction, backend_instance=backend_id, country_code=country_code, national_number=national_number, ) if gateway_fee: conversion_rate = cls.get_conversion_rate(gateway_fee) else: log_smsbillables_error( "No matching gateway fee criteria for SMS %s" % couch_id ) return _ProviderChargeInfo( direct_gateway_fee, gateway_fee, multipart_count, conversion_rate )
def get_backend_name(backend_id): """ Returns None if the backend is not found, otherwise returns the backend's name. """ if not backend_id: return None from corehq.apps.sms.models import SQLMobileBackend try: return SQLMobileBackend.load(backend_id, is_couch_id=True).name except: return None
def get_backend_name(backend_id): """ Returns None if the backend is not found, otherwise returns the backend's name. """ if not backend_id: return None from corehq.apps.sms.models import SQLMobileBackend try: return SQLMobileBackend.load(backend_id, is_couch_id=True).name except: return None
def get_inbound_phone_entry(msg): if msg.backend_id: backend = SQLMobileBackend.load(msg.backend_id, is_couch_id=True) if not backend.is_global and toggles.INBOUND_SMS_LENIENCY.enabled(backend.domain): p = PhoneNumber.get_two_way_number_with_domain_scope(msg.phone_number, backend.domains_with_access) return ( p, p is not None ) return ( PhoneNumber.get_reserved_number(msg.phone_number), False )
def _get_backend_tag(backend=None, backend_id=None): assert not (backend_id and backend) if backend_id: try: backend = SQLMobileBackend.load(backend_id, is_couch_id=True) except Exception: backend = None if not backend: return 'unknown' elif backend.is_global: return backend.name else: return f'{backend.domain}/{backend.name}'
def _clean_backend_id(self, backend_id): try: backend_id = int(backend_id) except (ValueError, TypeError): raise ValidationError(_("Invalid Backend Specified.")) try: backend = SQLMobileBackend.load(backend_id) except: raise ValidationError(_("Invalid Backend Specified.")) if (backend.deleted or not backend.is_global or backend.backend_type != SQLMobileBackend.SMS): raise ValidationError(_("Invalid Backend Specified.")) return backend_id
def _clean_backend_id(self, backend_id): try: backend_id = int(backend_id) except (ValueError, TypeError): raise ValidationError(_("Invalid Backend Specified.")) try: backend = SQLMobileBackend.load(backend_id) except: raise ValidationError(_("Invalid Backend Specified.")) if ( backend.deleted or not backend.is_global or backend.backend_type != SQLMobileBackend.SMS ): raise ValidationError(_("Invalid Backend Specified.")) return backend_id
def _sms_backend_is_global(sms_backend_id): return SQLMobileBackend.load( sms_backend_id, is_couch_id=True, include_deleted=True ).is_global
def _sms_backend_is_global(sms_backend_id): return SQLMobileBackend.load( sms_backend_id, is_couch_id=True, include_deleted=True ).is_global