From f1ba2bcba69e5456ed85ce20cbd52bc94ca089fa Mon Sep 17 00:00:00 2001 From: Patrick Smith Date: Tue, 17 Jul 2018 16:29:02 -0400 Subject: [PATCH] Perform extra validation on financial verification step --- atst/forms/financial.py | 27 +++++++++++++++++++++++++++ atst/forms/forms.py | 9 +++++++-- atst/handlers/request_new.py | 7 +++++-- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/atst/forms/financial.py b/atst/forms/financial.py index 9d1bf6c7..3bdb8e53 100644 --- a/atst/forms/financial.py +++ b/atst/forms/financial.py @@ -1,13 +1,40 @@ +import tornado +from tornado.gen import Return from wtforms.fields.html5 import EmailField from wtforms.fields import StringField, SelectField +from wtforms.form import Form from wtforms.validators import Required, Email from .fields import NewlineListField from .forms import ValidatedForm +@tornado.gen.coroutine +def validate_pe_id(field, existing_request, fundz_client): + response = yield fundz_client.get( + "/pe-number/{}".format(field.data), + 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." + ) + return False + + return True + + class FinancialForm(ValidatedForm): + @tornado.gen.coroutine + def perform_extra_validation(self, existing_request, fundz_client): + valid = True + if existing_request['pe_id'] != self.pe_id.data: + valid = yield validate_pe_id(self.pe_id, existing_request, fundz_client) + raise Return(valid) + task_order_id = StringField( "Task Order Number associated with this request.", validators=[Required()] ) diff --git a/atst/forms/forms.py b/atst/forms/forms.py index d49c70ce..c3ea02c4 100644 --- a/atst/forms/forms.py +++ b/atst/forms/forms.py @@ -1,7 +1,12 @@ +import tornado +from tornado.gen import Return from wtforms_tornado import Form class ValidatedForm(Form): - def validate_warnings(self, *args, **kwargs): - return True + @tornado.gen.coroutine + def perform_extra_validation(self, *args, **kwargs): + """A coroutine that performs any applicable extra validation. Must + return True if the form is valid or False otherwise.""" + raise Return(True) diff --git a/atst/handlers/request_new.py b/atst/handlers/request_new.py index 23315bcd..2b31b0f6 100644 --- a/atst/handlers/request_new.py +++ b/atst/handlers/request_new.py @@ -53,7 +53,8 @@ class RequestNew(BaseHandler): if jedi_flow.validate(): response = yield jedi_flow.create_or_update_request() if response.ok: - if jedi_flow.validate_warnings(): + valid = yield jedi_flow.validate_warnings() + if valid: if jedi_flow.next_screen >= len(jedi_flow.screens): where = "/requests" else: @@ -143,11 +144,13 @@ class JEDIRequestFlow(object): def validate(self): return self.form.validate() + @tornado.gen.coroutine def validate_warnings(self): - return self.form.validate_warnings( + valid = yield self.form.perform_extra_validation( self.existing_request.get('body', {}).get(self.form_section), self.fundz_client, ) + return valid @property def current_screen(self):