Validate column heading.

Still need to show that it is the heading that is wrong.
This commit is contained in:
Rebecca Law
2016-03-04 10:09:46 +00:00
parent 0fd2572b08
commit e3c692ede7
3 changed files with 28 additions and 4 deletions

View File

@@ -27,7 +27,8 @@ from app.main.dao import templates_dao
from app.main.dao import services_dao
from app import job_api_client
from app.utils import (
validate_recipient, InvalidPhoneError, InvalidEmailError, user_has_permissions)
validate_recipient, validate_header_row, InvalidPhoneError, InvalidEmailError, user_has_permissions,
InvalidHeaderError)
from utils.process_csv import first_column_heading
@@ -285,6 +286,7 @@ def _get_rows(contents, raw_template):
values=row,
drop_values={first_column_heading[raw_template['template_type']]}
).replaced
except (InvalidEmailError, InvalidPhoneError, NeededByTemplateError, NoPlaceholderForDataError):
except (InvalidEmailError, InvalidPhoneError, NeededByTemplateError,
NoPlaceholderForDataError, InvalidHeaderError):
valid = False
return {"valid": valid, "rows": rows}

View File

@@ -3,7 +3,7 @@ import re
from functools import wraps
from flask import (abort, session)
from utils.process_csv import get_recipient_from_row
from utils.process_csv import get_recipient_from_row, first_column_heading
class BrowsableItem(object):
@@ -42,6 +42,11 @@ class InvalidPhoneError(Exception):
self.message = message
class InvalidHeaderError(Exception):
def __init__(self, message):
self.message = message
def validate_phone_number(number):
sanitised_number = number.replace('(', '')
sanitised_number = sanitised_number.replace(')', '')
@@ -90,12 +95,21 @@ def validate_email_address(email_address):
def validate_recipient(row, template_type):
validate_header_row(row, template_type)
return {
'email': validate_email_address,
'sms': validate_phone_number
}[template_type](get_recipient_from_row(row, template_type))
def validate_header_row(row, template_type):
try:
column_heading = first_column_heading[template_type]
row[column_heading]
except KeyError as e:
raise InvalidHeaderError('Invalid header name, should be {}'.format(column_heading))
def user_has_permissions(*permissions, or_=False):
def wrap(func):
@wraps(func)