forked from mysociety/local-intelligence-hub
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from commonknowledge/feat/disable-call-sms-bu…
…ttons-if-phone-number-invalid Validate phone number when importing external data
- Loading branch information
Showing
6 changed files
with
265 additions
and
13 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Generated by Django 4.2.11 on 2024-12-12 16:48 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import django_jsonform.models.fields | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("hub", "0144_hubimage_description"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="LocalJSONSource", | ||
fields=[ | ||
( | ||
"externaldatasource_ptr", | ||
models.OneToOneField( | ||
auto_created=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
parent_link=True, | ||
primary_key=True, | ||
serialize=False, | ||
to="hub.externaldatasource", | ||
), | ||
), | ||
( | ||
"data", | ||
django_jsonform.models.fields.JSONField(blank=True, default=list), | ||
), | ||
("id_field", models.CharField(default="id", max_length=250)), | ||
], | ||
options={ | ||
"verbose_name": "Test source", | ||
}, | ||
bases=("hub.externaldatasource",), | ||
), | ||
] |
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
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,22 @@ | ||
import phonenumbers | ||
from phonenumbers.phonenumberutil import NumberParseException | ||
|
||
from utils.py import ensure_list | ||
|
||
|
||
def validate_and_format_phone_number(value, countries=[]): | ||
""" | ||
Validates and formats a phone number to E164 format if valid, otherwise returns None. | ||
""" | ||
countries = ensure_list(countries or []) | ||
if len(countries) == 0: | ||
countries = ["GB"] | ||
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