Exemplo n.º 1
0
def bootstrap_mach_gateway(orm):
    currency_class = orm['accounting.Currency'] if orm else Currency
    sms_gateway_fee_class = orm[
        'smsbillables.SmsGatewayFee'] if orm else SmsGatewayFee
    sms_gateway_fee_criteria_class = orm[
        'smsbillables.SmsGatewayFeeCriteria'] if orm else SmsGatewayFeeCriteria

    workbook = xlrd.open_workbook(
        'corehq/apps/smsbillables/management/'
        'commands/pricing_data/Syniverse_coverage_list_DIAMONDplus.xls')
    table = workbook.sheet_by_index(0)

    data = {}
    try:
        row = 7
        while True:
            if table.cell_value(row, 6) == 'yes':
                country_code = int(table.cell_value(row, 0))
                if not (country_code in data):
                    data[country_code] = []
                subscribers = table.cell_value(row, 10).replace('.', '')
                try:
                    data[country_code].append(
                        (table.cell_value(row, 9), int(subscribers)))
                except ValueError:
                    logger.info('Incomplete data for country code %d' %
                                country_code)
            row += 1
    except IndexError:
        pass

    for country_code in data:
        total_subscribers = 0
        weighted_price = 0
        for price, subscribers in data[country_code]:
            total_subscribers += subscribers
            weighted_price += price * subscribers
        weighted_price = weighted_price / total_subscribers
        SmsGatewayFee.create_new(
            MachBackend.get_api_id(),
            OUTGOING,
            weighted_price,
            country_code=country_code,
            currency=currency_class.objects.get(code="EUR"),
            fee_class=sms_gateway_fee_class,
            criteria_class=sms_gateway_fee_criteria_class,
        )

    # Fee for invalid phonenumber
    SmsGatewayFee.create_new(
        MachBackend.get_api_id(),
        OUTGOING,
        0.0225,
        country_code=None,
        currency=currency_class.objects.get(code="EUR"),
        fee_class=sms_gateway_fee_class,
        criteria_class=sms_gateway_fee_criteria_class,
    )

    logger.info("Updated MACH/Syniverse gateway fees.")
def bootstrap_mach_gateway(orm):
    currency_class = orm['accounting.Currency'] if orm else Currency
    sms_gateway_fee_class = orm['smsbillables.SmsGatewayFee'] if orm else SmsGatewayFee
    sms_gateway_fee_criteria_class = orm['smsbillables.SmsGatewayFeeCriteria'] if orm else SmsGatewayFeeCriteria

    workbook = xlrd.open_workbook('corehq/apps/smsbillables/management/'
                                  'commands/pricing_data/Syniverse_coverage_list_DIAMONDplus.xls')
    table = workbook.sheet_by_index(0)

    data = {}
    try:
        row = 7
        while True:
            if table.cell_value(row, 6) == 'yes':
                country_code = int(table.cell_value(row, 0))
                if not(country_code in data):
                    data[country_code] = []
                subscribers = table.cell_value(row, 10).replace('.', '')
                try:
                    data[country_code].append(
                        (table.cell_value(row, 9), int(subscribers)))
                except ValueError:
                    logger.info('Incomplete data for country code %d' % country_code)
            row += 1
    except IndexError:
        pass

    for country_code in data:
        total_subscribers = 0
        weighted_price = 0
        for price, subscribers in data[country_code]:
            total_subscribers += subscribers
            weighted_price += price * subscribers
        weighted_price = weighted_price / total_subscribers
        SmsGatewayFee.create_new(
            MachBackend.get_api_id(),
            OUTGOING,
            weighted_price,
            country_code=country_code,
            currency=currency_class.objects.get(code="EUR"),
            fee_class=sms_gateway_fee_class,
            criteria_class=sms_gateway_fee_criteria_class,
        )

    # Fee for invalid phonenumber
    SmsGatewayFee.create_new(
        MachBackend.get_api_id(),
        OUTGOING,
        0.0225,
        country_code=None,
        currency=currency_class.objects.get(code="EUR"),
        fee_class=sms_gateway_fee_class,
        criteria_class=sms_gateway_fee_criteria_class,
    )

    logger.info("Updated MACH/Syniverse gateway fees.")
    def handle(self, *args, **options):
        workbook = xlrd.open_workbook('corehq/apps/smsbillables/management/'
                                      'commands/pricing_data/Syniverse_coverage_list_DIAMONDplus.xls')
        table = workbook.sheet_by_index(0)

        data = {}
        try:
            row = 7
            while True:
                if table.cell_value(row, 6) == 'yes':
                    country_code = int(table.cell_value(row, 0))
                    if not(country_code in data):
                        data[country_code] = []
                    subscribers = table.cell_value(row,10).replace('.', '')
                    try:
                        data[country_code].append(
                            (table.cell_value(row, 9), int(subscribers)))
                    except ValueError:
                        print 'Incomplete data for country code %d' % country_code
                row += 1
        except IndexError:
            pass

        for country_code in data:
            total_subscribers = 0
            weighted_price = 0
            for price, subscribers in data[country_code]:
                total_subscribers += subscribers
                weighted_price += price * subscribers
            weighted_price = weighted_price / total_subscribers
            SmsGatewayFee.create_new(MachBackend.get_api_id(), OUTGOING, weighted_price,
                                     country_code=country_code, currency=Currency.objects.get(code="EUR"))

        print "Updated MACH/Syniverse gateway fees."
 def handle(self, *args, **options):
     backends = MobileBackend.view("sms/old_mobile_backend", include_docs=True).all()
     for backend in backends:
         print "Processing backend %s" % backend._id
         if backend._id == "MOBILE_BACKEND_UNICEL":
             backend = UnicelBackend.wrap(backend.to_json())
             backend.doc_type = "UnicelBackend"
             backend.domain = None
             backend.name = backend._id
             backend.authorized_domains = []
             backend.is_global = True
             backend.username = settings.UNICEL_CONFIG.get("username")
             backend.password = settings.UNICEL_CONFIG.get("password")
             backend.sender = settings.UNICEL_CONFIG.get("sender")
             backend.reply_to_phone_number = settings.UNICEL_CONFIG.get("receive_phone", None)
             backend.outbound_module = None
             backend.outbound_params = None
             backend.save()
         elif backend._id == "MOBILE_BACKEND_MACH":
             backend = MachBackend.wrap(backend.to_json())
             backend.doc_type = "MachBackend"
             backend.domain = None
             backend.name = backend._id
             backend.authorized_domains = []
             backend.is_global = True
             backend.account_id = re.match(r".*id=(.+?)&", settings.SMS_GATEWAY_PARAMS).group(1)
             backend.password = re.match(r".*pw=(.+?)&", settings.SMS_GATEWAY_PARAMS).group(1)
             backend.sender_id = "DIMAGI"
             backend.outbound_module = None
             backend.outbound_params = None
             backend.save()
         elif backend.outbound_module == "corehq.apps.tropo.api":
             backend = TropoBackend.wrap(backend.to_json())
             backend.doc_type = "TropoBackend"
             backend.domain = None
             backend.name = backend._id
             backend.authorized_domains = []
             backend.is_global = True
             backend.messaging_token = backend.outbound_params.get("messaging_token")
             backend.outbound_module = None
             backend.outbound_params = None
             backend.save()
         elif backend.outbound_module == "corehq.apps.sms.backend.http_api":
             print "REMINDER: Need to set global / domain for backend %s" % backend._id
             backend = HttpBackend.wrap(backend.to_json())
             backend.doc_type = "HttpBackend"
             backend.domain = None
             backend.name = backend._id
             backend.authorized_domains = []
             backend.is_global = False
             backend.url = backend.outbound_params.get("url")
             backend.message_param = backend.outbound_params.get("message_param")
             backend.number_param = backend.outbound_params.get("number_param")
             backend.include_plus = backend.outbound_params.get("include_plus", False)
             backend.method = backend.outbound_params.get("method", "GET")
             backend.additional_params = backend.outbound_params.get("additional_params", {})
             backend.outbound_module = None
             backend.outbound_params = None
             backend.save()
         elif backend.outbound_module == "corehq.apps.sms.test_backend":
             backend = TestSMSBackend.wrap(backend.to_json())
             backend.doc_type = "TestSMSBackend"
             backend.domain = None
             backend.name = backend._id
             backend.authorized_domains = []
             backend.is_global = True
             backend.outbound_module = None
             backend.outbound_params = None
             backend.save()
