X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=bluechips%2Flib%2Ftotals.py;h=94a62a127bbe73d63f70bec4f8c9e6928414a18e;hb=82ad8466cea54eb777e71bb1a74798461461598b;hp=e0c455967d1566c0815c462e9bf22677884fc008;hpb=7e074ca2b00ac193c0d8fa6a52e457045ee37493;p=bluechips.git diff --git a/bluechips/lib/totals.py b/bluechips/lib/totals.py index e0c4559..94a62a1 100644 --- a/bluechips/lib/totals.py +++ b/bluechips/lib/totals.py @@ -21,11 +21,14 @@ def debts(): # house users = meta.Session.query(model.User) - debts_dict = {} + debts_dict = dict((u, Currency(0)) for u in users) # First, credit everyone for expenditures they've made - for user in users: - debts_dict[user] = Currency(-sum(map((lambda x: x.amount), user.expenditures))) + total_expenditures = meta.Session.query(model.Expenditure).\ + add_column(sqlalchemy.func.sum(model.Expenditure.amount).label('total_spend')).\ + group_by(model.Expenditure.spender_id) + for expenditure, total_spend in total_expenditures: + debts_dict[expenditure.spender] -= total_spend # Next, debit everyone for expenditures that they have an # investment in (i.e. splits)