1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
"""
flask_security.phone_util
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Utility class for managing phone numbers
:copyright: (c) 2020 by J. Christopher Wagner (jwag).
:license: MIT, see LICENSE for more details.
Avoid making 'phonenumbers' a required package unless needed.
"""
from .utils import config_value, get_message
class PhoneUtil:
"""
Provide parsing and validation for user inputted phone numbers.
Subclass this to use a different underlying phone number parsing library.
To provide your own implementation, pass in the class as ``phone_util_cls``
at init time. Your class will be instantiated once as part of
Flask-Security initialization.
.. versionadded:: 3.4.0
.. versionchanged:: 4.0.0
__init__ takes app argument, and is instantiated at Flask-Security
initialization time rather than at first request.
"""
def __init__(self, app):
"""Instantiate class.
:param app: The Flask application being initialized.
"""
pass
def validate_phone_number(self, input_data):
"""Return ``None`` if a valid phone number else
the ``PHONE_INVALID`` error message."""
import phonenumbers
try:
z = phonenumbers.parse(
input_data, region=config_value("PHONE_REGION_DEFAULT")
)
if phonenumbers.is_valid_number(z):
return None
except phonenumbers.phonenumberutil.NumberParseException:
pass
return get_message("PHONE_INVALID")[0]
def get_canonical_form(self, input_data):
"""Validate and return a canonical form to be stored in DB
and compared against.
Returns ``None`` if input isn't a valid phone number.
"""
import phonenumbers
try:
z = phonenumbers.parse(
input_data, region=config_value("PHONE_REGION_DEFAULT")
)
if phonenumbers.is_valid_number(z):
return phonenumbers.format_number(
z, phonenumbers.PhoneNumberFormat.E164
)
return None
except phonenumbers.phonenumberutil.NumberParseException:
return None
|