X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=bluechips%2Flib%2Ftotals.py;h=94a62a127bbe73d63f70bec4f8c9e6928414a18e;hb=fed6d11d2cbd6d617d18bc01a78196865da4155b;hp=730ca648ea1b228c3f10ab712d0b77868858cd29;hpb=a9c8da2841f394df01c7f02a01a73e666b66e786;p=bluechips.git diff --git a/bluechips/lib/totals.py b/bluechips/lib/totals.py index 730ca64..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) @@ -94,9 +97,9 @@ def settle(debts_dict): settle_list.append((owes['who'], owed['who'], val)) if len(owes_list) > 0: - raise DirtyBooks, ("People still owe money", owes_list) #pragma:nocover + raise DirtyBooks, ("People still owe money", owes_list) if len(owed_list) > 0: - raise DirtyBooks, ("People are still owed money", owed_list) #pragma:nocover + raise DirtyBooks, ("People are still owed money", owed_list) return settle_list