本文整理汇总了Python中temba.contacts.models.URN.to_parts方法的典型用法代码示例。如果您正苦于以下问题:Python URN.to_parts方法的具体用法?Python URN.to_parts怎么用?Python URN.to_parts使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类temba.contacts.models.URN
的用法示例。
在下文中一共展示了URN.to_parts方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: serialize_contact
# 需要导入模块: from temba.contacts.models import URN [as 别名]
# 或者: from temba.contacts.models.URN import to_parts [as 别名]
def serialize_contact(contact):
from temba.contacts.models import URN
field_values = {}
for field in contact.org.cached_contact_fields.values():
field_values[field.key] = contact.get_field_json(field)
# augment URN values with preferred channel UUID as a parameter
urn_values = []
for u in contact.urns.order_by("-priority", "id"):
# for each URN we include the preferred channel as a query param if there is one
if u.channel and u.channel.is_active:
scheme, path, query, display = URN.to_parts(u.urn)
urn_str = URN.from_parts(scheme, path, query=urlencode({"channel": str(u.channel.uuid)}), display=display)
else:
urn_str = u.urn
urn_values.append(urn_str)
return {
"uuid": contact.uuid,
"id": contact.id,
"name": contact.name,
"language": contact.language,
"urns": urn_values,
"groups": [serialize_ref(group) for group in contact.user_groups.filter(is_active=True)],
"fields": field_values,
}
示例2: validate
# 需要导入模块: from temba.contacts.models import URN [as 别名]
# 或者: from temba.contacts.models.URN import to_parts [as 别名]
def validate(self, data):
urns = data.get("urn", [])
phones = data.get("phone", [])
contacts = data.get("contact", [])
channel = data.get("channel")
if (not urns and not phones and not contacts) or (urns and phones): # pragma: needs cover
raise serializers.ValidationError("Must provide either urns or phone or contact and not both")
if not channel:
channel = Channel.objects.filter(is_active=True, org=self.org).order_by("-last_seen").first()
if not channel: # pragma: no cover
raise serializers.ValidationError("There are no channels for this organization.")
data["channel"] = channel
if phones:
if self.org.is_anon: # pragma: needs cover
raise serializers.ValidationError("Cannot create messages for anonymous organizations")
# check our numbers for validity
country = channel.country
for urn in phones:
try:
tel, phone, query, display = URN.to_parts(urn)
normalized = phonenumbers.parse(phone, country.code)
if not phonenumbers.is_possible_number(normalized): # pragma: needs cover
raise serializers.ValidationError("Invalid phone number: '%s'" % phone)
except Exception:
raise serializers.ValidationError("Invalid phone number: '%s'" % phone)
return data
示例3: validate_urns
# 需要导入模块: from temba.contacts.models import URN [as 别名]
# 或者: from temba.contacts.models.URN import to_parts [as 别名]
def validate_urns(self, value):
if value is not None:
self.parsed_urns = []
for urn in value:
try:
normalized = URN.normalize(urn)
scheme, path, query, display = URN.to_parts(normalized)
# for backwards compatibility we don't validate phone numbers here
if scheme != TEL_SCHEME and not URN.validate(normalized): # pragma: needs cover
raise ValueError()
except ValueError:
raise serializers.ValidationError("Invalid URN: '%s'" % urn)
self.parsed_urns.append(normalized)
return value
示例4: reduce_event
# 需要导入模块: from temba.contacts.models import URN [as 别名]
# 或者: from temba.contacts.models.URN import to_parts [as 别名]
def reduce_event(event):
new_event = copy_keys(event, {"type", "msg"})
if "msg" in new_event:
new_event["msg"] = copy_keys(event["msg"], {"text", "urn", "channel", "attachments"})
new_msg = new_event["msg"]
# legacy events are re-constructed from real messages which have their text stripped
if "text" in new_msg:
new_msg["text"] = new_msg["text"].strip()
# legacy events have absolute paths for attachments, new have relative
if "attachments" in new_msg:
abs_prefix = f"https://{settings.AWS_BUCKET_DOMAIN}/"
new_msg["attachments"] = [a.replace(abs_prefix, "") for a in new_msg["attachments"]]
# new engine events might have params on URNs that we're not interested in
if "urn" in new_msg:
scheme, path, query, fragment = URN.to_parts(new_msg["urn"])
new_msg["urn"] = URN.from_parts(scheme, path, None, fragment)
return new_event