當前位置: 首頁>>代碼示例>>Python>>正文


Python URN.normalize方法代碼示例

本文整理匯總了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()
開發者ID:Ilhasoft,項目名稱:rapidpro,代碼行數:10,代碼來源:fields.py

示例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)
開發者ID:teehamaral,項目名稱:rapidpro,代碼行數:10,代碼來源:views_base.py

示例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
開發者ID:Ilhasoft,項目名稱:rapidpro,代碼行數:11,代碼來源:fields.py

示例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
開發者ID:eHealthAfrica,項目名稱:rapidpro,代碼行數:11,代碼來源:fields.py

示例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()
開發者ID:teehamaral,項目名稱:rapidpro,代碼行數:12,代碼來源:fields.py

示例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
開發者ID:teehamaral,項目名稱:rapidpro,代碼行數:18,代碼來源:serializers.py

示例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
開發者ID:teehamaral,項目名稱:rapidpro,代碼行數:19,代碼來源:serializers.py

示例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
開發者ID:teehamaral,項目名稱:rapidpro,代碼行數:42,代碼來源:serializers.py

示例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)))
開發者ID:mxabierto,項目名稱:rapidpro,代碼行數:79,代碼來源:tasks.py

示例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)
開發者ID:ewheeler,項目名稱:rapidpro,代碼行數:7,代碼來源:serializers.py


注:本文中的temba.contacts.models.URN.normalize方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。