Use formdata and InputRequired

This commit is contained in:
richard-dds 2018-10-15 16:22:46 -04:00
parent ed7d508e30
commit 4b8585234f
2 changed files with 33 additions and 25 deletions

View File

@ -2,7 +2,7 @@ import re
import pendulum import pendulum
from wtforms.fields.html5 import DateField, EmailField from wtforms.fields.html5 import DateField, EmailField
from wtforms.fields import StringField, FileField from wtforms.fields import StringField, FileField
from wtforms.validators import DataRequired, Email, Regexp from wtforms.validators import InputRequired, Email, Regexp
from flask_wtf.file import FileAllowed from flask_wtf.file import FileAllowed
from .fields import NewlineListField, SelectField, NumberStringField from .fields import NewlineListField, SelectField, NumberStringField
@ -36,7 +36,7 @@ class BaseFinancialForm(ValidatedForm):
task_order_number = StringField( task_order_number = StringField(
"Task Order Number associated with this request", "Task Order Number associated with this request",
description="Include the original Task Order number (including the 000X at the end). Do not include any modification numbers. Note that there may be a lag between approving a task order and when it becomes available in our system.", description="Include the original Task Order number (including the 000X at the end). Do not include any modification numbers. Note that there may be a lag between approving a task order and when it becomes available in our system.",
validators=[DataRequired()], validators=[InputRequired()],
) )
uii_ids = NewlineListField( uii_ids = NewlineListField(
@ -47,35 +47,35 @@ class BaseFinancialForm(ValidatedForm):
pe_id = StringField( pe_id = StringField(
"Program Element Number", "Program Element Number",
description="PE numbers help the Department of Defense identify which offices' budgets are contributing towards this resource use. <br/><em>It should be 7 digits followed by 1-3 letters, and should have a zero as the first and third digits.</em>", description="PE numbers help the Department of Defense identify which offices' budgets are contributing towards this resource use. <br/><em>It should be 7 digits followed by 1-3 letters, and should have a zero as the first and third digits.</em>",
validators=[DataRequired()], validators=[InputRequired()],
) )
treasury_code = StringField( treasury_code = StringField(
"Program Treasury Code", "Program Treasury Code",
description="Program Treasury Code (or Appropriations Code) identifies resource types. <br/> <em>It should be a four digit or six digit number, optionally prefixed by one or more zeros.</em>", description="Program Treasury Code (or Appropriations Code) identifies resource types. <br/> <em>It should be a four digit or six digit number, optionally prefixed by one or more zeros.</em>",
validators=[DataRequired(), Regexp(TREASURY_CODE_REGEX)], validators=[InputRequired(), Regexp(TREASURY_CODE_REGEX)],
) )
ba_code = StringField( ba_code = StringField(
"Program Budget Activity (BA) Code", "Program Budget Activity (BA) Code",
description="BA Code is used to identify the purposes, projects, or types of activities financed by the appropriation fund. <br/><em>It should be two digits, followed by an optional letter.</em>", description="BA Code is used to identify the purposes, projects, or types of activities financed by the appropriation fund. <br/><em>It should be two digits, followed by an optional letter.</em>",
validators=[DataRequired(), Regexp(BA_CODE_REGEX)], validators=[InputRequired(), Regexp(BA_CODE_REGEX)],
) )
fname_co = StringField("KO First Name", validators=[DataRequired()]) fname_co = StringField("KO First Name", validators=[InputRequired()])
lname_co = StringField("KO Last Name", validators=[DataRequired()]) lname_co = StringField("KO Last Name", validators=[InputRequired()])
email_co = EmailField("KO Email", validators=[DataRequired(), Email()]) email_co = EmailField("KO Email", validators=[InputRequired(), Email()])
office_co = StringField("KO Office", validators=[DataRequired()]) office_co = StringField("KO Office", validators=[InputRequired()])
fname_cor = StringField("COR First Name", validators=[DataRequired()]) fname_cor = StringField("COR First Name", validators=[InputRequired()])
lname_cor = StringField("COR Last Name", validators=[DataRequired()]) lname_cor = StringField("COR Last Name", validators=[InputRequired()])
email_cor = EmailField("COR Email", validators=[DataRequired(), Email()]) email_cor = EmailField("COR Email", validators=[InputRequired(), Email()])
office_cor = StringField("COR Office", validators=[DataRequired()]) office_cor = StringField("COR Office", validators=[InputRequired()])
class FinancialForm(BaseFinancialForm): class FinancialForm(BaseFinancialForm):
@ -91,13 +91,13 @@ class FinancialForm(BaseFinancialForm):
class ExtendedFinancialForm(BaseFinancialForm): class ExtendedFinancialForm(BaseFinancialForm):
def validate(self, *args, **kwargs): def validate(self, *args, **kwargs):
if self.funding_type.data == "OTHER": if self.funding_type.data == "OTHER":
self.funding_type_other.validators.append(DataRequired()) self.funding_type_other.validators.append(InputRequired())
return super().validate(*args, **kwargs) return super().validate(*args, **kwargs)
funding_type = SelectField( funding_type = SelectField(
description="What is the source of funding?", description="What is the source of funding?",
choices=FUNDING_TYPES, choices=FUNDING_TYPES,
validators=[DataRequired()], validators=[InputRequired()],
render_kw={"required": False}, render_kw={"required": False},
) )
@ -107,7 +107,7 @@ class ExtendedFinancialForm(BaseFinancialForm):
"Task Order Expiration Date", "Task Order Expiration Date",
description="Please enter the expiration date for the Task Order", description="Please enter the expiration date for the Task Order",
validators=[ validators=[
DataRequired(), InputRequired(),
DateRange( DateRange(
lower_bound=pendulum.duration(days=0), lower_bound=pendulum.duration(days=0),
upper_bound=pendulum.duration(years=100), upper_bound=pendulum.duration(years=100),
@ -119,42 +119,42 @@ class ExtendedFinancialForm(BaseFinancialForm):
clin_0001 = NumberStringField( clin_0001 = NumberStringField(
"<dl><dt>CLIN 0001</dt> - <dd>Unclassified IaaS and PaaS Amount</dd></dl>", "<dl><dt>CLIN 0001</dt> - <dd>Unclassified IaaS and PaaS Amount</dd></dl>",
validators=[DataRequired()], validators=[InputRequired()],
description="Review your task order document, the amounts for each CLIN must match exactly here", description="Review your task order document, the amounts for each CLIN must match exactly here",
filters=[number_to_int], filters=[number_to_int],
) )
clin_0003 = NumberStringField( clin_0003 = NumberStringField(
"<dl><dt>CLIN 0003</dt> - <dd>Unclassified Cloud Support Package</dd></dl>", "<dl><dt>CLIN 0003</dt> - <dd>Unclassified Cloud Support Package</dd></dl>",
validators=[DataRequired()], validators=[InputRequired()],
description="Review your task order document, the amounts for each CLIN must match exactly here", description="Review your task order document, the amounts for each CLIN must match exactly here",
filters=[number_to_int], filters=[number_to_int],
) )
clin_1001 = NumberStringField( clin_1001 = NumberStringField(
"<dl><dt>CLIN 1001</dt> - <dd>Unclassified IaaS and PaaS Amount <br> OPTION PERIOD 1</dd></dl>", "<dl><dt>CLIN 1001</dt> - <dd>Unclassified IaaS and PaaS Amount <br> OPTION PERIOD 1</dd></dl>",
validators=[DataRequired()], validators=[InputRequired()],
description="Review your task order document, the amounts for each CLIN must match exactly here", description="Review your task order document, the amounts for each CLIN must match exactly here",
filters=[number_to_int], filters=[number_to_int],
) )
clin_1003 = NumberStringField( clin_1003 = NumberStringField(
"<dl><dt>CLIN 1003</dt> - <dd>Unclassified Cloud Support Package <br> OPTION PERIOD 1</dd></dl>", "<dl><dt>CLIN 1003</dt> - <dd>Unclassified Cloud Support Package <br> OPTION PERIOD 1</dd></dl>",
validators=[DataRequired()], validators=[InputRequired()],
description="Review your task order document, the amounts for each CLIN must match exactly here", description="Review your task order document, the amounts for each CLIN must match exactly here",
filters=[number_to_int], filters=[number_to_int],
) )
clin_2001 = NumberStringField( clin_2001 = NumberStringField(
"<dl><dt>CLIN 2001</dt> - <dd>Unclassified IaaS and PaaS Amount <br> OPTION PERIOD 2</dd></dl>", "<dl><dt>CLIN 2001</dt> - <dd>Unclassified IaaS and PaaS Amount <br> OPTION PERIOD 2</dd></dl>",
validators=[DataRequired()], validators=[InputRequired()],
description="Review your task order document, the amounts for each CLIN must match exactly here", description="Review your task order document, the amounts for each CLIN must match exactly here",
filters=[number_to_int], filters=[number_to_int],
) )
clin_2003 = NumberStringField( clin_2003 = NumberStringField(
"<dl><dt>CLIN 2003</dt> - <dd>Unclassified Cloud Support Package <br> OPTION PERIOD 2</dd></dl>", "<dl><dt>CLIN 2003</dt> - <dd>Unclassified Cloud Support Package <br> OPTION PERIOD 2</dd></dl>",
validators=[DataRequired()], validators=[InputRequired()],
description="Review your task order document, the amounts for each CLIN must match exactly here", description="Review your task order document, the amounts for each CLIN must match exactly here",
filters=[number_to_int], filters=[number_to_int],
) )
@ -163,6 +163,6 @@ class ExtendedFinancialForm(BaseFinancialForm):
"Upload a copy of your Task Order", "Upload a copy of your Task Order",
validators=[ validators=[
FileAllowed(["pdf"], "Only PDF documents can be uploaded."), FileAllowed(["pdf"], "Only PDF documents can be uploaded."),
DataRequired(), InputRequired(),
], ],
) )

View File

@ -1,5 +1,6 @@
from flask import g, render_template, redirect, url_for from flask import g, render_template, redirect, url_for
from flask import request as http_request from flask import request as http_request
from werkzeug.datastructures import ImmutableMultiDict
from . import requests_bp from . import requests_bp
from atst.domain.requests import Requests from atst.domain.requests import Requests
@ -61,6 +62,10 @@ class UpdateFinancialVerification(object):
def _get_form(self): def _get_form(self):
data = self.fv_data data = self.fv_data
existing_fv_data = self.request.body.get("financial_verification", {})
data = {**data, **existing_fv_data}
if self.request.task_order: if self.request.task_order:
task_order_dict = self.request.task_order.to_dictionary() task_order_dict = self.request.task_order.to_dictionary()
task_order_dict.update({ task_order_dict.update({
@ -69,10 +74,11 @@ class UpdateFinancialVerification(object):
}) })
data = {**data, **task_order_dict} data = {**data, **task_order_dict}
mdict = ImmutableMultiDict(data)
if self.is_extended: if self.is_extended:
return ExtendedFinancialForm(data=data) return ExtendedFinancialForm(formdata=mdict)
else: else:
return FinancialForm(data=data) return FinancialForm(formdata=mdict)
def execute(self): def execute(self):
form = self._get_form() form = self._get_form()
@ -149,6 +155,8 @@ def update_financial_verification(request_id):
fv_data = http_request.form fv_data = http_request.form
is_extended = http_request.args.get("extended") is_extended = http_request.args.get("extended")
import ipdb; ipdb.set_trace()
try: try:
response_context = UpdateFinancialVerification( response_context = UpdateFinancialVerification(
PENumberValidator(), PENumberValidator(),