Update models: Bill.pay_each()

SQL query is faster for summing up weights
This commit is contained in:
DavidRThrashJr 2020-02-08 19:02:47 -05:00 committed by GitHub
parent 1f62f18154
commit 3c4437643e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -376,8 +376,10 @@ class Bill(db.Model):
def pay_each(self):
"""Compute what each share has to pay"""
if self.owers:
# FIXME: SQL might do that more efficiently
weights = sum(i.weight for i in self.owers)
weights = (db.session.query(func.sum(Person.weight))
.join(billowers, Bill)
.filter(Bill.id == self.id))\
.scalar()
return self.amount / weights
else:
return 0