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): 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