From 425eb1c84932c200e72ea055f150284c43e74fd7 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Sun, 1 Nov 2020 16:16:51 +0100 Subject: [PATCH] [Plugin] balance: Added config option to add a default limit --- flaschengeist.example.toml | 2 ++ flaschengeist/controller/userController.py | 2 ++ flaschengeist/plugins/__init__.py | 1 + flaschengeist/plugins/balance/__init__.py | 10 +++++++++- flaschengeist/plugins/balance/balance_controller.py | 7 ++++--- 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/flaschengeist.example.toml b/flaschengeist.example.toml index a07cd66..e0a0cda 100644 --- a/flaschengeist.example.toml +++ b/flaschengeist.example.toml @@ -56,6 +56,8 @@ enabled = true [balance] enabled = true +# Enable a default limit, will be set if no other limit is set +# limit = -10.00 [geruecht] enabled = false diff --git a/flaschengeist/controller/userController.py b/flaschengeist/controller/userController.py index d5dd7f8..d1ff44a 100644 --- a/flaschengeist/controller/userController.py +++ b/flaschengeist/controller/userController.py @@ -1,6 +1,7 @@ from flask import current_app from werkzeug.exceptions import NotFound, BadRequest +from flaschengeist.hook import Hook from flaschengeist.models.user import User, Role from flaschengeist.database import db from flaschengeist import logger @@ -18,6 +19,7 @@ def login_user(username, password): return None +@Hook def update_user(user): current_app.config["FG_AUTH_BACKEND"].update_user(user) if not user.display_name: diff --git a/flaschengeist/plugins/__init__.py b/flaschengeist/plugins/__init__.py index 46aa1a1..bfc73fe 100644 --- a/flaschengeist/plugins/__init__.py +++ b/flaschengeist/plugins/__init__.py @@ -4,6 +4,7 @@ from werkzeug.exceptions import MethodNotAllowed from flaschengeist.hook import HookCall send_message_hook = HookCall("send_message") +update_user_hook = HookCall("update_user") class Plugin: diff --git a/flaschengeist/plugins/balance/__init__.py b/flaschengeist/plugins/balance/__init__.py index 144e799..0d29b26 100644 --- a/flaschengeist/plugins/balance/__init__.py +++ b/flaschengeist/plugins/balance/__init__.py @@ -9,10 +9,11 @@ from flask import Blueprint, jsonify, request from werkzeug.exceptions import Forbidden, BadRequest from backports.datetime_fromisoformat import MonkeyPatch -from flaschengeist.plugins import Plugin +from flaschengeist import logger from flaschengeist.models.session import Session from flaschengeist.decorator import login_required from flaschengeist.controller import userController +from flaschengeist.plugins import Plugin, update_user_hook from . import balance_controller, permissions @@ -24,6 +25,13 @@ class BalancePlugin(Plugin): def __init__(self, config): super().__init__(blueprint=balance_bp, permissions=permissions.permissions) + @update_user_hook + def set_default_limit(user): + if "limit" in config: + limit = config["limit"] + logger.debug("Setting default limit of {} to user {}".format(limit, user.userid)) + balance_controller.set_limit(user, limit, override=False) + def install(self): from flaschengeist.database import db from .models import Transaction diff --git a/flaschengeist/plugins/balance/balance_controller.py b/flaschengeist/plugins/balance/balance_controller.py index dd9112c..5d9e609 100644 --- a/flaschengeist/plugins/balance/balance_controller.py +++ b/flaschengeist/plugins/balance/balance_controller.py @@ -14,9 +14,10 @@ from . import permissions __attribute_limit = "balance_limit" -def set_limit(user: User, limit: float): - user.set_attribute(__attribute_limit, limit) - db.session.commit() +def set_limit(user: User, limit: float, override=True): + if override or not user.has_attribute(__attribute_limit): + user.set_attribute(__attribute_limit, limit) + db.session.commit() def get_limit(user: User) -> float: