本文整理汇总了Python中corehq.apps.smsbillables.models.SmsBillable类的典型用法代码示例。如果您正苦于以下问题:Python SmsBillable类的具体用法?Python SmsBillable怎么用?Python SmsBillable使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SmsBillable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle
def handle(self, *args, **options):
billables_created = 0
for domain in Domain.get_all():
key = [domain.name, 'SMSLog']
start_date = [datetime.datetime(2014, 1, 1).isoformat()]
end_date = [datetime.datetime(2014, 1, 24).isoformat()]
sms_docs = SMSLog.get_db().view('sms/by_domain',
reduce=False,
startkey=key + start_date,
endkey=key + end_date + [{}])
for sms_doc in sms_docs:
sms_log = SMSLog.get(sms_doc['id'])
try:
if sms_log.phone_number is not None:
parse_phone_number(sms_log.phone_number)
except PhoneNumberParseException:
billables = SmsBillable.objects.filter(log_id=sms_log._id)
if len(billables) == 0:
SmsBillable.create(sms_log)
billables_created += 1
print 'created SmsBillable for invalid number %s in domain %s, id=%s'\
% (sms_log.phone_number, domain.name, sms_log._id)
elif len(billables) > 1:
print "Warning: >1 SmsBillable exists for SMSLog with id=%" % sms_log._id
print 'Number of SmsBillables created: %d' % billables_created
print 'Completed retrobilling.'
示例2: arbitrary_sms_billables_for_domain
def arbitrary_sms_billables_for_domain(domain, message_month_date, num_sms, direction=None, multipart_count=1):
from corehq.apps.smsbillables.models import SmsBillable, SmsGatewayFee, SmsUsageFee
direction = direction or random.choice(DIRECTIONS)
gateway_fee = SmsGatewayFee.create_new('MACH', direction, Decimal(0.5))
usage_fee = SmsUsageFee.create_new(direction, Decimal(0.25))
_, last_day_message = calendar.monthrange(message_month_date.year, message_month_date.month)
billables = []
for _ in range(0, num_sms):
sms_billable = SmsBillable(
gateway_fee=gateway_fee,
usage_fee=usage_fee,
log_id=data_gen.arbitrary_unique_name()[:50],
phone_number=data_gen.random_phonenumber(),
domain=domain,
direction=direction,
date_sent=datetime.date(message_month_date.year, message_month_date.month,
random.randint(1, last_day_message)),
multipart_count=multipart_count,
)
sms_billable.save()
billables.append(sms_billable)
return billables
示例3: handle
def handle(self, *args, **options):
num_sms = 0
start_datetime = datetime.datetime(*str_to_int_tuple(args[0:6]))
end_datetime = datetime.datetime(*str_to_int_tuple(args[6:12]))
for domain in Domain.get_all():
key = [domain.name, 'SMSLog']
sms_docs = SMSLog.get_db().view('sms/by_domain',
reduce=False,
startkey=key + [start_datetime.isoformat()],
endkey=key + [end_datetime.isoformat(), {}],
)
for sms_doc in sms_docs:
sms_log = SMSLog.get(sms_doc['id'])
if options.get('create', False):
SmsBillable.create(sms_log)
print 'Created billable for SMSLog %s in domain %s from %s' \
% (sms_doc['id'], domain.name, sms_log.date)
else:
print 'Found SMSLog %s in domain %s from %s' \
% (sms_doc['id'], domain.name, sms_log.date)
num_sms += 1
print 'Number of SMSs in datetime range: %d' % num_sms
示例4: store_billable
def store_billable(msg):
if msg._id and not SmsBillable.objects.filter(log_id=msg._id).exists():
try:
msg.text.encode('iso-8859-1')
msg_length = 160
except UnicodeEncodeError:
# This string contains unicode characters, so the allowed
# per-sms message length is shortened
msg_length = 70
for _ in range(int(math.ceil(float(len(msg.text)) / msg_length))):
SmsBillable.create(msg)
示例5: test_twilio_multipart_usage_charge
def test_twilio_multipart_usage_charge(self):
self.apply_direction_fee()
twilio_backend = SQLTwilioBackend.objects.create(
name='TWILIO',
is_global=True,
hq_api_id=SQLTwilioBackend.get_api_id(),
couch_id='global_backend',
)
twilio_backend.set_extra_fields(
account_sid='sid',
auth_token='token',
)
twilio_backend.save()
messages = generator.arbitrary_messages_by_backend_and_direction(
{twilio_backend.hq_api_id: twilio_backend.couch_id}
)
for message in messages:
FakeTwilioMessageFactory.add_num_segments_for_message(message.backend_message_id, randint(1, 10))
FakeTwilioMessageFactory.add_price_for_message(message.backend_message_id, generator.arbitrary_fee())
for message in messages:
multipart_count = randint(1, 10) # Should be ignored
billable = SmsBillable.create(message, multipart_count=multipart_count)
self.assertIsNotNone(billable)
self.assertEqual(
billable.usage_charge,
(
self.least_specific_fees[message.direction]
* FakeTwilioMessageFactory.get_num_segments_for_message(
message.backend_message_id
)
)
)
示例6: test_prefix_fees
def test_prefix_fees(self):
self.create_prefix_gateway_fees()
for phone_number, prefix in generator.arbitrary_phone_numbers_and_prefixes(
self.country_code_and_prefixes
):
messages = generator.arbitrary_messages_by_backend_and_direction(
{
random_key: self.backend_ids[random_key]
for random_key in [choice(self.backend_ids.keys())]
},
phone_number=phone_number,
)
for msg_log in messages:
billable = SmsBillable.create(msg_log)
self.assertIsNotNone(billable)
try:
self.assertEqual(
billable.gateway_charge,
self.prefix_fees
[billable.direction]
[billable.gateway_fee.criteria.backend_api_id]
[phone_number[:-10]]
[prefix]
[msg_log.backend_id]
)
except AssertionError:
raise Exception(
"Phone number: %s, " % phone_number
+ "given prefix: %s, " % prefix
+ "found prefix: %s" % billable.gateway_fee.criteria.prefix
)
示例7: test_only_direction
def test_only_direction(self):
self.apply_direction_fee()
messages = generator.arbitrary_messages_by_backend_and_direction(self.backend_ids)
for message in messages:
billable = SmsBillable.create(message)
self.assertIsNotNone(billable)
self.assertEqual(billable.usage_charge, self.least_specific_fees[message.direction])
示例8: store_billable
def store_billable(self, msg):
if not isinstance(msg, SMS):
raise Exception("Expected msg to be an SMS")
if msg.couch_id and not SmsBillable.objects.filter(log_id=msg.couch_id).exists():
try:
msg.text.encode('iso-8859-1')
msg_length = 160
except UnicodeEncodeError:
# This string contains unicode characters, so the allowed
# per-sms message length is shortened
msg_length = 70
try:
SmsBillable.create(
msg,
multipart_count=int(math.ceil(float(len(msg.text)) / msg_length)),
)
except RetryBillableTaskException as e:
self.retry(exc=e)
示例9: test_least_specific_fees
def test_least_specific_fees(self):
self.create_least_specific_gateway_fees()
for msg_log in self.message_logs:
billable = SmsBillable.create(msg_log)
self.assertIsNotNone(billable)
self.assertEqual(
billable.gateway_charge,
self.least_specific_fees[billable.direction][billable.gateway_fee.criteria.backend_api_id]
)
示例10: test_domain_and_direction
def test_domain_and_direction(self):
self.apply_direction_fee()
self.apply_direction_and_domain_fee()
for direction, domain_fee in self.most_specific_fees.items():
for domain in domain_fee:
messages = generator.arbitrary_messages_by_backend_and_direction(self.backend_ids, domain=domain)
for message in messages:
billable = SmsBillable.create(message)
self.assertIsNotNone(billable)
self.assertEqual(billable.usage_charge, self.most_specific_fees[message.direction][domain])
示例11: arbitrary_sms_billables_for_domain
def arbitrary_sms_billables_for_domain(domain, direction, message_month_date, num_sms):
from corehq.apps.smsbillables.models import SmsBillable, SmsGatewayFee, SmsUsageFee
from corehq.apps.smsbillables import generator as sms_gen
gateway_fee = SmsGatewayFee.create_new('MACH', direction, sms_gen.arbitrary_fee())
usage_fee = SmsUsageFee.create_new(direction, sms_gen.arbitrary_fee())
_, last_day_message = calendar.monthrange(message_month_date.year, message_month_date.month)
for _ in range(0, num_sms):
sms_billable = SmsBillable(
gateway_fee=gateway_fee,
usage_fee=usage_fee,
log_id=data_gen.arbitrary_unique_name()[:50],
phone_number=data_gen.random_phonenumber(),
domain=domain,
direction=direction,
date_sent=datetime.date(message_month_date.year, message_month_date.month,
random.randint(1, last_day_message)),
)
sms_billable.save()
示例12: handle
def handle(self, start_datetime, end_datetime, **options):
num_sms = 0
for domain in Domain.get_all():
result = SMS.by_domain(
domain.name,
start_date=start_datetime,
end_date=end_datetime,
)
for sms_log in result:
if options.get('create', False):
SmsBillable.create(sms_log)
print('Created billable for SMS %s in domain %s from %s' \
% (sms_log.couch_id, domain.name, sms_log.date))
else:
print('Found SMS %s in domain %s from %s' \
% (sms_log.couch_id, domain.name, sms_log.date))
num_sms += 1
print('Number of SMSs in datetime range: %d' % num_sms)
示例13: test_log_no_usage_fee
def test_log_no_usage_fee(self):
self.apply_direction_fee()
self.apply_direction_and_domain_fee()
for direction, domain_fee in self.most_specific_fees.items():
for domain in domain_fee:
messages = generator.arbitrary_messages_by_backend_and_direction(
self.backend_ids, domain=domain, directions=["X", "Y"]
)
for message in messages:
billable = SmsBillable.create(message)
self.assertIsNotNone(billable)
self.assertIsNone(billable.usage_fee)
示例14: test_no_matching_fee
def test_no_matching_fee(self):
self.create_least_specific_gateway_fees()
self.create_country_code_gateway_fees()
self.create_instance_gateway_fees()
self.create_most_specific_gateway_fees()
phone_numbers = [generator.arbitrary_phone_number() for i in range(10)]
for phone_number in phone_numbers:
messages = generator.arbitrary_messages_by_backend_and_direction(self.backend_ids,
phone_number=phone_number,
directions=['X', 'Y'])
for msg_log in messages:
billable = SmsBillable.create(msg_log)
self.assertIsNotNone(billable)
self.assertIsNone(billable.gateway_fee)
示例15: test_country_code_fees
def test_country_code_fees(self):
self.create_least_specific_gateway_fees()
self.create_country_code_gateway_fees()
phone_numbers = [generator.arbitrary_phone_number() for i in range(10)]
for phone_number in phone_numbers:
messages = generator.arbitrary_messages_by_backend_and_direction(self.backend_ids,
phone_number=phone_number)
for msg_log in messages:
billable = SmsBillable.create(msg_log)
self.assertIsNotNone(billable)
self.assertEqual(
billable.gateway_charge,
self.country_code_fees[billable.direction]
[billable.gateway_fee.criteria.backend_api_id]
[int(phone_number[:-10])]
)