From 06a806da3d491a01afee6dbb971c68ed96c9842b Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Fri, 29 Jan 2021 21:06:46 +0100 Subject: [PATCH] [Plugin] Balance: Allow filtering reversals and cancelled transactions --- flaschengeist/plugins/balance/__init__.py | 18 +++++++++++++++++- .../plugins/balance/balance_controller.py | 7 ++++++- flaschengeist/plugins/balance/models.py | 3 +-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/flaschengeist/plugins/balance/__init__.py b/flaschengeist/plugins/balance/__init__.py index b916656..f67e924 100644 --- a/flaschengeist/plugins/balance/__init__.py +++ b/flaschengeist/plugins/balance/__init__.py @@ -41,6 +41,14 @@ class BalancePlugin(Plugin): db.create_all() +def str2bool(string: str): + if string.lower() in ["true", "yes", "1"]: + return True + elif string.lower() in ["false", "no", "0"]: + return False + raise ValueError + + @balance_bp.route("/users//balance/shortcuts", methods=["GET", "PUT"]) @login_required() def get_shortcuts(userid, current_session: Session): @@ -189,6 +197,8 @@ def get_transactions(userid, current_session: Session): end = request.args.get("to") if end: end = from_iso_format(end) + show_reversals = request.args.get("showReversals", False) + show_cancelled = request.args.get("showCancelled", True) limit = request.args.get("limit") offset = request.args.get("offset") try: @@ -196,10 +206,16 @@ def get_transactions(userid, current_session: Session): limit = int(limit) if offset is not None: offset = int(offset) + if not isinstance(show_reversals, bool): + show_reversals = str2bool(show_reversals) + if not isinstance(show_cancelled, bool): + show_cancelled = str2bool(show_cancelled) except ValueError: raise BadRequest - transactions, count = balance_controller.get_transactions(user, start, end, limit, offset) + transactions, count = balance_controller.get_transactions( + user, start, end, limit, offset, show_reversal=show_reversals, show_cancelled=show_cancelled + ) return {"transactions": transactions, "count": count} diff --git a/flaschengeist/plugins/balance/balance_controller.py b/flaschengeist/plugins/balance/balance_controller.py index cd730d5..475ef2a 100644 --- a/flaschengeist/plugins/balance/balance_controller.py +++ b/flaschengeist/plugins/balance/balance_controller.py @@ -100,13 +100,18 @@ def get_transaction(transaction_id) -> Transaction: return 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, show_reversal=False, show_cancelled=True): 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) + # Do not show reversals if disabled or cancelled ones are hidden + if not show_reversal or not show_cancelled: + query = query.filter(Transaction.original_ == None) + if not show_cancelled: + query = query.filter(Transaction.reversal_id.is_(None)) query = query.order_by(Transaction.time) if limit is not None: count = query.count() diff --git a/flaschengeist/plugins/balance/models.py b/flaschengeist/plugins/balance/models.py index e9f1d02..e981ad9 100644 --- a/flaschengeist/plugins/balance/models.py +++ b/flaschengeist/plugins/balance/models.py @@ -1,9 +1,8 @@ from datetime import datetime from typing import Optional -from flaschengeist.models.user import User - from flaschengeist.database import db +from flaschengeist.models.user import User from flaschengeist.models import ModelSerializeMixin, UtcDateTime