From 55736b723eb221d1d8bfa789d154780d62a27fbf Mon Sep 17 00:00:00 2001 From: graham-dds Date: Wed, 5 Feb 2020 15:04:28 -0500 Subject: [PATCH] Move calc of a portfolio's obligated funds to prop Add a property on the portfolio model to calculate the total obligated funds for a portfolio. This replaces a one-off calculation in a view function, and sets up functionality for future access --- atst/models/portfolio.py | 6 ++++++ atst/routes/portfolios/index.py | 10 ++-------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/atst/models/portfolio.py b/atst/models/portfolio.py index 5a8f0f1e..2ddcaa41 100644 --- a/atst/models/portfolio.py +++ b/atst/models/portfolio.py @@ -89,6 +89,12 @@ class Portfolio( def active_task_orders(self): return [task_order for task_order in self.task_orders if task_order.is_active] + @property + def total_obligated_funds(self): + return sum( + (task_order.total_obligated_funds for task_order in self.active_task_orders) + ) + @property def funding_duration(self): """ diff --git a/atst/routes/portfolios/index.py b/atst/routes/portfolios/index.py index f9e7d5cf..c0fea654 100644 --- a/atst/routes/portfolios/index.py +++ b/atst/routes/portfolios/index.py @@ -40,17 +40,11 @@ def reports(portfolio_id): if any(map(lambda clin: clin["remaining"] < 0, current_obligated_funds)): flash("insufficient_funds") - # wrapped in str() because the sum of obligated funds returns a Decimal object - total_portfolio_value = str( - sum( - task_order.total_obligated_funds - for task_order in portfolio.active_task_orders - ) - ) return render_template( "portfolios/reports/index.html", portfolio=portfolio, - total_portfolio_value=total_portfolio_value, + # wrapped in str() because the sum of obligated funds returns a Decimal object + total_portfolio_value=str(portfolio.total_obligated_funds), current_obligated_funds=current_obligated_funds, expired_task_orders=Reports.expired_task_orders(portfolio), monthly_spending=Reports.monthly_spending(portfolio),