[System][Plugin] Added plugin settings

This commit is contained in:
Ferdinand Thiessen 2021-03-14 12:56:04 +01:00
parent 5826ec7a00
commit 1b29e602f1
4 changed files with 53 additions and 3 deletions

View File

@ -40,7 +40,9 @@ def __load_plugins(app):
logger.debug("Found plugin: >{}<".format(entry_point.name)) logger.debug("Found plugin: >{}<".format(entry_point.name))
plugin = None plugin = None
if entry_point.name in config and config[entry_point.name].get("enabled", False): if entry_point.name in config and config[entry_point.name].get("enabled", False):
plugin = entry_point.load()(config[entry_point.name]) plugin = entry_point.load()
setattr(plugin, "_plugin_name", entry_point.name)
plugin = plugin(config[entry_point.name])
if plugin.blueprint: if plugin.blueprint:
app.register_blueprint(plugin.blueprint) app.register_blueprint(plugin.blueprint)
logger.info("Load plugin >{}<".format(entry_point.name)) logger.info("Load plugin >{}<".format(entry_point.name))

View File

@ -0,0 +1,9 @@
from ..database import db
class _PluginSetting(db.Model):
__tablename__ = "plugin_setting"
id = db.Column("id", db.Integer, primary_key=True)
plugin: str = db.Column(db.String(30))
name: str = db.Column(db.String(30), nullable=False)
value: any = db.Column(db.PickleType(protocol=4))

View File

@ -1,7 +1,10 @@
import sqlalchemy
import pkg_resources import pkg_resources
from werkzeug.exceptions import MethodNotAllowed, NotFound from werkzeug.exceptions import MethodNotAllowed, NotFound
from flaschengeist.database import db
from flaschengeist.models.user import _Avatar from flaschengeist.models.user import _Avatar
from flaschengeist.models.setting import _PluginSetting
from flaschengeist.utils.hook import HookBefore, HookAfter from flaschengeist.utils.hook import HookBefore, HookAfter
before_role_updated = HookBefore("update_role") before_role_updated = HookBefore("update_role")
@ -50,6 +53,40 @@ class Plugin:
""" """
pass pass
def get_setting(self, name: str, **kwargs):
"""Get plugin setting from database
Args:
name: string identifying the setting
default: Default value
Returns:
Value stored in database (native python)
"""
try:
setting = _PluginSetting.query\
.filter(_PluginSetting.plugin == self._plugin_name)\
.filter(_PluginSetting.name == name).one()
return setting.value
except sqlalchemy.orm.exc.NoResultFound:
if "default" in kwargs:
return kwargs["default"]
else:
raise KeyError
def set_setting(self, name: str, value):
"""Save setting in database
Args:
name: String identifying the setting
value: Value to be stored
"""
setting = _PluginSetting.query \
.filter(_PluginSetting.plugin == self._plugin_name) \
.filter(_PluginSetting.name == name).one_or_none()
if setting is not None:
setting.value = value
else:
db.session.add(_PluginSetting(plugin=self._plugin_name, name=name, value=value))
db.session.commit()
def serialize(self): def serialize(self):
"""Serialize a plugin into a dict """Serialize a plugin into a dict

View File

@ -30,10 +30,12 @@ class BalancePlugin(Plugin):
@before_update_user @before_update_user
def set_default_limit(user): def set_default_limit(user):
if "limit" in config: try:
limit = config["limit"] limit = self.get_setting("limit")
logger.debug("Setting default limit of {} to user {}".format(limit, user.userid)) logger.debug("Setting default limit of {} to user {}".format(limit, user.userid))
balance_controller.set_limit(user, limit, override=False) balance_controller.set_limit(user, limit, override=False)
except KeyError:
pass
def install(self): def install(self):
from flaschengeist.database import db from flaschengeist.database import db