Moved mobile validation to utils module for use in csv upload as well.

This could be moved to shared utils code base at some point.
This commit is contained in:
Adam Shimali
2016-02-01 16:57:40 +00:00
parent b5b13254c7
commit 9e8c0b8d59
4 changed files with 133 additions and 33 deletions

View File

@@ -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():