From 9077c9fd118eb48e75a04924916b8c662eff87b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Wed, 10 May 2023 01:12:41 +0200 Subject: [PATCH] (balance) fix notifications if only author and sender oder receiver exists, create special notifications --- flaschengeist/plugins/balance/__init__.py | 5 ++ .../plugins/balance/balance_controller.py | 49 ++++++++++++++----- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/flaschengeist/plugins/balance/__init__.py b/flaschengeist/plugins/balance/__init__.py index 03b5e4a..31fdfc3 100644 --- a/flaschengeist/plugins/balance/__init__.py +++ b/flaschengeist/plugins/balance/__init__.py @@ -5,6 +5,7 @@ Extends users plugin with balance functions from flask import current_app from werkzeug.exceptions import NotFound +from werkzeug.local import LocalProxy from flaschengeist import logger from flaschengeist.config import config @@ -82,3 +83,7 @@ class BalancePlugin(Plugin): balance_controller.set_limit(user, limit, override=False) except KeyError: pass + + @staticmethod + def getPlugin() -> LocalProxy["BalancePlugin"]: + return LocalProxy(lambda: current_app.config["FG_PLUGINS"]["balance"]) diff --git a/flaschengeist/plugins/balance/balance_controller.py b/flaschengeist/plugins/balance/balance_controller.py index 0d9093b..797b039 100644 --- a/flaschengeist/plugins/balance/balance_controller.py +++ b/flaschengeist/plugins/balance/balance_controller.py @@ -21,6 +21,8 @@ __attribute_limit = "balance_limit" class NotifyType(IntEnum): SEND_TO = 0x01 SEND_FROM = 0x02 + ADD_FROM = 0x03 + SUB_FROM = 0x04 def set_limit(user: User, limit: float, override=True): @@ -178,6 +180,7 @@ def send(sender: User, receiver, amount: float, author: User): Raises: BadRequest if amount <= 0 """ + logger.debug(f"send(sender={sender}, receiver={receiver}, amount={amount}, author={author})") if amount <= 0: raise BadRequest @@ -191,20 +194,40 @@ def send(sender: User, receiver, amount: float, author: User): db.session.add(transaction) db.session.commit() if sender is not None and sender.id_ != author.id_: - BalancePlugin.plugin.notify( - sender, - "Neue Transaktion", - { - "type": NotifyType.SEND_FROM, - "receiver_id": receiver.userid, - "author_id": author.userid, - "amount": amount, - }, - ) + if receiver is not None: + BalancePlugin.getPlugin().notify( + sender, + "Neue Transaktion", + { + "type": NotifyType.SEND_FROM, + "receiver_id": receiver.userid, + "author_id": author.userid, + "amount": amount, + }, + ) + else: + BalancePlugin.getPlugin().notify( + sender, + "Neue Transaktion", + {"type": NotifyType.SUB_FROM, "author_id": author.userid, "amount": amount}, + ) if receiver is not None and receiver.id_ != author.id_: - BalancePlugin.plugin.notify( - receiver, "Neue Transaktion", {"type": NotifyType.SEND_TO, "sender_id": sender.userid, "amount": amount} - ) + if sender is not None: + BalancePlugin.getPlugin().notify( + receiver, + "Neue Transaktion", + { + "type": NotifyType.SEND_TO, + "sender_id": sender.userid, + "amount": amount, + }, + ) + else: + BalancePlugin.getPlugin().notify( + receiver, + "Neue Transaktion", + {"type": NotifyType.ADD_FROM, "author_id": author.userid, "amount": amount}, + ) return transaction