mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-19 05:59:44 -04:00
Merge pull request #134 from alphagov/share-phone-validation
Moved mobile validation to utils module for use in csv upload as well.
This commit is contained in:
@@ -12,7 +12,12 @@ from wtforms import (
|
||||
from wtforms.validators import DataRequired, Email, Length, Regexp
|
||||
|
||||
from app.main.validators import Blacklist, CsvFileValidator
|
||||
from app.main.encryption import check_hash
|
||||
|
||||
from app.main.utils import (
|
||||
validate_phone_number,
|
||||
format_phone_number,
|
||||
InvalidPhoneError
|
||||
)
|
||||
|
||||
|
||||
def email_address():
|
||||
@@ -28,33 +33,10 @@ def email_address():
|
||||
class UKMobileNumber(StringField):
|
||||
|
||||
def pre_validate(self, form):
|
||||
|
||||
self.data = self.data.replace('(', '')
|
||||
self.data = self.data.replace(')', '')
|
||||
self.data = self.data.replace(' ', '')
|
||||
self.data = self.data.replace('-', '')
|
||||
|
||||
if self.data.startswith('+'):
|
||||
self.data = self.data[1:]
|
||||
|
||||
if not sum(
|
||||
self.data.startswith(prefix) for prefix in ['07', '447', '4407', '00447']
|
||||
):
|
||||
raise ValidationError('Must be a UK mobile number (eg 07700 900460)')
|
||||
|
||||
for digit in self.data:
|
||||
try:
|
||||
int(digit)
|
||||
except(ValueError):
|
||||
raise ValidationError('Must not contain letters or symbols')
|
||||
|
||||
self.data = self.data.split('7', 1)[1]
|
||||
|
||||
if len(self.data) > 9:
|
||||
raise ValidationError('Too many digits')
|
||||
|
||||
if len(self.data) < 9:
|
||||
raise ValidationError('Not enough digits')
|
||||
try:
|
||||
self.data = validate_phone_number(self.data)
|
||||
except InvalidPhoneError as e:
|
||||
raise ValidationError(e.message)
|
||||
|
||||
def post_validate(self, form, validation_stopped):
|
||||
|
||||
@@ -63,7 +45,7 @@ class UKMobileNumber(StringField):
|
||||
# TODO implement in the render field method.
|
||||
# API's require no spaces in the number
|
||||
# self.data = '+44 7{} {} {}'.format(*re.findall('...', self.data))
|
||||
self.data = '+447{}{}{}'.format(*re.findall('...', self.data))
|
||||
self.data = format_phone_number(self.data)
|
||||
|
||||
|
||||
def mobile_number():
|
||||
|
||||
@@ -24,3 +24,50 @@ class BrowsableItem(object):
|
||||
@property
|
||||
def destructive(self):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidPhoneError(Exception):
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
|
||||
|
||||
def validate_phone_number(number):
|
||||
|
||||
sanitised_number = number.replace('(', '')
|
||||
sanitised_number = sanitised_number.replace(')', '')
|
||||
sanitised_number = sanitised_number.replace(' ', '')
|
||||
sanitised_number = sanitised_number.replace('-', '')
|
||||
|
||||
if sanitised_number.startswith('+'):
|
||||
sanitised_number = sanitised_number[1:]
|
||||
|
||||
valid_prefixes = ['07', '447', '4407', '00447']
|
||||
if not sum(sanitised_number.startswith(prefix) for prefix in valid_prefixes):
|
||||
raise InvalidPhoneError('Must be a UK mobile number (eg 07700 900460)')
|
||||
|
||||
for digit in sanitised_number:
|
||||
try:
|
||||
int(digit)
|
||||
except(ValueError):
|
||||
raise InvalidPhoneError('Must not contain letters or symbols')
|
||||
|
||||
# Split number on first 7
|
||||
sanitised_number = sanitised_number.split('7', 1)[1]
|
||||
|
||||
if len(sanitised_number) > 9:
|
||||
raise InvalidPhoneError('Too many digits')
|
||||
|
||||
if len(sanitised_number) < 9:
|
||||
raise InvalidPhoneError('Not enough digits')
|
||||
|
||||
return sanitised_number
|
||||
|
||||
|
||||
def format_phone_number(number):
|
||||
import re
|
||||
if len(number) > 9:
|
||||
raise InvalidPhoneError('Too many digits')
|
||||
|
||||
if len(number) < 9:
|
||||
raise InvalidPhoneError('Not enough digits')
|
||||
return '+447{}{}{}'.format(*re.findall('...', number))
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import csv
|
||||
import re
|
||||
import uuid
|
||||
|
||||
from datetime import date
|
||||
@@ -26,6 +25,10 @@ from app.main.uploader import (
|
||||
)
|
||||
from app.main.dao import templates_dao
|
||||
from app import job_api_client
|
||||
from app.main.utils import (
|
||||
validate_phone_number,
|
||||
InvalidPhoneError
|
||||
)
|
||||
|
||||
|
||||
@main.route("/services/<int:service_id>/sms/send", methods=['GET', 'POST'])
|
||||
@@ -116,15 +119,15 @@ def _format_filename(filename):
|
||||
|
||||
|
||||
def _get_numbers(contents):
|
||||
pattern = re.compile(r'^\+44\s?\d{4}\s?\d{6}$') # TODO need better validation
|
||||
reader = csv.DictReader(
|
||||
contents.split('\n'),
|
||||
lineterminator='\n',
|
||||
quoting=csv.QUOTE_NONE)
|
||||
valid, rejects = [], []
|
||||
for i, row in enumerate(reader):
|
||||
if pattern.match(row['phone']):
|
||||
try:
|
||||
validate_phone_number(row['phone'])
|
||||
valid.append(row)
|
||||
else:
|
||||
except InvalidPhoneError:
|
||||
rejects.append({"line_number": i+2, "phone": row['phone']})
|
||||
return {"valid": valid, "rejects": rejects}
|
||||
|
||||
Reference in New Issue
Block a user