From 2b071a1a3bc752bfaa0fd6c0b2d8f8460721d6d8 Mon Sep 17 00:00:00 2001 From: Jocelyn Delande Date: Thu, 20 Aug 2015 10:33:43 +0200 Subject: Add members weight in models and budget backend refs #94 --- budget/models.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'budget/models.py') diff --git a/budget/models.py b/budget/models.py index 727200f..16cc6c1 100644 --- a/budget/models.py +++ b/budget/models.py @@ -40,8 +40,9 @@ class Project(db.Model): bills = Bill.query.filter(Bill.owers.contains(person)) for bill in bills.all(): if person != bill.payer: - should_pay[person] += bill.pay_each() - should_receive[bill.payer] += bill.pay_each() + share = bill.pay_each() * person.weight + should_pay[person] += share + should_receive[bill.payer] += share for person in self.members: balance = should_receive[person] - should_pay[person] @@ -159,6 +160,7 @@ class Person(db.Model): bills = db.relationship("Bill", backref="payer") name = db.Column(db.UnicodeText) + weight = db.Column(db.Float, default=1) activated = db.Column(db.Boolean, default=True) def has_bills(self): @@ -219,7 +221,8 @@ class Bill(db.Model): def pay_each(self): """Compute what each person has to pay""" if self.owers: - return self.amount / len(self.owers) + # FIXME: SQL might dot that more efficiently + return self.amount / sum(i.weight for i in self.owers) else: return 0 -- cgit v1.1