From d1157f0f37109126fadc86a62a1874dba530f8ee Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Fri, 29 Jan 2021 20:08:34 +0100 Subject: [PATCH] [Plugin] Balance: Return Transaction count when using limit parameter for pagination --- flaschengeist/plugins/balance/__init__.py | 11 ++++++----- flaschengeist/plugins/balance/balance_controller.py | 11 +++++++---- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/flaschengeist/plugins/balance/__init__.py b/flaschengeist/plugins/balance/__init__.py index 71c0bcf..b916656 100644 --- a/flaschengeist/plugins/balance/__init__.py +++ b/flaschengeist/plugins/balance/__init__.py @@ -164,6 +164,7 @@ def get_balance(userid, current_session: Session): @login_required(permission=permissions.SHOW) def get_transactions(userid, current_session: Session): """Get transactions of user, optionally filtered + Returns also count of transactions if limit is set (e.g. just count with limit = 0) Route: ``/users//balance/transactions`` | Method: ``GET`` @@ -174,7 +175,7 @@ def get_transactions(userid, current_session: Session): current_session: Session sent with Authorization Header Returns: - JSON encoded list of transactions or HTTP error + JSON Object {transactions: Transaction[], count?: number} or HTTP error """ if userid != current_session._user.userid and not current_session._user.has_permission(permissions.SHOW_OTHER): raise Forbidden @@ -191,15 +192,15 @@ def get_transactions(userid, current_session: Session): limit = request.args.get("limit") offset = request.args.get("offset") try: - if limit: + if limit is not None: limit = int(limit) - if offset: + if offset is not None: offset = int(offset) except ValueError: raise BadRequest - transactions = balance_controller.get_transactions(user, start, end, limit, offset) - return jsonify(transactions) + transactions, count = balance_controller.get_transactions(user, start, end, limit, offset) + return {"transactions": transactions, "count": count} @balance_bp.route("/users//balance", methods=["PUT"]) diff --git a/flaschengeist/plugins/balance/balance_controller.py b/flaschengeist/plugins/balance/balance_controller.py index 4cf0360..cd730d5 100644 --- a/flaschengeist/plugins/balance/balance_controller.py +++ b/flaschengeist/plugins/balance/balance_controller.py @@ -101,17 +101,20 @@ def get_transaction(transaction_id) -> Transaction: def get_transactions(user, start=None, end=None, limit=None, offset=None): + count = None query = Transaction.query.filter((Transaction.sender_ == user) | (Transaction.receiver_ == user)) if start: query = query.filter(start <= Transaction.time) if end: query = query.filter(Transaction.time <= end) - query = query.order_by(Transaction.time.desc()) + query = query.order_by(Transaction.time) + if limit is not None: + count = query.count() + query = query.limit(limit) if offset is not None: query = query.offset(offset) - if limit is not None: - query = query.limit(limit) - return query.all() + + return query.all(), count def reverse_transaction(transaction: Transaction, author: User):