From 11204662be80ea2dee37d3b6e72d13eee79547ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Wed, 3 May 2023 14:03:59 +0200 Subject: [PATCH] (balance) add filter to search user --- .../plugins/balance/balance_controller.py | 16 ++++++++++++++-- flaschengeist/plugins/balance/routes.py | 7 ++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/flaschengeist/plugins/balance/balance_controller.py b/flaschengeist/plugins/balance/balance_controller.py index 48a261b..0d9093b 100644 --- a/flaschengeist/plugins/balance/balance_controller.py +++ b/flaschengeist/plugins/balance/balance_controller.py @@ -3,13 +3,14 @@ # English: Debit -> from account # Credit -> to account from enum import IntEnum -from sqlalchemy import func, case, and_ +from sqlalchemy import func, case, and_, or_ from sqlalchemy.ext.hybrid import hybrid_property from datetime import datetime from werkzeug.exceptions import BadRequest, NotFound, Conflict from flaschengeist.database import db from flaschengeist.models.user import User, _UserAttribute +from flaschengeist.app import logger from .models import Transaction from . import permissions, BalancePlugin @@ -44,7 +45,13 @@ def get_balance(user, start: datetime = None, end: datetime = None): return credit, debit, credit - debit -def get_balances(start: datetime = None, end: datetime = None, limit=None, offset=None, descending=None, sortBy=None): +def get_balances( + start: datetime = None, end: datetime = None, limit=None, offset=None, descending=None, sortBy=None, _filter=None +): + logger.debug( + f"get_balances(start={start}, end={end}, limit={limit}, offset={offset}, descending={descending}, sortBy={sortBy}, _filter={_filter})" + ) + class _User(User): _debit = db.relationship(Transaction, back_populates="sender_", foreign_keys=[Transaction._sender_id]) _credit = db.relationship(Transaction, back_populates="receiver_", foreign_keys=[Transaction._receiver_id]) @@ -116,6 +123,11 @@ def get_balances(start: datetime = None, end: datetime = None, limit=None, offse q2 = query.join(_User._debit).filter(Transaction.time <= end) query = q1.union(q2) + if _filter: + query = query.filter( + or_(_User.firstname.ilike(f"%{_filter.lower()}%"), _User.lastname.ilike(f"%{_filter.lower()}%")) + ) + if sortBy == "balance": if descending: query = query.order_by((_User.credit - _User.debit).desc(), _User.lastname.asc(), _User.firstname.asc()) diff --git a/flaschengeist/plugins/balance/routes.py b/flaschengeist/plugins/balance/routes.py index f0edc62..8ba1efb 100644 --- a/flaschengeist/plugins/balance/routes.py +++ b/flaschengeist/plugins/balance/routes.py @@ -7,6 +7,7 @@ from flaschengeist.models.session import Session from flaschengeist.utils.datetime import from_iso_format from flaschengeist.utils.decorators import login_required from flaschengeist.controller import userController +from flaschengeist.app import logger from . import BalancePlugin, balance_controller, permissions @@ -317,7 +318,11 @@ def get_balances(current_session: Session): offset = request.args.get("offset", type=int) descending = request.args.get("descending", False, type=bool) sortBy = request.args.get("sortBy", type=str) - balances, count = balance_controller.get_balances(limit=limit, offset=offset, descending=descending, sortBy=sortBy) + _filter = request.args.get("filter", None, type=str) + logger.debug(f"request.args: {request.args}") + balances, count = balance_controller.get_balances( + limit=limit, offset=offset, descending=descending, sortBy=sortBy, _filter=_filter + ) return jsonify( { "balances": [{"userid": u, "credit": v[0], "debit": v[1]} for u, v in balances.items()],