Merge pull request #90 from dod-ccpo/suggest-pe-id
Suggest a PE number when PE number is not found
This commit is contained in:
commit
240da29945
@ -1,3 +1,4 @@
|
||||
import re
|
||||
import tornado
|
||||
from tornado.gen import Return
|
||||
from wtforms.fields.html5 import EmailField
|
||||
@ -9,6 +10,30 @@ from .fields import NewlineListField
|
||||
from .forms import ValidatedForm
|
||||
|
||||
|
||||
PE_REGEX = re.compile(r"""
|
||||
(0?\d) # program identifier
|
||||
(0?\d) # category
|
||||
(\d) # activity
|
||||
(\d+) # sponsor element
|
||||
(.+) # service
|
||||
""", re.X)
|
||||
|
||||
def suggest_pe_id(pe_id):
|
||||
suggestion = pe_id
|
||||
match = PE_REGEX.match(pe_id)
|
||||
if match:
|
||||
(program, category, activity, sponsor, service) = match.groups()
|
||||
if len(program) < 2:
|
||||
program = "0" + program
|
||||
if len(category) < 2:
|
||||
category = "0" + category
|
||||
suggestion = "".join((program, category, activity, sponsor, service))
|
||||
|
||||
if suggestion != pe_id:
|
||||
return suggestion
|
||||
return None
|
||||
|
||||
|
||||
@tornado.gen.coroutine
|
||||
def validate_pe_id(field, existing_request, fundz_client):
|
||||
response = yield fundz_client.get(
|
||||
@ -16,11 +41,13 @@ def validate_pe_id(field, existing_request, fundz_client):
|
||||
raise_error=False,
|
||||
)
|
||||
if not response.ok:
|
||||
field.errors.append(
|
||||
"We couldn't find that PE number, but if you have double checked "
|
||||
"it you can submit anyway. Your request will need to go through a "
|
||||
"manual review."
|
||||
)
|
||||
suggestion = suggest_pe_id(field.data)
|
||||
error_str = (
|
||||
"We couldn't find that PE number. {}"
|
||||
"If you have double checked it you can submit anyway. "
|
||||
"Your request will need to go through a manual review."
|
||||
).format("Did you mean \"{}\"? ".format(suggestion) if suggestion else "")
|
||||
field.errors.append(error_str)
|
||||
return False
|
||||
|
||||
return True
|
||||
|
14
tests/forms/test_financial.py
Normal file
14
tests/forms/test_financial.py
Normal file
@ -0,0 +1,14 @@
|
||||
import pytest
|
||||
|
||||
from atst.forms.financial import suggest_pe_id
|
||||
|
||||
|
||||
@pytest.mark.parametrize("input,expected", [
|
||||
('0603502N', None),
|
||||
('0603502NZ', None),
|
||||
('603502N', '0603502N'),
|
||||
('063502N', '0603502N'),
|
||||
('63502N', '0603502N'),
|
||||
])
|
||||
def test_suggest_pe_id(input, expected):
|
||||
assert suggest_pe_id(input) == expected
|
Loading…
x
Reference in New Issue
Block a user