Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if isinstance(pattern, list):
pat = len(pattern) > index and pattern[index] or ''
else:
lst = pattern.split('|')
pat = len(lst) > index and lst[index] or ''
if not pat:
return nb
nbl = []
for i, part in enumerate(nb.split()):
nbl.append(part)
nbl.append((pat[i:i + 1] + pat[-1:])[0])
return ''.join(nbl[:-1])
if format:
ret = format_with_pattern(phonenumbers.format_number(number, format == 'int' and 1 or 2))
elif country in phonenumbers.data._COUNTRY_CODE_TO_REGION_CODE.get(number.country_code, []):
ret = format_with_pattern(phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.NATIONAL))
else:
ret = format_with_pattern(phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.INTERNATIONAL))
return ret
def to_local_formatted_phone(self, value):
return self._get_formatted_phone(
value, phonenumbers.PhoneNumberFormat.NATIONAL
)
def nazionale(self):
"""
Ritorna il numero formattato come numero nazionale.
:return: Stringa che rappresenta il numero in formato nazionale.
"""
return phonenumbers.format_number(self._phonenumber(), phonenumbers.PhoneNumberFormat.NATIONAL)
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)
#!/usr/bin/env python
# encoding: utf-8
import phonenumbers
from .simple import StringField
class PhoneNumber(phonenumbers.phonenumber.PhoneNumber):
'''
extend phonenumbers.phonenumber.PhoneNumber with easier accessors
'''
FORMATS = {
'E164': phonenumbers.PhoneNumberFormat.E164,
'INTERNATIONAL': phonenumbers.PhoneNumberFormat.INTERNATIONAL,
'NATIONAL': phonenumbers.PhoneNumberFormat.NATIONAL,
'RFC3966': phonenumbers.PhoneNumberFormat.RFC3966,
}
default_format = 'INTERNATIONAL'
_region = ''
def __repr__(self):
fmt = self.FORMATS[self.default_format]
return '<{} {}>'.format(self.__class__.__name__, self.format_as(fmt))
@classmethod
def from_string(cls, phone_number, region=None):
try:
phone_number_obj = cls()
phonenumbers.parse(number=phone_number, region=region,
keep_raw_input=True, numobj=phone_number_obj)
return phone_number_obj
def as_national(self):
return self.format_as(phonenumbers.PhoneNumberFormat.NATIONAL)
def nazionale(self):
"""
Ritorna il numero formattato come numero nazionale.
:return: Stringa che rappresenta il numero in formato nazionale.
"""
return phonenumbers.format_number(self._phonenumber(), phonenumbers.PhoneNumberFormat.NATIONAL)
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
import phonenumbers
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 __unicode__(self):
format_string = getattr(
settings, 'PHONENUMBER_DEFAULT_FORMAT', 'INTERNATIONAL')
fmt = self.format_map[format_string]
int_dial_code = trunk_int_dial_code
try:
if region:
o = parse(phone_number, region)
else:
if int_dial_code is not None:
country_data = CallerLookupCountryCodes.get_country_data(country_int_dial_code=int_dial_code)
for country in country_data:
region = country[CallerLookupLabel.COUNTRY_CODE].upper()
o = parse(phone_number, region)
if o is None:
o = parse(phone_number, UNKNOWN_REGION) if o is None else o
result[CallerLookupLabel.IS_VALID] = is_valid_number(o)
if result[CallerLookupLabel.IS_VALID]:
result[CallerLookupLabel.NUMBER_E164] = fmt_number(o, PhoneNumberFormat.E164)
result[CallerLookupLabel.NUMBER_NATIONAL] = fmt_number(o, PhoneNumberFormat.NATIONAL)
result[CallerLookupLabel.REGION] = region_code_for_number(o)
result[CallerLookupLabel.REGION_DIAL_CODE] = o.country_code
except Exception as ex:
log_debug(config, ["FORMAT_NUMBER_ERROR", str(ex), str(result)])
return result
def as_national(self):
return self.format_as(phonenumbers.PhoneNumberFormat.NATIONAL)