Skip to content

Commit

Permalink
Merge pull request #348 from anandtripathi5/master
Browse files Browse the repository at this point in the history
#347 support of short-code in phone number type
  • Loading branch information
kvesteri committed Nov 19, 2018
2 parents 3563858 + bdcdb33 commit 9521bee
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
16 changes: 12 additions & 4 deletions sqlalchemy_utils/types/phone_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class PhoneNumberParseException(NumberParseException, exc.DontWrapMixin):

@str_coercible
class PhoneNumber(BasePhoneNumber):
'''
"""
Extends a PhoneNumber class from `Python phonenumbers library`_. Adds
different phone number formats to attributes, so they can be easily used
in templates. Phone number validation method is also implemented.
Expand Down Expand Up @@ -72,16 +72,24 @@ class User(self.Base):
String representation of the phone number.
:param region:
Region of the phone number.
'''
def __init__(self, raw_number, region=None):
:param check_region:
Whether to check the supplied region parameter;
should always be True for external callers.
Can be useful for short codes or toll free
"""
def __init__(self, raw_number, region=None, check_region=True):
# Bail if phonenumbers is not found.
if phonenumbers is None:
raise ImproperlyConfigured(
"'phonenumbers' is required to use 'PhoneNumber'"
)

try:
self._phone_number = phonenumbers.parse(raw_number, region)
self._phone_number = phonenumbers.parse(
raw_number,
region,
_check_region=check_region
)
except NumberParseException as e:
# Wrap exception so SQLAlchemy doesn't swallow it as a
# StatementError
Expand Down
12 changes: 12 additions & 0 deletions tests/types/test_phonenumber.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ def test_phone_number_attributes(self):
assert number.international == u'+358 40 1234567'
assert number.national == u'040 1234567'

def test_phone_number_attributes_for_short_code(self):
"""
For international and national shortcode remains the same, if we pass
short code to PhoneNumber library without giving check_region it will
raise exception
:return:
"""
number = PhoneNumber('72404', check_region=False)
assert number.e164 == u'+072404'
assert number.international == u'72404'
assert number.national == u'72404'

def test_phone_number_str_repr(self):
number = PhoneNumber('+358401234567')
if six.PY2:
Expand Down

0 comments on commit 9521bee

Please sign in to comment.