コード例 #1
0
 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()
コード例 #2
0
    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()
コード例 #3
0
ファイル: test_usage_fees.py プロジェクト: dimagi/commcare-hq
    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()
コード例 #4
0
    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()
コード例 #5
0
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)
コード例 #6
0
    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
コード例 #7
0
ファイル: forms.py プロジェクト: kkrampa/commcare-hq
    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
コード例 #8
0
ファイル: utils.py プロジェクト: dankohn/commcare-hq
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))
コード例 #9
0
ファイル: test_backends.py プロジェクト: ansarbek/commcare-hq
    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()
コード例 #10
0
ファイル: utils.py プロジェクト: dimagi/commcare-hq
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)
コード例 #11
0
    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()
コード例 #12
0
    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()
コード例 #13
0
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)
コード例 #14
0
ファイル: utils.py プロジェクト: tstalka/commcare-hq
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)
コード例 #15
0
ファイル: models.py プロジェクト: mxdxlx/commcare-hq
    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
        )
コード例 #16
0
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
コード例 #17
0
ファイル: util.py プロジェクト: dimagi/commcare-hq
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
コード例 #18
0
ファイル: api.py プロジェクト: kkrampa/commcare-hq
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
    )
コード例 #19
0
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}'
コード例 #20
0
    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
コード例 #21
0
    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
コード例 #22
0
def _sms_backend_is_global(sms_backend_id):
    return SQLMobileBackend.load(
        sms_backend_id,
        is_couch_id=True,
        include_deleted=True
    ).is_global
コード例 #23
0
ファイル: models.py プロジェクト: dimagi/commcare-hq
def _sms_backend_is_global(sms_backend_id):
    return SQLMobileBackend.load(
        sms_backend_id,
        is_couch_id=True,
        include_deleted=True
    ).is_global