From ff9ead22031563b31f14e87ae3b3c537a06b8c41 Mon Sep 17 00:00:00 2001 From: "A.Avenel" Date: Thu, 9 May 2013 23:23:23 +0200 Subject: [PATCH] Optimization --- budget/models.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/budget/models.py b/budget/models.py index 49c3eb4f..2d441452 100644 --- a/budget/models.py +++ b/budget/models.py @@ -51,13 +51,15 @@ class Project(db.Model): def get_transactions_to_settle_bill(self): """Return a list of transactions that could be made to settle the bill""" + #cache value for better performance + balance = self.balance credits, debts, transactions = [],[],[] # Create lists of credits and debts for person in self.members: - if self.balance[person.id] > 0: - credits.append({"person": person, "balance": self.balance[person.id]}) - elif self.balance[person.id] < 0: - debts.append({"person": person, "balance": -self.balance[person.id]}) + if balance[person.id] > 0: + credits.append({"person": person, "balance": balance[person.id]}) + elif balance[person.id] < 0: + debts.append({"person": person, "balance": -balance[person.id]}) # Try and find exact matches for credit in credits: match = self.exactmatch(credit["balance"], debts)