本文整理汇总了Python中corehq.apps.sms.models.PhoneNumber.get_by_phone_number_or_none方法的典型用法代码示例。如果您正苦于以下问题:Python PhoneNumber.get_by_phone_number_or_none方法的具体用法?Python PhoneNumber.get_by_phone_number_or_none怎么用?Python PhoneNumber.get_by_phone_number_or_none使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类corehq.apps.sms.models.PhoneNumber
的用法示例。
在下文中一共展示了PhoneNumber.get_by_phone_number_or_none方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: send_message_via_backend
# 需要导入模块: from corehq.apps.sms.models import PhoneNumber [as 别名]
# 或者: from corehq.apps.sms.models.PhoneNumber import get_by_phone_number_or_none [as 别名]
def send_message_via_backend(msg, backend=None, orig_phone_number=None):
"""send sms using a specific backend
msg - outbound message object
backend - MobileBackend object to use for sending; if None, use
msg.outbound_backend
orig_phone_number - the originating phone number to use when sending; this
is sent in if the backend supports load balancing
"""
try:
msg.text = clean_text(msg.text)
except Exception:
logging.exception("Could not clean text for sms dated '%s' in domain '%s'" % (msg.date, msg.domain))
try:
if not domain_has_privilege(msg.domain, privileges.OUTBOUND_SMS):
raise Exception(
("Domain '%s' does not have permission to send SMS."
" Please investigate why this function was called.") % msg.domain
)
phone_obj = PhoneNumber.get_by_phone_number_or_none(msg.phone_number)
if phone_obj and not phone_obj.send_sms:
if msg.ignore_opt_out and phone_obj.can_opt_in:
# If ignore_opt_out is True on the message, then we'll still
# send it. However, if we're not letting the phone number
# opt back in and it's in an opted-out state, we will not
# send anything to it no matter the state of the ignore_opt_out
# flag.
pass
else:
msg.set_system_error(SMS.ERROR_PHONE_NUMBER_OPTED_OUT)
return False
if not backend:
backend = msg.outbound_backend
# note: this will handle "verified" contacts that are still pending
# verification, thus the backend is None. it's best to only call
# send_sms_to_verified_number on truly verified contacts, though
if not msg.backend_id:
msg.backend_id = backend._id
if backend.domain_is_authorized(msg.domain):
backend.send(msg, orig_phone_number=orig_phone_number)
else:
raise BackendAuthorizationException("Domain '%s' is not authorized to use backend '%s'" % (msg.domain, backend._id))
try:
msg.backend_api = backend.__class__.get_api_id()
except Exception:
pass
msg.save()
create_billable_for_sms(msg)
return True
except Exception:
log_sms_exception(msg)
return False