Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def normalize_phone_number(num):
return format_number(parse(num.decode('utf-8'), 'US'), PhoneNumberFormat.INTERNATIONAL)
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')
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)
def normalize_phone_number(num):
return format_number(parse(num.decode('utf-8'), 'US'), PhoneNumberFormat.INTERNATIONAL)
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)
def caption(self, value):
number = parse_number(value)
return format_number(number, PhoneNumberFormat.INTERNATIONAL)
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)
def as_international(self):
return self.format_as(phonenumbers.PhoneNumberFormat.INTERNATIONAL)
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
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)