[Plugin] Balance: Allow filtering reversals and cancelled transactions

This commit is contained in:
Ferdinand Thiessen 2021-01-29 21:06:46 +01:00
parent d1157f0f37
commit 06a806da3d
3 changed files with 24 additions and 4 deletions

View File

@ -41,6 +41,14 @@ class BalancePlugin(Plugin):
db.create_all() 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/<userid>/balance/shortcuts", methods=["GET", "PUT"]) @balance_bp.route("/users/<userid>/balance/shortcuts", methods=["GET", "PUT"])
@login_required() @login_required()
def get_shortcuts(userid, current_session: Session): def get_shortcuts(userid, current_session: Session):
@ -189,6 +197,8 @@ def get_transactions(userid, current_session: Session):
end = request.args.get("to") end = request.args.get("to")
if end: if end:
end = from_iso_format(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") limit = request.args.get("limit")
offset = request.args.get("offset") offset = request.args.get("offset")
try: try:
@ -196,10 +206,16 @@ def get_transactions(userid, current_session: Session):
limit = int(limit) limit = int(limit)
if offset is not None: if offset is not None:
offset = int(offset) 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: except ValueError:
raise BadRequest 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} return {"transactions": transactions, "count": count}

View File

@ -100,13 +100,18 @@ def get_transaction(transaction_id) -> Transaction:
return 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 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)
# 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) query = query.order_by(Transaction.time)
if limit is not None: if limit is not None:
count = query.count() count = query.count()

View File

@ -1,9 +1,8 @@
from datetime import datetime from datetime import datetime
from typing import Optional from typing import Optional
from flaschengeist.models.user import User
from flaschengeist.database import db from flaschengeist.database import db
from flaschengeist.models.user import User
from flaschengeist.models import ModelSerializeMixin, UtcDateTime from flaschengeist.models import ModelSerializeMixin, UtcDateTime