atst/atst/routes/requests/approval.py
2018-09-24 08:28:14 -04:00

111 lines
3.7 KiB
Python

from flask import (
render_template,
g,
Response,
request as http_request,
redirect,
url_for,
)
from flask import current_app as app
from . import requests_bp
from atst.domain.requests import Requests
from atst.domain.exceptions import NotFoundError
from atst.forms.ccpo_review import CCPOReviewForm
from atst.forms.internal_comment import InternalCommentForm
from datetime import date
def map_ccpo_authorizing(user):
return {"fname_ccpo": user.first_name, "lname_ccpo": user.last_name}
def render_approval(request, form=None):
data = request.body
if request.has_financial_data:
data["task_order"] = request.task_order.to_dictionary()
if not form:
mo_data = map_ccpo_authorizing(g.current_user)
form = CCPOReviewForm(data=mo_data)
internal_comment_form = InternalCommentForm(text=request.internal_comments_text)
# Dummy internal comments
comments = [
{
"time_created": date(2018, 9, 18),
"full_name_commenter": "Darth Vader",
"message": "We'll have no more of this Obi-Wan Kenobi jibberish...and don't talk to me about your mission, either. You're fortunate he doesn't blast you into a million pieces right here. ",
},
{
"time_created": date(2018, 9, 20),
"full_name_commenter": "Grand Moff Tarkin",
"message": "We'll have no more of this Obi-Wan Kenobi jibberish...and don't talk to me about your mission, either. You're fortunate he doesn't blast you into a million pieces right here. ",
},
]
return render_template(
"requests/approval.html",
data=data,
reviews=list(reversed(request.reviews)),
request=request,
current_status=request.status.value,
f=form or CCPOReviewForm(),
internal_comment_form=internal_comment_form,
comments=comments,
)
@requests_bp.route("/requests/approval/<string:request_id>", methods=["GET"])
def approval(request_id):
request = Requests.get_for_approval(g.current_user, request_id)
return render_approval(request)
@requests_bp.route("/requests/submit_approval/<string:request_id>", methods=["POST"])
def submit_approval(request_id):
request = Requests.get_for_approval(g.current_user, request_id)
form = CCPOReviewForm(http_request.form)
if form.validate():
if http_request.form.get("review") == "approving":
Requests.advance(g.current_user, request, form.data)
else:
Requests.request_changes(g.current_user, request, form.data)
return redirect(url_for("requests.requests_index"))
else:
return render_approval(request, form)
@requests_bp.route("/requests/task_order_download/<string:request_id>", methods=["GET"])
def task_order_pdf_download(request_id):
request = Requests.get(g.current_user, request_id)
if request.task_order and request.task_order.pdf:
pdf = request.task_order.pdf
generator = app.uploader.download_stream(pdf.object_name)
return Response(
generator,
headers={
"Content-Disposition": "attachment; filename={}".format(pdf.filename)
},
mimetype="application/pdf",
)
else:
raise NotFoundError("task_order pdf")
@requests_bp.route("/requests/internal_comments/<string:request_id>", methods=["POST"])
def create_internal_comment(request_id):
# form = InternalCommentForm(http_request.form)
# if form.validate():
# request = Requests.get(g.current_user, request_id)
# Requests.update_internal_comments(g.current_user, request, form.data["text"])
return redirect(
url_for("requests.approval", request_id=request_id, _anchor="ccpo-notes")
)