From 13e0d60d8e64f8858dd7be685440a3455cf3430e Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Fri, 29 Jan 2021 21:19:32 +0100 Subject: [PATCH] [Plugin] Balance: Simplified get_balance --- .../plugins/balance/balance_controller.py | 27 +++++-------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/flaschengeist/plugins/balance/balance_controller.py b/flaschengeist/plugins/balance/balance_controller.py index 475ef2a..35050ff 100644 --- a/flaschengeist/plugins/balance/balance_controller.py +++ b/flaschengeist/plugins/balance/balance_controller.py @@ -28,27 +28,14 @@ def get_limit(user: User) -> float: def get_balance(user, start: datetime = None, end: datetime = None): - if not start: - start = datetime.fromtimestamp(0, tz=timezone.utc) - if not end: - end = datetime.now(tz=timezone.utc) - - credit = ( - db.session.query(func.sum(Transaction.amount)) - .filter(Transaction.receiver_ == user) - .filter(start <= Transaction.time) - .filter(Transaction.time <= end) - .scalar() - ) or 0 - - debit = ( - db.session.query(func.sum(Transaction.amount)) - .filter(Transaction.sender_ == user) - .filter(start <= Transaction.time) - .filter(Transaction.time <= end) - .scalar() - ) or 0 + query = db.session.query(func.sum(Transaction.amount)) + if start: + query = query.filter(start <= Transaction.time) + if end: + query = query.filter(Transaction.time <= end) + credit = query.filter(Transaction.receiver_ == user).scalar() or 0 + debit = query.filter(Transaction.sender_ == user).scalar() or 0 return credit, debit, credit - debit