class Command(BaseCommand):
    args = ""
    help = "Migrate backends to support backend refactor."

    # Migrate all backends
    def handle(self, *args, **options):
        backends = MobileBackend.view("sms/old_mobile_backend", include_docs=True).all()
        for backend in backends:
            print "Processing backend %s" % backend._id
            if backend._id == "MOBILE_BACKEND_UNICEL":
                backend = UnicelBackend.wrap(backend.to_json())
                backend.doc_type = "UnicelBackend"
                backend.domain = None
                backend.name = backend._id
                backend.authorized_domains = []
                backend.is_global = True
                backend.username = settings.UNICEL_CONFIG.get("username")
                backend.password = settings.UNICEL_CONFIG.get("password")
                backend.sender = settings.UNICEL_CONFIG.get("sender")
                backend.reply_to_phone_number = settings.UNICEL_CONFIG.get("receive_phone", None)
                backend.outbound_module = None
                backend.outbound_params = None
                backend.save()
            elif backend._id == "MOBILE_BACKEND_MACH":
                backend = MachBackend.wrap(backend.to_json())
                backend.doc_type = "MachBackend"
                backend.domain = None
                backend.name = backend._id
                backend.authorized_domains = []
                backend.is_global = True
                backend.account_id = re.match(r".*id=(.+?)&", settings.SMS_GATEWAY_PARAMS).group(1)
                backend.password = re.match(r".*pw=(.+?)&", settings.SMS_GATEWAY_PARAMS).group(1)
                backend.sender_id = "DIMAGI"
                backend.outbound_module = None
                backend.outbound_params = None
                backend.save()
            elif backend.outbound_module == "corehq.apps.tropo.api":
                backend = TropoBackend.wrap(backend.to_json())
                backend.doc_type = "TropoBackend"
                backend.domain = None
                backend.name = backend._id
                backend.authorized_domains = []
                backend.is_global = True
                backend.messaging_token = backend.outbound_params.get("messaging_token")
                backend.outbound_module = None
                backend.outbound_params = None
                backend.save()
            elif backend.outbound_module == "corehq.apps.sms.backend.http_api":
                print "REMINDER: Need to set global / domain for backend %s" % backend._id
                backend = HttpBackend.wrap(backend.to_json())
                backend.doc_type = "HttpBackend"
                backend.domain = None
                backend.name = backend._id
                backend.authorized_domains = []
                backend.is_global = False
                backend.url = backend.outbound_params.get("url")
                backend.message_param = backend.outbound_params.get("message_param")
                backend.number_param = backend.outbound_params.get("number_param")
                backend.include_plus = backend.outbound_params.get("include_plus", False)
                backend.method = backend.outbound_params.get("method", "GET")
                backend.additional_params = backend.outbound_params.get("additional_params", {})
                backend.outbound_module = None
                backend.outbound_params = None
                backend.save()
            elif backend.outbound_module == "corehq.apps.sms.test_backend":
                backend = TestSMSBackend.wrap(backend.to_json())
                backend.doc_type = "TestSMSBackend"
                backend.domain = None
                backend.name = backend._id
                backend.authorized_domains = []
                backend.is_global = True
                backend.outbound_module = None
                backend.outbound_params = None
                backend.save()

    # If no unicel backend was present, create one
    try:
        MobileBackend.get("MOBILE_BACKEND_UNICEL")
    except ResourceNotFound:
        backend = UnicelBackend(
            domain = None,
            name = "MOBILE_BACKEND_UNICEL",
            authorized_domains = [],
            is_global = True,
            username = settings.UNICEL_CONFIG.get("username"),
            password = settings.UNICEL_CONFIG.get("password"),
            sender = settings.UNICEL_CONFIG.get("sender"),
            reply_to_phone_number = settings.UNICEL_CONFIG.get("receive_phone", None),
        )
        backend._id = backend.name
        backend.save()

    # If no mach backend was present, create one
    try:
        MobileBackend.get("MOBILE_BACKEND_MACH")
    except ResourceNotFound:
        backend = MachBackend(
            domain = None,
            name = "MOBILE_BACKEND_MACH",
            authorized_domains = [],
            is_global = True,
            account_id = re.match(r".*id=(.+?)&", settings.SMS_GATEWAY_PARAMS).group(1),
            password = re.match(r".*pw=(.+?)&", settings.SMS_GATEWAY_PARAMS).group(1),
            sender_id = "DIMAGI",
        )
        backend._id = backend.name
        backend.save()

    # If no test backend was present, create one
    try:
        MobileBackend.get("MOBILE_BACKEND_TEST")
    except ResourceNotFound:
        backend = TestSMSBackend(
            domain = None,
            name = "MOBILE_BACKEND_TEST",
            authorized_domains = [],
            is_global = True,
        )
        backend._id = backend.name
        backend.save()
Exemplo n.º 6
0
                        continue

                    mach_number = MachPhoneNumber.get_by_number(message.phone_number, mach_row)
                    rate_item = MachSMSRate.get_by_number(message.direction, mach_number) if mach_number else None

                    billable = cls.new_billable(rate_item, message)
                    if billable:
                        billable.contacted_mach_api = datetime.datetime.now(tz=pytz.utc)
                        billable.update_mach_delivery_status(mach_row)
                        billable.save()
                        return
                    logging.error("[Billing] MACH API Response was successful, but creating the MACH "
                                  "billable was not. SMSLog # %s" % message.get_id)
                else:
                    logging.error("[Billing] There was an error retrieving message delivery information from MACH.")
            else:
                logging.error("[Billing] There was an error accessing the MACHI API.")
        else:
            logging.error("[Billing] There was an error while trying to send an SMS via MACH.")

    @staticmethod
    def api_name():
        return "Mach"


API_TO_BILLABLE = {
    UnicelBackend.get_api_id(): UnicelSMSBillable,
    TropoBackend.get_api_id(): TropoSMSBillable,
    MachBackend.get_api_id(): MachSMSBillable,
}