本文整理汇总了Python中phonenumbers.NumberParseException方法的典型用法代码示例。如果您正苦于以下问题:Python phonenumbers.NumberParseException方法的具体用法?Python phonenumbers.NumberParseException怎么用?Python phonenumbers.NumberParseException使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类phonenumbers
的用法示例。
在下文中一共展示了phonenumbers.NumberParseException方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: clean
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import NumberParseException [as 别名]
def clean(self):
cd = super(CustomerForm, self).clean()
phone = cd.get('phone')
country = cd.get('country')
if len(phone) < 1:
return cd
try:
phonenumbers.parse(phone, country)
except phonenumbers.NumberParseException:
msg = _('Enter a valid phone number')
self._errors["phone"] = self.error_class([msg])
return cd
示例2: validate_phone
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import NumberParseException [as 别名]
def validate_phone(cls, number):
"""
Validates the given phone number which should be in E164 format.
"""
try:
parsed = phonenumbers.parse(number)
except phonenumbers.NumberParseException as e:
raise InvalidURN(str(e))
if number != phonenumbers.format_number(parsed, phonenumbers.PhoneNumberFormat.E164):
raise InvalidURN("Phone numbers must be in E164 format")
if not phonenumbers.is_possible_number(parsed) or not phonenumbers.is_valid_number(parsed):
raise InvalidURN("Phone numbers must be in E164 format")
return True
示例3: __eq__
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import NumberParseException [as 别名]
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)
or isinstance(other, str)
):
format_string = "E164"
default_region = None
fmt = self.format_map[format_string]
if isinstance(other, str):
# convert string to phonenumbers.PhoneNumber
# instance
try:
other = phonenumbers.parse(other, region=default_region)
except phonenumbers.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
示例4: to_python
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import NumberParseException [as 别名]
def to_python(value):
if value in (None, ""): # None or ''
phone_number = value
elif value and isinstance(value, str):
try:
phone_number = PhoneNumber.from_string(phone_number=value)
except phonenumbers.NumberParseException:
# the string provided is not a valid PhoneNumber.
phone_number = PhoneNumber(raw_input=value)
elif isinstance(value, phonenumbers.PhoneNumber) and not isinstance(
value, PhoneNumber
):
phone_number = PhoneNumber()
phone_number.merge_from(value)
elif isinstance(value, PhoneNumber):
phone_number = value
else:
# TODO: this should somehow show that it has invalid data, but not
# completely die for bad data in the database.
# (Same for the NumberParseException above)
phone_number = None
return phone_number
示例5: __call__
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import NumberParseException [as 别名]
def __call__(self, node, value):
try:
phone = phonenumbers.parse(value)
except phonenumbers.NumberParseException:
raise colander.Invalid(
node,
_(
u"Phone must be in format +XXXXXXXXXXX "
u"and contains country code",
mapping={'val': value}
)
)
if not phonenumbers.is_valid_number(phone):
raise colander.Invalid(
node,
_(
u"Phone is not valid",
mapping={'val': value}
)
)
示例6: clean_international_phonenumber
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import NumberParseException [as 别名]
def clean_international_phonenumber(value):
"""
Validate phone number taken from TelPrefixInput and return in format suitable for our DB.
"""
invalid_number_message = _(u'The phone number entered is not valid.')
try:
num = phonenumbers.parse(value)
if not phonenumbers.is_valid_number(num):
raise forms.ValidationError(invalid_number_message)
except phonenumbers.NumberParseException:
raise forms.ValidationError(invalid_number_message)
return phonenumbers.format_number(num, phonenumbers.PhoneNumberFormat.E164)
# noinspection PyAbstractClass
示例7: normalized_phone_numbers
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import NumberParseException [as 别名]
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
示例8: clean
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import NumberParseException [as 别名]
def clean(self):
data = self.cleaned_data
phone_number = (data['phone_country_code'] or '') + \
(data['phone_number'] or '')
if data['phone_country_code'] and data['phone_number']:
phone_number = data['phone_country_code'] + data['phone_number']
try:
phone_number = phonenumbers.parse(phone_number, None)
if not phonenumbers.is_valid_number(phone_number):
self.add_error('phone_number', 'Invalid phone number')
except phonenumbers.NumberParseException as e:
self.add_error('phone_number', e)
if data['pushbullet_access_token']:
pushbullet_access_token = data['pushbullet_access_token']
try:
Pushbullet(pushbullet_access_token)
except PushbulletError:
self.add_error('pushbullet_access_token',
'Invalid pushbullet access token.')
data['telegram_chat_id'] = data['telegram_chat_id'] if data['telegram_chat_id'] else None
示例9: phone_validator
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import NumberParseException [as 别名]
def phone_validator(val):
try:
phonenumbers.parse(val, settings.INSTALL_COUNTRY)
except phonenumbers.NumberParseException:
raise ValidationError(_('%s is not a valid phone number') % val)
示例10: phone_format_filter
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import NumberParseException [as 别名]
def phone_format_filter(s):
try:
number = phonenumbers.parse(s, "US")
return phonenumbers.format_number(
number, phonenumbers.PhoneNumberFormat.INTERNATIONAL
)
except phonenumbers.NumberParseException:
return s
示例11: validate_phone_number
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import NumberParseException [as 别名]
def validate_phone_number(form, field):
try:
number = phonenumbers.parse(field.data, "US")
except phonenumbers.NumberParseException:
raise wtforms.ValidationError(
f"{field.data} does not appear to be a valid number."
)
if not phonenumbers.is_possible_number(number):
raise wtforms.ValidationError(
f"{field.data} does not appear to be a possible number."
)
field.data = phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.E164)
示例12: validate_phone
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import NumberParseException [as 别名]
def validate_phone(self, phone):
if phone:
if VerificationDevice.objects.filter(
unverified_phone=phone).exists():
raise serializers.ValidationError(
_("User with this Phone number already exists."))
try:
parse_phone(phone)
except NumberParseException:
raise serializers.ValidationError(
_("Please enter a valid country code."))
else:
phone = None
return phone
示例13: validate_password
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import NumberParseException [as 别名]
def validate_password(self, password):
validate_password(password)
errors = []
email = self.initial_data.get('email')
if email:
email = email.split('@')
if email[0].casefold() in password.casefold():
errors.append(_("Passwords cannot contain your email."))
username = self.initial_data.get('username')
if len(username) and username.casefold() in password.casefold():
errors.append(
_("The password is too similar to the username."))
phone = self.initial_data.get('phone')
if phone:
if phone_validator(phone):
try:
phone = str(parse_phone(phone).national_number)
if phone in password:
errors.append(
_("Passwords cannot contain your phone."))
except NumberParseException:
pass
if errors:
raise serializers.ValidationError(errors)
return password
示例14: clean_password
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import NumberParseException [as 别名]
def clean_password(self):
password = self.data.get('password')
validate_password(password)
errors = []
email = self.data.get('email')
if email:
email = email.split('@')
if email[0].casefold() in password.casefold():
errors.append(_("Passwords cannot contain your email."))
username = self.data.get('username')
if len(username) and username.casefold() in password.casefold():
errors.append(
_("The password is too similar to the username."))
phone = self.data.get('phone')
if phone and phone_validator(phone):
try:
phone = str(parse_phone(phone).national_number)
if phone in password:
errors.append(_("Passwords cannot contain your phone."))
except NumberParseException:
pass
if errors:
raise forms.ValidationError(errors)
return password
示例15: clean_phone
# 需要导入模块: import phonenumbers [as 别名]
# 或者: from phonenumbers import NumberParseException [as 别名]
def clean_phone(self):
phone = self.data.get('phone')
if phone:
if VerificationDevice.objects.filter(
unverified_phone=phone).exists():
raise forms.ValidationError(
_("User with this Phone number already exists."))
try:
parse_phone(phone)
except NumberParseException:
raise forms.ValidationError(
_("Please enter a valid country code."))
else:
phone = None
return phone