Skip to content
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
merged 18 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions hub/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from django.contrib.gis.db.models import MultiPolygonField, PointField
from django.contrib.gis.geos import Point
from django.core.cache import cache
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import Avg, IntegerField, Max, Min, Q
from django.db.models.functions import Cast, Coalesce
Expand Down Expand Up @@ -73,6 +74,7 @@
refresh_pages,
refresh_webhooks,
)
from hub.validation import validate_and_format_phone_number
from hub.views.mapped import ExternalDataSourceWebhook
from utils import google_maps, google_sheets
from utils.log import get_simple_debug_logger
Expand Down Expand Up @@ -817,6 +819,17 @@ def get_postcode_data(self) -> Optional[PostcodesIOResult]:

return self.postcode_data

def save(self, *args, **kwargs):
if self.phone:
try:
self.phone = validate_and_format_phone_number(
self.phone, default_countries()
Copy link
Member

@janbaykara janbaykara Dec 12, 2024

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 the ExternalDataSource.countries value that relates to this GenericData 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.

)
except ValidationError as e:
raise ValidationError({"phone": f"Invalid phone number: {e}"})

super().save(*args, **kwargs)


class Area(models.Model):
mapit_id = models.CharField(max_length=30)
Expand Down Expand Up @@ -1543,6 +1556,8 @@ def get_update_data(record):
value: datetime = parse_datetime(value)
if field == "can_display_point_field":
value = bool(value) # cast None value to False
if field == "phone_field":
value = validate_and_format_phone_number(value, self.countries)
update_data[field.removesuffix("_field")] = value

return update_data
Expand Down
17 changes: 17 additions & 0 deletions hub/validation.py
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
13 changes: 12 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ google-auth-oauthlib = "^1.2.0"
django-codemirror2 = "^0.2"
wagtail-color-panel = "^1.5.0"
dateparser = "^1.2.0"
phonenumbers = "8.13.51"

[tool.poetry.dev-dependencies]
django-debug-toolbar = "^4.3"
Expand Down
Loading