[Plugin] Balance: Return Transaction count when using limit parameter for pagination
This commit is contained in:
parent
2bbc4898e7
commit
d1157f0f37
|
@ -164,6 +164,7 @@ def get_balance(userid, current_session: Session):
|
||||||
@login_required(permission=permissions.SHOW)
|
@login_required(permission=permissions.SHOW)
|
||||||
def get_transactions(userid, current_session: Session):
|
def get_transactions(userid, current_session: Session):
|
||||||
"""Get transactions of user, optionally filtered
|
"""Get transactions of user, optionally filtered
|
||||||
|
Returns also count of transactions if limit is set (e.g. just count with limit = 0)
|
||||||
|
|
||||||
Route: ``/users/<userid>/balance/transactions`` | Method: ``GET``
|
Route: ``/users/<userid>/balance/transactions`` | Method: ``GET``
|
||||||
|
|
||||||
|
@ -174,7 +175,7 @@ def get_transactions(userid, current_session: Session):
|
||||||
current_session: Session sent with Authorization Header
|
current_session: Session sent with Authorization Header
|
||||||
|
|
||||||
Returns:
|
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):
|
if userid != current_session._user.userid and not current_session._user.has_permission(permissions.SHOW_OTHER):
|
||||||
raise Forbidden
|
raise Forbidden
|
||||||
|
@ -191,15 +192,15 @@ def get_transactions(userid, current_session: Session):
|
||||||
limit = request.args.get("limit")
|
limit = request.args.get("limit")
|
||||||
offset = request.args.get("offset")
|
offset = request.args.get("offset")
|
||||||
try:
|
try:
|
||||||
if limit:
|
if limit is not None:
|
||||||
limit = int(limit)
|
limit = int(limit)
|
||||||
if offset:
|
if offset is not None:
|
||||||
offset = int(offset)
|
offset = int(offset)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise BadRequest
|
raise BadRequest
|
||||||
|
|
||||||
transactions = balance_controller.get_transactions(user, start, end, limit, offset)
|
transactions, count = balance_controller.get_transactions(user, start, end, limit, offset)
|
||||||
return jsonify(transactions)
|
return {"transactions": transactions, "count": count}
|
||||||
|
|
||||||
|
|
||||||
@balance_bp.route("/users/<userid>/balance", methods=["PUT"])
|
@balance_bp.route("/users/<userid>/balance", methods=["PUT"])
|
||||||
|
|
|
@ -101,17 +101,20 @@ def get_transaction(transaction_id) -> Transaction:
|
||||||
|
|
||||||
|
|
||||||
def get_transactions(user, start=None, end=None, limit=None, offset=None):
|
def get_transactions(user, start=None, end=None, limit=None, offset=None):
|
||||||
|
count = None
|
||||||
query = Transaction.query.filter((Transaction.sender_ == user) | (Transaction.receiver_ == user))
|
query = Transaction.query.filter((Transaction.sender_ == user) | (Transaction.receiver_ == user))
|
||||||
if start:
|
if start:
|
||||||
query = query.filter(start <= Transaction.time)
|
query = query.filter(start <= Transaction.time)
|
||||||
if end:
|
if end:
|
||||||
query = query.filter(Transaction.time <= 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:
|
if offset is not None:
|
||||||
query = query.offset(offset)
|
query = query.offset(offset)
|
||||||
if limit is not None:
|
|
||||||
query = query.limit(limit)
|
return query.all(), count
|
||||||
return query.all()
|
|
||||||
|
|
||||||
|
|
||||||
def reverse_transaction(transaction: Transaction, author: User):
|
def reverse_transaction(transaction: Transaction, author: User):
|
||||||
|
|
Loading…
Reference in New Issue