improve balances query.

This commit is contained in:
Alexis Lahouze 2017-05-25 21:23:56 +02:00
parent d9e9e3f2a0
commit 5b87a27667

View File

@ -30,20 +30,24 @@ class Account(db.Model):
def balances(self): def balances(self):
"""Return the balances of this account.""" """Return the balances of this account."""
return db.session.query( return db.session.query(
db.func.sum(Operation.value).label("future"), db.func.coalesce(
db.func.sum( db.func.sum(
db.case( Operation.value
[(Operation.pointed, ), 0
Operation.value)], ).label("future"),
else_=0 db.func.coalesce(
) db.func.sum(
Operation.value
).filter(
Operation.pointed
), 0
).label("pointed"), ).label("pointed"),
db.func.coalesce(
db.func.sum( db.func.sum(
db.case( Operation.value,
[(Operation.operation_date <= db.func.current_date(), ).filter(
Operation.value)], Operation.operation_date <= db.func.current_date()
else_=0 ), 0
)
).label("current"), ).label("current"),
).filter( ).filter(
Operation.account_id == self.id Operation.account_id == self.id