本文整理汇总了Python中temba.contacts.models.URN.normalize方法的典型用法代码示例。如果您正苦于以下问题:Python URN.normalize方法的具体用法?Python URN.normalize怎么用?Python URN.normalize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类temba.contacts.models.URN
的用法示例。
在下文中一共展示了URN.normalize方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_object
# 需要导入模块: from temba.contacts.models import URN [as 别名]
# 或者: from temba.contacts.models.URN import normalize [as 别名]
def get_object(self, value):
# try to normalize as URN but don't blow up if it's a UUID
try:
as_urn = URN.normalize(value)
except ValueError:
as_urn = value
return self.get_queryset().filter(Q(uuid=value) | Q(urns__urn=as_urn)).first()
示例2: normalize_urn
# 需要导入模块: from temba.contacts.models import URN [as 别名]
# 或者: from temba.contacts.models.URN import normalize [as 别名]
def normalize_urn(self, value):
if self.request.user.get_org().is_anon:
raise InvalidQueryError("URN lookups not allowed for anonymous organizations")
try:
return URN.identity(URN.normalize(value))
except ValueError:
raise InvalidQueryError("Invalid URN: %s" % value)
示例3: validate_urn
# 需要导入模块: from temba.contacts.models import URN [as 别名]
# 或者: from temba.contacts.models.URN import normalize [as 别名]
def validate_urn(value, strict=True):
try:
normalized = URN.normalize(value)
if strict and not URN.validate(normalized):
raise ValueError()
except ValueError:
raise serializers.ValidationError("Invalid URN: %s. Ensure phone numbers contain country codes." % value)
return normalized
示例4: to_internal_value
# 需要导入模块: from temba.contacts.models import URN [as 别名]
# 或者: from temba.contacts.models.URN import normalize [as 别名]
def to_internal_value(self, data):
try:
normalized = URN.normalize(data)
if not URN.validate(normalized):
raise ValueError()
except ValueError:
raise serializers.ValidationError("Invalid URN: %s. Ensure phone numbers contain country codes." % data)
return normalized
示例5: get_object
# 需要导入模块: from temba.contacts.models import URN [as 别名]
# 或者: from temba.contacts.models.URN import normalize [as 别名]
def get_object(self, value):
# try to normalize as URN but don't blow up if it's a UUID
try:
as_urn = URN.identity(URN.normalize(value))
except ValueError:
as_urn = value
contact_ids_with_urn = list(ContactURN.objects.filter(identity=as_urn).values_list("contact_id", flat=True))
return self.get_queryset().filter(Q(uuid=value) | Q(id__in=contact_ids_with_urn)).first()
示例6: validate_urns
# 需要导入模块: from temba.contacts.models import URN [as 别名]
# 或者: from temba.contacts.models.URN import normalize [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
示例7: validate_urn
# 需要导入模块: from temba.contacts.models import URN [as 别名]
# 或者: from temba.contacts.models.URN import normalize [as 别名]
def validate_urn(self, value):
urns = []
if value:
# if we have tel URNs, we may need a country to normalize by
country = self.org.get_country_code()
for urn in value:
try:
normalized = URN.normalize(urn, country)
except ValueError as e: # pragma: needs cover
raise serializers.ValidationError(str(e))
if not URN.validate(normalized, country): # pragma: needs cover
raise serializers.ValidationError("Invalid URN: '%s'" % urn)
urns.append(normalized)
return urns
示例8: validate
# 需要导入模块: from temba.contacts.models import URN [as 别名]
# 或者: from temba.contacts.models.URN import normalize [as 别名]
def validate(self, data):
if data.get("urns") is not None and data.get("phone") is not None:
raise serializers.ValidationError("Cannot provide both urns and phone parameters together")
if data.get("group_uuids") is not None and data.get("groups") is not None:
raise serializers.ValidationError(
"Parameter groups is deprecated and can't be used together with group_uuids"
)
if self.org.is_anon and self.instance and self.parsed_urns is not None:
raise serializers.ValidationError("Cannot update contact URNs on anonymous organizations")
if self.parsed_urns is not None:
# look up these URNs, keeping track of the contacts that are connected to them
urn_contacts = set()
country = self.org.get_country_code()
for parsed_urn in self.parsed_urns:
normalized_urn = URN.identity(URN.normalize(parsed_urn, country))
urn = ContactURN.objects.filter(org=self.org, identity__exact=normalized_urn).first()
if urn and urn.contact:
urn_contacts.add(urn.contact)
if len(urn_contacts) > 1:
raise serializers.ValidationError(_("URNs are used by multiple contacts"))
contact_by_urns = urn_contacts.pop() if len(urn_contacts) > 0 else None
if self.instance and contact_by_urns and contact_by_urns != self.instance: # pragma: no cover
raise serializers.ValidationError(_("URNs are used by other contacts"))
else:
contact_by_urns = None
contact = self.instance or contact_by_urns
# if contact is blocked, they can't be added to groups
if contact and contact.is_blocked and self.group_objs:
raise serializers.ValidationError("Cannot add blocked contact to groups")
return data
示例9: resolve_twitter_ids
# 需要导入模块: from temba.contacts.models import URN [as 别名]
# 或者: from temba.contacts.models.URN import normalize [as 别名]
def resolve_twitter_ids():
r = get_redis_connection()
# TODO: we can't use our non-overlapping task decorator as it creates a loop in the celery resolver when registering
if r.get("resolve_twitter_ids_task"): # pragma: no cover
return
with r.lock("resolve_twitter_ids_task", 1800):
# look up all 'twitter' URNs, limiting to 30k since that's the most our API would allow anyways
twitter_urns = ContactURN.objects.filter(
scheme=TWITTER_SCHEME, contact__is_stopped=False, contact__is_blocked=False
).exclude(contact=None)
twitter_urns = twitter_urns[:30000].only("id", "org", "contact", "path")
api_key = settings.TWITTER_API_KEY
api_secret = settings.TWITTER_API_SECRET
client = Twython(api_key, api_secret)
updated = 0
print("found %d twitter urns to resolve" % len(twitter_urns))
# contacts we will stop
stop_contacts = []
# we try to look these up 100 at a time
for urn_batch in chunk_list(twitter_urns, 100):
screen_names = [u.path for u in urn_batch]
screen_map = {u.path: u for u in urn_batch}
# try to fetch our users by screen name
try:
resp = client.lookup_user(screen_name=",".join(screen_names))
for twitter_user in resp:
screen_name = twitter_user["screen_name"].lower()
twitter_id = twitter_user["id"]
if screen_name in screen_map and twitter_user["id"]:
twitterid_urn = URN.normalize(URN.from_twitterid(twitter_id, screen_name))
old_urn = screen_map[screen_name]
# create our new contact URN
new_urn = ContactURN.get_or_create(old_urn.org, old_urn.contact, twitterid_urn)
# if our new URN already existed for another contact and it is newer
# than our old contact, reassign it to the old contact
if (
new_urn.contact != old_urn.contact
and new_urn.contact.created_on > old_urn.contact.created_on
):
new_urn.contact = old_urn.contact
new_urn.save(update_fields=["contact"])
# get rid of our old URN
ContactURN.objects.filter(id=old_urn.id).update(contact=None)
del screen_map[screen_name]
updated += 1
except Exception as e:
# if this wasn't an exception caused by not finding any of the users, then break
if str(e).find("No user matches") < 0:
# exit, we'll try again later
print("exiting resolve_twitter_ids due to exception: %s" % e)
break
# add all remaining contacts to the contacts we will stop
for contact in screen_map.values():
stop_contacts.append(contact)
# stop all the contacts we couldn't resolve that have only a twitter URN
stopped = 0
for contact_urn in stop_contacts:
contact = contact_urn.contact
if len(contact.urns.all()) == 1:
contact.stop(contact.created_by)
stopped += 1
if len(twitter_urns) > 0:
print("updated %d twitter urns, %d stopped" % (updated, len(stop_contacts)))
示例10: to_internal_value
# 需要导入模块: from temba.contacts.models import URN [as 别名]
# 或者: from temba.contacts.models.URN import normalize [as 别名]
def to_internal_value(self, data):
if not URN.validate(data):
raise ValidationError("Invalid URN: %s" % data)
return URN.normalize(data)