(balance) add filter to search user
This commit is contained in:
parent
cb0795a6ac
commit
11204662be
|
@ -3,13 +3,14 @@
|
||||||
# English: Debit -> from account
|
# English: Debit -> from account
|
||||||
# Credit -> to account
|
# Credit -> to account
|
||||||
from enum import IntEnum
|
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 sqlalchemy.ext.hybrid import hybrid_property
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from werkzeug.exceptions import BadRequest, NotFound, Conflict
|
from werkzeug.exceptions import BadRequest, NotFound, Conflict
|
||||||
|
|
||||||
from flaschengeist.database import db
|
from flaschengeist.database import db
|
||||||
from flaschengeist.models.user import User, _UserAttribute
|
from flaschengeist.models.user import User, _UserAttribute
|
||||||
|
from flaschengeist.app import logger
|
||||||
|
|
||||||
from .models import Transaction
|
from .models import Transaction
|
||||||
from . import permissions, BalancePlugin
|
from . import permissions, BalancePlugin
|
||||||
|
@ -44,7 +45,13 @@ def get_balance(user, start: datetime = None, end: datetime = None):
|
||||||
return credit, debit, credit - debit
|
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):
|
class _User(User):
|
||||||
_debit = db.relationship(Transaction, back_populates="sender_", foreign_keys=[Transaction._sender_id])
|
_debit = db.relationship(Transaction, back_populates="sender_", foreign_keys=[Transaction._sender_id])
|
||||||
_credit = db.relationship(Transaction, back_populates="receiver_", foreign_keys=[Transaction._receiver_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)
|
q2 = query.join(_User._debit).filter(Transaction.time <= end)
|
||||||
query = q1.union(q2)
|
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 sortBy == "balance":
|
||||||
if descending:
|
if descending:
|
||||||
query = query.order_by((_User.credit - _User.debit).desc(), _User.lastname.asc(), _User.firstname.asc())
|
query = query.order_by((_User.credit - _User.debit).desc(), _User.lastname.asc(), _User.firstname.asc())
|
||||||
|
|
|
@ -7,6 +7,7 @@ from flaschengeist.models.session import Session
|
||||||
from flaschengeist.utils.datetime import from_iso_format
|
from flaschengeist.utils.datetime import from_iso_format
|
||||||
from flaschengeist.utils.decorators import login_required
|
from flaschengeist.utils.decorators import login_required
|
||||||
from flaschengeist.controller import userController
|
from flaschengeist.controller import userController
|
||||||
|
from flaschengeist.app import logger
|
||||||
from . import BalancePlugin, balance_controller, permissions
|
from . import BalancePlugin, balance_controller, permissions
|
||||||
|
|
||||||
|
|
||||||
|
@ -317,7 +318,11 @@ def get_balances(current_session: Session):
|
||||||
offset = request.args.get("offset", type=int)
|
offset = request.args.get("offset", type=int)
|
||||||
descending = request.args.get("descending", False, type=bool)
|
descending = request.args.get("descending", False, type=bool)
|
||||||
sortBy = request.args.get("sortBy", type=str)
|
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(
|
return jsonify(
|
||||||
{
|
{
|
||||||
"balances": [{"userid": u, "credit": v[0], "debit": v[1]} for u, v in balances.items()],
|
"balances": [{"userid": u, "credit": v[0], "debit": v[1]} for u, v in balances.items()],
|
||||||
|
|
Loading…
Reference in New Issue