Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def parse_number(self, number):
try:
return self._parse_number(number)
except phonenumbers.phonenumberutil.NumberParseException as exn:
logging.error("invalid number: %s", number)
return {
'premium': False,
'mobile': False,
'international': False,
'phonenumber': number,
'region': 'INVALID',
'country': 'INVALID'
}
def save(self, *args, **kwargs):
if self.phone is not None and self.phone.strip() == "":
self.phone = None
if self.phone is not None:
try:
phone_number = phonenumbers.parse(self.phone, "US")
self.phone = phonenumbers.format_number(
phone_number, phonenumbers.PhoneNumberFormat.E164
)
except phonenumbers.phonenumberutil.NumberParseException:
raise ValidationError("Invalid phone number (this should have been caught already)")
super(UserProfile, self).save(*args, **kwargs)
def __eq__(self, other):
'''
Override parent equality because we store only string representation
of phone number, so we must compare only this string representation
'''
if (isinstance(other, PhoneNumber) or
isinstance(other, phonenumbers.phonenumber.PhoneNumber) or
isinstance(other, str)):
fmt = self.FORMATS['E164']
if isinstance(other, str):
# convert string to phonenumbers.phonenumber.PhoneNumber instance
try:
other = phonenumbers.phonenumberutil.parse(other)
except phonenumbers.phonenumberutil.NumberParseException:
# Conversion is not possible, thus not equal
return False
other_string = phonenumbers.format_number(other, fmt)
return self.format_as(fmt) == other_string
else:
return False
def _sms_sanitization(self, partner, field_name):
number = partner[field_name]
if number and _sms_phonenumbers_lib_imported:
country = self._phone_get_country(partner)
country_code = country.code if country else None
try:
phone_nbr = phonenumbers.parse(number, region=country_code, keep_raw_input=True)
except phonenumbers.phonenumberutil.NumberParseException:
return number
if not phonenumbers.is_possible_number(phone_nbr) or not phonenumbers.is_valid_number(phone_nbr):
return number
phone_fmt = phonenumbers.PhoneNumberFormat.E164
return phonenumbers.format_number(phone_nbr, phone_fmt)
else:
return number