forked from mysociety/local-intelligence-hub
-
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate phone number when importing external data [MAP-631] [MAP-77] #159
Merged
janbaykara
merged 18 commits into
main
from
feat/disable-call-sms-buttons-if-phone-number-invalid
Dec 12, 2024
Merged
Changes from 9 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9f3b1fe
install phonenumbers Python Library
Moggach bd0de63
get phone number from imported data and check if its a number and for…
Moggach cf0f720
use zero index of countries array to avoid hard coding GB as the defa…
Moggach edd23b1
fix python linting errors
Moggach 6c724de
extract phone number validation logic into separate function
Moggach d0fb0bf
use validate_and_format_phone_number function in get_update_data meth…
Moggach a932cab
merge latest from main
Moggach 8fb1963
pass the return value of default_countries() rather than the function…
Moggach 31f7ef9
fix linting error
Moggach 0ee5df7
switch default_countries() out for ExternalDataSource.countries field
Moggach 0c5a167
use reference to field instead of calling as method
Moggach 7dac271
Fix countries ref
janbaykara 401dabc
Add tests
janbaykara e414cd4
lint
janbaykara 639fe31
lint
janbaykara 3522ddd
Add DB tests for phone field
janbaykara 4052684
Add DB tests for phone field
janbaykara 89f5764
Roll out database-level tests to datetime parser too
janbaykara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import phonenumbers | ||
from phonenumbers.phonenumberutil import NumberParseException | ||
|
||
|
||
def validate_and_format_phone_number(value, countries): | ||
""" | ||
Validates and formats a phone number to E164 format if valid, otherwise returns None. | ||
""" | ||
try: | ||
phone_number = phonenumbers.parse(value, countries[0]) | ||
if phonenumbers.is_valid_number(phone_number): | ||
return phonenumbers.format_number( | ||
phone_number, phonenumbers.PhoneNumberFormat.E164 | ||
) | ||
except NumberParseException: | ||
pass | ||
return None |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is using the global
default_countries
method, but we really want to use theExternalDataSource.countries
value that relates to thisGenericData
record instead — so that if a given external data source is switched to "US", then the right country gets used by its phone number parser.