mirror of
https://github.com/spiral-project/ihatemoney.git
synced 2025-05-06 05:01:48 +02:00
Update models: Bill.pay_each()
SQL query is faster for summing up weights
This commit is contained in:
parent
1f62f18154
commit
3c4437643e
1 changed files with 4 additions and 2 deletions
|
@ -376,8 +376,10 @@ class Bill(db.Model):
|
||||||
def pay_each(self):
|
def pay_each(self):
|
||||||
"""Compute what each share has to pay"""
|
"""Compute what each share has to pay"""
|
||||||
if self.owers:
|
if self.owers:
|
||||||
# FIXME: SQL might do that more efficiently
|
weights = (db.session.query(func.sum(Person.weight))
|
||||||
weights = sum(i.weight for i in self.owers)
|
.join(billowers, Bill)
|
||||||
|
.filter(Bill.id == self.id))\
|
||||||
|
.scalar()
|
||||||
return self.amount / weights
|
return self.amount / weights
|
||||||
else:
|
else:
|
||||||
return 0
|
return 0
|
||||||
|
|
Loading…
Reference in a new issue