Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def validate_mobile(self, value):
"""Validate if number is mobile
Arguments:
value {string|phonenumbers.PhoneNumber} -- phone number
Returns:
bool -- return True if number is mobile
"""
if not settings.UNIVERSAL_NOTIFICATIONS_VALIDATE_MOBILE:
return True
if not isinstance(value, phonenumbers.PhoneNumber):
try:
value = phonenumbers.parse(value, 'US')
except phonenumbers.phonenumberutil.NumberParseException:
return False
return value
def parse_numero(numero, paese="IT"):
try:
n = phonenumbers.parse(numero, paese)
except phonenumbers.phonenumberutil.NumberParseException:
return numero
f = phonenumbers.format_number(n, phonenumbers.PhoneNumberFormat.E164)
return f
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
except phonenumbers.phonenumberutil.NumberParseException:
return None
def normalized_phone_numbers(cls, a1, a2):
num1 = a1.get(EntityDetails.PHONE, u'').strip()
num2 = a2.get(EntityDetails.PHONE, u'').strip()
country_code1 = a1.get(AddressComponents.COUNTRY)
country_code2 = a2.get(AddressComponents.COUNTRY)
if country_code1 and country_code2 and country_code1 != country_code2:
return None, None
country_code = country_code1 or country_code2
try:
p1 = phonenumbers.parse(num1, region=country_code)
p2 = phonenumbers.parse(num2, region=country_code)
except phonenumbers.NumberParseException:
return None, None
return p1, p2
def format_phone(value):
if value:
number = phonenumbers.parse(value, 'US')
return phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
else:
return ''
def _phonenumber(self):
return phonenumbers.parse(self.numero)
def phonenumber(value, country='US'):
"""Formats a phone number for a country
"""
import phonenumbers
try:
formatted = phonenumbers.format_number(phonenumbers.parse(value, country), phonenumbers.PhoneNumberFormat.NATIONAL)
except:
formatted = value
return formatted
def display_phone(self, field_name=None, phone=None, country='BE', check=True, format='', pattern=''):
"""
Return a formatted localized phone number.
country = 2 letters country code
check = check the validity of the given phone
format = 'nat' or 'int'
pattern = space replacing separators in rtl order
* list of 2 lists for nat and int formats => [['/', '.'], ['-', '.']]
* string or 2 strings (pipe separator) for nat and int formats '/.|-.'
"""
if field_name:
phone = self.get_value(field_name)
if not phone:
return u''
try:
number = phonenumbers.parse(phone, country)
except phonenumbers.NumberParseException:
return translate(u"Bad phone number: '${nb}'", domain='collective.documentgenerator',
mapping={'nb': phone}, context=self.request)
return safe_unicode(phone)
if check and not phonenumbers.is_valid_number(number):
return translate(u"Invalid phone number: '${nb}'", domain='collective.documentgenerator',
mapping={'nb': phone}, context=self.request)
def format_with_pattern(nb):
if not pattern:
return nb
index = 0
if nb.startswith('+'):
index = 1
if isinstance(pattern, list):
pat = len(pattern) > index and pattern[index] or ''
args = get_args(request, ('phone_number', 'country', 'client_secret', 'send_attempt'))
raw_phone_number = args['phone_number']
country = args['country']
sendAttempt = args['send_attempt']
clientSecret = args['client_secret']
if not is_valid_client_secret(clientSecret):
request.setResponseCode(400)
return {
'errcode': 'M_INVALID_PARAM',
'error': 'Invalid client_secret provided'
}
try:
phone_number_object = phonenumbers.parse(raw_phone_number, country)
except Exception as e:
logger.warn("Invalid phone number given: %r", e)
request.setResponseCode(400)
return {'errcode': 'M_INVALID_PHONE_NUMBER', 'error': "Invalid phone number" }
msisdn = phonenumbers.format_number(
phone_number_object, phonenumbers.PhoneNumberFormat.E164
)[1:]
# International formatted number. The same as an E164 but with spaces
# in appropriate places to make it nicer for the humans.
intl_fmt = phonenumbers.format_number(
phone_number_object, phonenumbers.PhoneNumberFormat.INTERNATIONAL
)
try: