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

Member phone validator #388

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
25 changes: 25 additions & 0 deletions src/meshapi/migrations/0009_alter_member_phone_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.2.13 on 2024-06-20 22:26

from django.db import migrations, models

import meshapi.validation


class Migration(migrations.Migration):
dependencies = [
("meshapi", "0008_alter_building_notes_alter_install_notes_and_more"),
]

operations = [
migrations.AlterField(
model_name="member",
name="phone_number",
field=models.CharField(
blank=True,
default=None,
help_text="A contact phone number for this member",
null=True,
validators=[meshapi.validation.validate_phone_number_field],
),
),
]
8 changes: 7 additions & 1 deletion src/meshapi/models/member.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from django.db.models.fields import EmailField
from django_jsonform.models.fields import ArrayField as JSONFormArrayField

from meshapi.validation import validate_phone_number_field


class Member(models.Model):
name = models.CharField(help_text='Member full name in the format: "First Last"')
Expand All @@ -22,7 +24,11 @@ class Member(models.Model):
help_text="Any additional email addresses associated with this member",
)
phone_number = models.CharField(
default=None, blank=True, null=True, help_text="A contact phone number for this member"
default=None,
blank=True,
null=True,
help_text="A contact phone number for this member",
validators=[validate_phone_number_field],
)
slack_handle = models.CharField(default=None, blank=True, null=True, help_text="The member's slack handle")
notes = models.TextField(
Expand Down
2 changes: 1 addition & 1 deletion src/meshapi/tests/sample_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
sample_member = {
"name": "John Smith",
"primary_email_address": "[email protected]",
"phone_number": "555-555-5555",
"phone_number": "+1-555-555-5555",
"slack_handle": "@jsmith",
}

Expand Down
6 changes: 6 additions & 0 deletions src/meshapi/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import phonenumbers
import requests
from django.core.exceptions import ValidationError
from validate_email import validate_email

from meshapi.exceptions import AddressAPIError, AddressError, OpenDataAPIError
Expand Down Expand Up @@ -34,6 +35,11 @@ def validate_phone_number(phone_number: str) -> bool:
return True


def validate_phone_number_field(phone_number: str):
if not validate_phone_number(phone_number):
raise ValidationError(f"Invalid phone number: {phone_number}")


# Used to obtain info about addresses within NYC. Uses a pair of APIs
# hosted by the city with all kinds of good info. Unfortunately, there's
# not a solid way to check if an address is actually _within_ NYC, so this
Expand Down
Loading