[Plugin] balance: Added config option to add a default limit

This commit is contained in:
Ferdinand Thiessen 2020-11-01 16:16:51 +01:00
parent 66d559f63b
commit 425eb1c849
5 changed files with 18 additions and 4 deletions

View File

@ -56,6 +56,8 @@ enabled = true
[balance] [balance]
enabled = true enabled = true
# Enable a default limit, will be set if no other limit is set
# limit = -10.00
[geruecht] [geruecht]
enabled = false enabled = false

View File

@ -1,6 +1,7 @@
from flask import current_app from flask import current_app
from werkzeug.exceptions import NotFound, BadRequest from werkzeug.exceptions import NotFound, BadRequest
from flaschengeist.hook import Hook
from flaschengeist.models.user import User, Role from flaschengeist.models.user import User, Role
from flaschengeist.database import db from flaschengeist.database import db
from flaschengeist import logger from flaschengeist import logger
@ -18,6 +19,7 @@ def login_user(username, password):
return None return None
@Hook
def update_user(user): def update_user(user):
current_app.config["FG_AUTH_BACKEND"].update_user(user) current_app.config["FG_AUTH_BACKEND"].update_user(user)
if not user.display_name: if not user.display_name:

View File

@ -4,6 +4,7 @@ from werkzeug.exceptions import MethodNotAllowed
from flaschengeist.hook import HookCall from flaschengeist.hook import HookCall
send_message_hook = HookCall("send_message") send_message_hook = HookCall("send_message")
update_user_hook = HookCall("update_user")
class Plugin: class Plugin:

View File

@ -9,10 +9,11 @@ from flask import Blueprint, jsonify, request
from werkzeug.exceptions import Forbidden, BadRequest from werkzeug.exceptions import Forbidden, BadRequest
from backports.datetime_fromisoformat import MonkeyPatch from backports.datetime_fromisoformat import MonkeyPatch
from flaschengeist.plugins import Plugin from flaschengeist import logger
from flaschengeist.models.session import Session from flaschengeist.models.session import Session
from flaschengeist.decorator import login_required from flaschengeist.decorator import login_required
from flaschengeist.controller import userController from flaschengeist.controller import userController
from flaschengeist.plugins import Plugin, update_user_hook
from . import balance_controller, permissions from . import balance_controller, permissions
@ -24,6 +25,13 @@ class BalancePlugin(Plugin):
def __init__(self, config): def __init__(self, config):
super().__init__(blueprint=balance_bp, permissions=permissions.permissions) 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): def install(self):
from flaschengeist.database import db from flaschengeist.database import db
from .models import Transaction from .models import Transaction

View File

@ -14,9 +14,10 @@ from . import permissions
__attribute_limit = "balance_limit" __attribute_limit = "balance_limit"
def set_limit(user: User, limit: float): def set_limit(user: User, limit: float, override=True):
user.set_attribute(__attribute_limit, limit) if override or not user.has_attribute(__attribute_limit):
db.session.commit() user.set_attribute(__attribute_limit, limit)
db.session.commit()
def get_limit(user: User) -> float: def get_limit(user: User) -> float: