How to use the phonenumbers.PhoneNumberFormat.INTERNATIONAL function in phonenumbers

To help you get started, we’ve selected a few phonenumbers examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github linkedin / oncall / src / oncall / user_sync / ldap_sync.py View on Github external
def normalize_phone_number(num):
    return format_number(parse(num.decode('utf-8'), 'US'), PhoneNumberFormat.INTERNATIONAL)
github django-oscar / django-oscar / src / oscar / core / phonenumber.py View on Github external
from django.core.exceptions import ValidationError
from django.utils import six
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _


@python_2_unicode_compatible
class PhoneNumber(phonenumbers.phonenumber.PhoneNumber):
    """
    A extended version of phonenumbers.phonenumber.PhoneNumber that provides
    some neat and more pythonic, easy to access methods. This makes using a
    PhoneNumber instance much easier, especially in templates and such.
    """
    format_map = {
        'E164': phonenumbers.PhoneNumberFormat.E164,
        'INTERNATIONAL': phonenumbers.PhoneNumberFormat.INTERNATIONAL,
        'NATIONAL': phonenumbers.PhoneNumberFormat.NATIONAL,
        'RFC3966': phonenumbers.PhoneNumberFormat.RFC3966,
    }

    @classmethod
    def from_string(cls, phone_number, region=None):
        phone_number_obj = cls()
        if region is None:
            region = getattr(settings, 'PHONENUMBER_DEFAULT_REGION', None)
        phonenumbers.parse(number=phone_number, region=region,
                           keep_raw_input=True, numobj=phone_number_obj)
        return phone_number_obj

    def __str__(self):
        format_string = getattr(
            settings, 'PHONENUMBER_DEFAULT_FORMAT', 'INTERNATIONAL')
github stefanfoulis / django-phonenumber-field / phonenumber_field / widgets.py View on Github external
def format_value(self, value):
        if isinstance(value, PhoneNumber):
            number_region = region_code_for_number(value)
            if self.region != number_region:
                formatter = PhoneNumberFormat.INTERNATIONAL
            else:
                formatter = PhoneNumberFormat.NATIONAL
            return value.format_as(formatter)
        return super().format_value(value)
github linkedin / oncall / src / oncall / user_sync / slack.py View on Github external
def normalize_phone_number(num):
    return format_number(parse(num.decode('utf-8'), 'US'), PhoneNumberFormat.INTERNATIONAL)
github pr-holonet / pr-holonet / holonet-web / holonet / utils.py View on Github external
def printable_phone_number(s):
    if not s:
        return ''

    # Note that we're assuming USA here, but this shouldn't matter, because
    # s should already be in E.164 format.
    no = phonenumbers.parse(s, 'US')
    if not phonenumbers.is_valid_number(no):
        return s

    # We're checking for +1 here, but this simply means that non-US numbers
    # will have the international prefix.
    fmt = (phonenumbers.PhoneNumberFormat.NATIONAL if no.country_code == 1
           else phonenumbers.PhoneNumberFormat.INTERNATIONAL)
    return phonenumbers.format_number(no, fmt)
github alephdata / followthemoney / followthemoney / types / phone.py View on Github external
def caption(self, value):
        number = parse_number(value)
        return format_number(number, PhoneNumberFormat.INTERNATIONAL)
github DavidCain / mitoc-trips / ws / templatetags / misc_tags.py View on Github external
def format_phone_number(number):
    """ Format phone numbers with spacing & area code. """
    if not number:
        return ''

    # Only include country code if outside the US
    if number.country_code == 1:
        fmt = phonenumbers.PhoneNumberFormat.NATIONAL
    else:
        fmt = phonenumbers.PhoneNumberFormat.INTERNATIONAL

    return phonenumbers.format_number(number, fmt)
github django-oscar / django-oscar / src / oscar / core / phonenumber.py View on Github external
def as_international(self):
        return self.format_as(phonenumbers.PhoneNumberFormat.INTERNATIONAL)
github stefanfoulis / django-phonenumber-field / phonenumber_field / phonenumber.py View on Github external
import phonenumbers
from django.conf import settings
from django.core import validators


class PhoneNumber(phonenumbers.PhoneNumber):
    """
    A extended version of phonenumbers.PhoneNumber that provides
    some neat and more pythonic, easy to access methods. This makes using a
    PhoneNumber instance much easier, especially in templates and such.
    """

    format_map = {
        "E164": phonenumbers.PhoneNumberFormat.E164,
        "INTERNATIONAL": phonenumbers.PhoneNumberFormat.INTERNATIONAL,
        "NATIONAL": phonenumbers.PhoneNumberFormat.NATIONAL,
        "RFC3966": phonenumbers.PhoneNumberFormat.RFC3966,
    }

    @classmethod
    def from_string(cls, phone_number, region=None):
        phone_number_obj = cls()
        if region is None:
            region = getattr(settings, "PHONENUMBER_DEFAULT_REGION", None)
        phonenumbers.parse(
            number=phone_number,
            region=region,
            keep_raw_input=True,
            numobj=phone_number_obj,
        )
        return phone_number_obj
github Bouke / django-two-factor-auth / two_factor / templatetags / two_factor.py View on Github external
def format_phone_number(number):
    """
    Formats a phone number in international notation.
    :param number: str or phonenumber object
    :return: str
    """
    if not isinstance(number, phonenumbers.PhoneNumber):
        number = phonenumbers.parse(number)
    return phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.INTERNATIONAL)