def cancel_edit(task_order_id=None, portfolio_id=None): # Either save the currently entered data, or delete the TO save = http_request.args.get("save", "True").lower() == "true" if save: form_data = {**http_request.form} form = None if task_order_id: task_order = TaskOrders.get(task_order_id) form = TaskOrderForm(form_data, obj=task_order) else: form = TaskOrderForm(form_data) if form.validate(flash_invalid=False): task_order = None if task_order_id: task_order = TaskOrders.update(task_order_id, **form.data) else: task_order = TaskOrders.create( g.current_user, portfolio_id, **form.data ) elif not save and task_order_id: TaskOrders.delete(task_order_id) return redirect( url_for("task_orders.portfolio_funding", portfolio_id=g.portfolio.id) )
def update_task_order( form_data, next_page, current_template, portfolio_id=None, task_order_id=None ): form = None if task_order_id: task_order = TaskOrders.get(task_order_id) form = TaskOrderForm(form_data, obj=task_order) else: form = TaskOrderForm(form_data) if form.validate(): task_order = None if task_order_id: task_order = TaskOrders.update(task_order_id, **form.data) portfolio_id = task_order.portfolio_id else: task_order = TaskOrders.create(g.current_user, portfolio_id, **form.data) return redirect(url_for(next_page, task_order_id=task_order.id)) else: return ( render_task_orders_edit( current_template, portfolio_id, task_order_id, form ), 400, )
def submit_task_order(task_order_id): task_order = TaskOrders.get(task_order_id) TaskOrders.sign(task_order=task_order, signer_dod_id=g.current_user.dod_id) flash("task_order_submitted", task_order=task_order) return redirect( url_for("task_orders.portfolio_funding", portfolio_id=task_order.portfolio_id) )
def test_review_task_order_draft(client, user_session, task_order): TaskOrders.update(task_order_id=task_order.id, number="1234567890", clins=[], pdf=None) user_session(task_order.portfolio.owner) response = client.get( url_for("task_orders.review_task_order", task_order_id=task_order.id)) assert response.status_code == 302 assert url_for("task_orders.edit", task_order_id=task_order.id) in response.location
def test_delete_task_order_with_clins(session): task_order = TaskOrderFactory.create(create_clins=[{ "number": 1 }, { "number": 2 }, { "number": 3 }]) TaskOrders.delete(task_order.id) assert not session.query( session.query(TaskOrder).filter_by( id=task_order.id).exists()).scalar()
def test_update_adds_clins(): task_order = TaskOrderFactory.create(number="1231231234") to_number = task_order.number clins = [ { "jedi_clin_type": "JEDI_CLIN_1", "number": "12312", "start_date": date(2020, 1, 1), "end_date": date(2021, 1, 1), "obligated_amount": Decimal("5000"), "total_amount": Decimal("10000"), }, { "jedi_clin_type": "JEDI_CLIN_1", "number": "12312", "start_date": date(2020, 1, 1), "end_date": date(2021, 1, 1), "obligated_amount": Decimal("5000"), "total_amount": Decimal("10000"), }, ] task_order = TaskOrders.create( creator=task_order.creator, portfolio_id=task_order.portfolio_id, number="0000000000", clins=clins, pdf={ "filename": "sample.pdf", "object_name": "1234567" }, ) assert task_order.number != to_number assert len(task_order.clins) == 2
def render_task_orders_edit( template, portfolio_id=None, task_order_id=None, form=None, extra_args=None ): render_args = extra_args or {} render_args["contract_start"] = app.config.get("CONTRACT_START_DATE") render_args["contract_end"] = app.config.get("CONTRACT_END_DATE") if task_order_id: task_order = TaskOrders.get(task_order_id) portfolio_id = task_order.portfolio_id render_args["form"] = form or TaskOrderForm(obj=task_order) render_args["task_order_id"] = task_order_id render_args["task_order"] = task_order else: render_args["form"] = form or TaskOrderForm() render_args["cancel_save_url"] = url_for( "task_orders.cancel_edit", task_order_id=task_order_id, portfolio_id=portfolio_id, save=True, ) render_args["cancel_discard_url"] = url_for( "task_orders.cancel_edit", task_order_id=task_order_id, portfolio_id=portfolio_id, save=False, ) return render_template(template, **render_args)
def edit(task_order_id): task_order = TaskOrders.get(task_order_id) if not task_order.pdf: return redirect( url_for("task_orders.form_step_one_add_pdf", task_order_id=task_order_id) ) elif not task_order.number: return redirect( url_for("task_orders.form_step_two_add_number", task_order_id=task_order_id) ) elif not task_order.clins_are_completed: return redirect( url_for( "task_orders.form_step_three_add_clins", task_order_id=task_order_id ) ) elif task_order.is_completed: return redirect( url_for("task_orders.form_step_four_review", task_order_id=task_order_id) ) else: return redirect( url_for("task_orders.form_step_one_add_pdf", task_order_id=task_order_id) )
def test_create_adds_clins(): portfolio = PortfolioFactory.create() clins = [ { "jedi_clin_type": "JEDI_CLIN_1", "number": "12312", "start_date": date(2020, 1, 1), "end_date": date(2021, 1, 1), "obligated_amount": Decimal("5000"), "total_amount": Decimal("10000"), }, { "jedi_clin_type": "JEDI_CLIN_1", "number": "12312", "start_date": date(2020, 1, 1), "end_date": date(2021, 1, 1), "obligated_amount": Decimal("5000"), "total_amount": Decimal("10000"), }, ] task_order = TaskOrders.create( creator=portfolio.owner, portfolio_id=portfolio.id, number="0123456789", clins=clins, pdf={ "filename": "sample.pdf", "object_name": "1234567" }, ) assert len(task_order.clins) == 2
def portfolio_funding(portfolio_id): portfolio = Portfolios.get(g.current_user, portfolio_id) task_orders = TaskOrders.sort_by_status(portfolio.task_orders) to_count = len(portfolio.task_orders) # TODO: Get expended amount from the CSP return render_template("task_orders/index.html", task_orders=task_orders, to_count=to_count)
def update_task_order(form, portfolio_id=None, task_order_id=None, flash_invalid=True): if form.validate(flash_invalid=flash_invalid): task_order = None try: if task_order_id: task_order = TaskOrders.update(task_order_id, **form.data) portfolio_id = task_order.portfolio_id else: task_order = TaskOrders.create(portfolio_id, **form.data) return task_order except AlreadyExistsError: flash("task_order_number_error", to_number=form.data["number"]) return False else: return False
def form_step_five_confirm_signature(task_order_id): task_order = TaskOrders.get(task_order_id) if task_order.is_completed == False: raise NoAccessError("task order form signature") return render_task_orders_edit( "task_orders/step_5.html", task_order_id=task_order_id, form=SignatureForm() )
def review_task_order(task_order_id): task_order = TaskOrders.get(task_order_id) if task_order.is_draft: return redirect(url_for("task_orders.edit", task_order_id=task_order.id)) else: signature_form = SignatureForm() return render_template( "task_orders/review.html", task_order=task_order, signature_form=signature_form, )
def test_task_orders_submit_form_step_three_add_clins_existing_to( client, user_session, task_order): clin_list = [ { "jedi_clin_type": "JEDI_CLIN_1", "number": "12312", "start_date": "01/01/2020", "end_date": "01/01/2021", "obligated_amount": "5000", "total_amount": "10000", }, { "jedi_clin_type": "JEDI_CLIN_1", "number": "12312", "start_date": "01/01/2020", "end_date": "01/01/2021", "obligated_amount": "5000", "total_amount": "10000", }, ] TaskOrders.create_clins(task_order.id, clin_list) assert len(task_order.clins) == 2 user_session(task_order.creator) form_data = { "clins-0-jedi_clin_type": "JEDI_CLIN_1", "clins-0-clin_number": "12312", "clins-0-start_date": "01/01/2020", "clins-0-end_date": "01/01/2021", "clins-0-obligated_amount": "5000", "clins-0-total_amount": "10000", } response = client.post( url_for("task_orders.submit_form_step_three_add_clins", task_order_id=task_order.id), data=form_data, ) assert response.status_code == 302 assert len(task_order.clins) == 1
def portfolio_funding(portfolio_id): portfolio = Portfolios.get(g.current_user, portfolio_id) task_orders = TaskOrders.sort(portfolio.task_orders) label_colors = { TaskOrderStatus.DRAFT: "warning", TaskOrderStatus.ACTIVE: "success", TaskOrderStatus.UPCOMING: "info", TaskOrderStatus.EXPIRED: "error", TaskOrderStatus.UNSIGNED: "purple", } return render_template( "task_orders/index.html", task_orders=task_orders, label_colors=label_colors )
def form_step_four_review(task_order_id): task_order = TaskOrders.get(task_order_id) extra_args = { "pdf_download_url": app.csp.files.generate_download_link( task_order.pdf.object_name, task_order.pdf.filename ) } if task_order.is_completed == False: raise NoAccessError("task order form review") return render_task_orders_edit( "task_orders/step_4.html", task_order_id=task_order_id, extra_args=extra_args )
def test_task_order_sort_by_status(): today = date.today() yesterday = today - timedelta(days=1) future = today + timedelta(days=100) initial_to_list = [ # Draft TaskOrderFactory.create(pdf=None), TaskOrderFactory.create(pdf=None), TaskOrderFactory.create(pdf=None), # Active TaskOrderFactory.create( signed_at=yesterday, clins=[CLINFactory.create(start_date=yesterday, end_date=future)], ), # Upcoming TaskOrderFactory.create( signed_at=yesterday, clins=[CLINFactory.create(start_date=future, end_date=future)], ), # Expired TaskOrderFactory.create( signed_at=yesterday, clins=[ CLINFactory.create(start_date=yesterday, end_date=yesterday) ], ), TaskOrderFactory.create( signed_at=yesterday, clins=[ CLINFactory.create(start_date=yesterday, end_date=yesterday) ], ), # Unsigned TaskOrderFactory.create( clins=[CLINFactory.create(start_date=today, end_date=today)]), ] sorted_by_status = TaskOrders.sort_by_status(initial_to_list) assert len(sorted_by_status["Draft"]) == 3 assert len(sorted_by_status["Active"]) == 1 assert len(sorted_by_status["Upcoming"]) == 1 assert len(sorted_by_status["Expired"]) == 2 assert len(sorted_by_status["Unsigned"]) == 1 assert list( sorted_by_status.keys()) == [status.value for status in SORT_ORDERING]
def update_and_render_next( form_data, next_page, current_template, portfolio_id=None, task_order_id=None ): form = None if task_order_id: task_order = TaskOrders.get(task_order_id) form = TaskOrderForm(form_data, obj=task_order) else: form = TaskOrderForm(form_data) task_order = update_task_order(form, portfolio_id, task_order_id) if task_order: return redirect(url_for(next_page, task_order_id=task_order.id)) else: return ( render_task_orders_edit( current_template, portfolio_id, task_order_id, form ), 400, )
def test_update_does_not_duplicate_clins(): task_order = TaskOrderFactory.create(number="3453453456", create_clins=[{ "number": "123" }, { "number": "456" }]) clins = [ { "jedi_clin_type": "JEDI_CLIN_1", "number": "123", "start_date": date(2020, 1, 1), "end_date": date(2021, 1, 1), "obligated_amount": Decimal("5000"), "total_amount": Decimal("10000"), }, { "jedi_clin_type": "JEDI_CLIN_1", "number": "111", "start_date": date(2020, 1, 1), "end_date": date(2021, 1, 1), "obligated_amount": Decimal("5000"), "total_amount": Decimal("10000"), }, ] task_order = TaskOrders.update( task_order_id=task_order.id, number="0000000000", clins=clins, pdf={ "filename": "sample.pdf", "object_name": "1234567" }, ) assert len(task_order.clins) == 2 for clin in task_order.clins: assert clin.number != "456"
def test_review_task_order_not_draft(client, user_session, task_order): TaskOrders.sign(task_order=task_order, signer_dod_id=random_dod_id()) user_session(task_order.portfolio.owner) response = client.get( url_for("task_orders.review_task_order", task_order_id=task_order.id)) assert response.status_code == 200
def test_task_order_sorting(): """ Task orders should be listed first by status, and then by time_created. """ today = date.today() yesterday = today - timedelta(days=1) future = today + timedelta(days=100) task_orders = [ # Draft TaskOrderFactory.create(pdf=None), TaskOrderFactory.create(pdf=None), TaskOrderFactory.create(pdf=None), # Active TaskOrderFactory.create( signed_at=yesterday, clins=[CLINFactory.create(start_date=yesterday, end_date=future)], ), TaskOrderFactory.create( signed_at=yesterday, clins=[CLINFactory.create(start_date=yesterday, end_date=future)], ), TaskOrderFactory.create( signed_at=yesterday, clins=[CLINFactory.create(start_date=yesterday, end_date=future)], ), # Upcoming TaskOrderFactory.create( signed_at=yesterday, clins=[CLINFactory.create(start_date=future, end_date=future)], ), TaskOrderFactory.create( signed_at=yesterday, clins=[CLINFactory.create(start_date=future, end_date=future)], ), TaskOrderFactory.create( signed_at=yesterday, clins=[CLINFactory.create(start_date=future, end_date=future)], ), # Expired TaskOrderFactory.create( signed_at=yesterday, clins=[ CLINFactory.create(start_date=yesterday, end_date=yesterday) ], ), TaskOrderFactory.create( signed_at=yesterday, clins=[ CLINFactory.create(start_date=yesterday, end_date=yesterday) ], ), TaskOrderFactory.create( signed_at=yesterday, clins=[ CLINFactory.create(start_date=yesterday, end_date=yesterday) ], ), # Unsigned TaskOrderFactory.create( clins=[CLINFactory.create(start_date=today, end_date=today)]), TaskOrderFactory.create( clins=[CLINFactory.create(start_date=today, end_date=today)]), TaskOrderFactory.create( clins=[CLINFactory.create(start_date=today, end_date=today)]), ] assert TaskOrders.sort(task_orders) == task_orders
def test_create_enforces_unique_number(): portfolio = PortfolioFactory.create() number = "1234567890123" assert TaskOrders.create(portfolio.id, number, [], None) with pytest.raises(AlreadyExistsError): TaskOrders.create(portfolio.id, number, [], None)
def test_update_enforces_unique_number(): task_order = TaskOrderFactory.create() dupe_task_order = TaskOrderFactory.create() with pytest.raises(AlreadyExistsError): TaskOrders.update(dupe_task_order.id, task_order.number, [], None)
def download_task_order_pdf(task_order_id): task_order = TaskOrders.get(task_order_id) if task_order.pdf: return send_file(task_order.pdf) else: raise NotFoundError("task_order pdf")