use financial verification service class for all routes

This commit is contained in:
dandds 2018-10-03 15:47:01 -04:00
parent 6419d96bbe
commit 3384738f5d
2 changed files with 78 additions and 60 deletions

View File

@ -1,8 +1,10 @@
from sqlalchemy import exists, and_, exc, text from sqlalchemy import exists, and_, exc, text
from sqlalchemy.orm.exc import NoResultFound
from atst.database import db from atst.database import db
from atst.domain.common import Query from atst.domain.common import Query
from atst.models.request import Request from atst.models.request import Request
from atst.domain.exceptions import NotFoundError
class RequestsQuery(Query): class RequestsQuery(Query):

View File

@ -6,32 +6,20 @@ from atst.domain.requests import Requests
from atst.forms.financial import FinancialForm, ExtendedFinancialForm from atst.forms.financial import FinancialForm, ExtendedFinancialForm
def task_order_data(task_order):
data = task_order.to_dictionary()
data["task_order_number"] = task_order.number
data["funding_type"] = task_order.funding_type.value
return data
def is_extended(request):
return (
http_request.args.get("extended")
or request.is_pending_financial_verification_changes
)
def financial_form(request, data):
if is_extended(request):
return ExtendedFinancialForm(data=data)
else:
return FinancialForm(data=data)
class FinancialVerification: class FinancialVerification:
def __init__(self, user, request_id=None, extended=False, post_data=None): def __init__(self, user, request_id=None, extended=False, post_data=None):
self.request = Requests.get(user, request_id) self.request = Requests.get(user, request_id)
self._extended = extended self._extended = extended
self.post_data = post_data self.post_data = post_data
self._form = None
self.reset()
def reset(self):
self.updateable = False
self.valid = False
self.workspace = None
if self._form:
self._form.reset()
@property @property
def is_extended(self): def is_extended(self):
@ -53,6 +41,9 @@ class FinancialVerification:
@property @property
def _form_data(self): def _form_data(self):
if self.post_data:
return self.post_data
else:
form_data = self.request.body.get("financial_verification", {}) form_data = self.request.body.get("financial_verification", {})
form_data.update(self._task_order_data) form_data.update(self._task_order_data)
@ -60,16 +51,49 @@ class FinancialVerification:
@property @property
def form(self): def form(self):
if not self._form:
if self.is_extended: if self.is_extended:
return ExtendedFinancialForm(self._form_data) self._form = ExtendedFinancialForm(data=self._form_data)
else: else:
return FinancialForm(self._form_data) self._form = FinancialForm(data=self._form_data)
return self._form
def validate(self):
if self.form.validate():
self.updateable = True
self.valid = self.form.perform_extra_validation(
self.request.body.get("financial_verification")
)
else:
self.updateable = False
self.valid = False
return self.valid
@property
def pending(self):
return self.request.is_pending_ccpo_approval
def finalize(self):
if self.updateable:
self.request = Requests.update_financial_verification(
self.request.id, self.form.data
)
if self.valid:
self.request = Requests.submit_financial_verification(self.request)
if self.request.is_financially_verified:
self.workspace = Requests.approve_and_create_workspace(self.request)
@requests_bp.route("/requests/verify/<string:request_id>", methods=["GET"]) @requests_bp.route("/requests/verify/<string:request_id>", methods=["GET"])
def financial_verification(request_id): def financial_verification(request_id):
finver = FinancialVerification( finver = FinancialVerification(
g.current_user, request_id=request_id, extended=http_request.args.get("extended") g.current_user,
request_id=request_id,
extended=http_request.args.get("extended"),
) )
return render_template( return render_template(
@ -83,40 +107,32 @@ def financial_verification(request_id):
@requests_bp.route("/requests/verify/<string:request_id>", methods=["POST"]) @requests_bp.route("/requests/verify/<string:request_id>", methods=["POST"])
def update_financial_verification(request_id): def update_financial_verification(request_id):
post_data = http_request.form finver = FinancialVerification(
existing_request = Requests.get(g.current_user, request_id) g.current_user,
form = financial_form(existing_request, post_data) request_id=request_id,
rerender_args = dict( extended=http_request.args.get("extended"),
jedi_request=existing_request, f=form, extended=is_extended(existing_request) post_data=http_request.form,
) )
if form.validate(): finver.validate()
valid = form.perform_extra_validation(
existing_request.body.get("financial_verification") finver.finalize()
)
updated_request = Requests.update_financial_verification(request_id, form.data) if finver.workspace:
if valid:
submitted_request = Requests.submit_financial_verification(updated_request)
if submitted_request.is_financially_verified:
new_workspace = Requests.approve_and_create_workspace(submitted_request)
return redirect( return redirect(
url_for( url_for(
"workspaces.new_project", "workspaces.new_project",
workspace_id=new_workspace.id, workspace_id=finver.workspace.id,
newWorkspace=True, newWorkspace=True,
) )
) )
elif finver.pending:
return redirect(url_for("requests.requests_index", modal="pendingCCPOApproval"))
else: else:
return redirect( finver.reset()
url_for("requests.requests_index", modal="pendingCCPOApproval")
)
else:
form.reset()
return render_template( return render_template(
"requests/financial_verification.html", **rerender_args "requests/financial_verification.html",
jedi_request=finver.request,
f=finver.form,
extended=finver.is_extended,
) )
else:
form.reset()
return render_template("requests/financial_verification.html", **rerender_args)