From 1b29e602f1be219bd6d2b56ccfc631b7884bd2dd Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Sun, 14 Mar 2021 12:56:04 +0100 Subject: [PATCH] [System][Plugin] Added plugin settings --- flaschengeist/app.py | 4 ++- flaschengeist/models/setting.py | 9 ++++++ flaschengeist/plugins/__init__.py | 37 +++++++++++++++++++++++ flaschengeist/plugins/balance/__init__.py | 6 ++-- 4 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 flaschengeist/models/setting.py diff --git a/flaschengeist/app.py b/flaschengeist/app.py index c460e99..4042601 100644 --- a/flaschengeist/app.py +++ b/flaschengeist/app.py @@ -40,7 +40,9 @@ def __load_plugins(app): logger.debug("Found plugin: >{}<".format(entry_point.name)) plugin = None 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: app.register_blueprint(plugin.blueprint) logger.info("Load plugin >{}<".format(entry_point.name)) diff --git a/flaschengeist/models/setting.py b/flaschengeist/models/setting.py new file mode 100644 index 0000000..8b3acc4 --- /dev/null +++ b/flaschengeist/models/setting.py @@ -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)) diff --git a/flaschengeist/plugins/__init__.py b/flaschengeist/plugins/__init__.py index 41a019e..d4249a9 100644 --- a/flaschengeist/plugins/__init__.py +++ b/flaschengeist/plugins/__init__.py @@ -1,7 +1,10 @@ +import sqlalchemy import pkg_resources from werkzeug.exceptions import MethodNotAllowed, NotFound +from flaschengeist.database import db from flaschengeist.models.user import _Avatar +from flaschengeist.models.setting import _PluginSetting from flaschengeist.utils.hook import HookBefore, HookAfter before_role_updated = HookBefore("update_role") @@ -50,6 +53,40 @@ class Plugin: """ 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): """Serialize a plugin into a dict diff --git a/flaschengeist/plugins/balance/__init__.py b/flaschengeist/plugins/balance/__init__.py index 3f458c7..edaa2a4 100644 --- a/flaschengeist/plugins/balance/__init__.py +++ b/flaschengeist/plugins/balance/__init__.py @@ -30,10 +30,12 @@ class BalancePlugin(Plugin): @before_update_user def set_default_limit(user): - if "limit" in config: - limit = config["limit"] + try: + limit = self.get_setting("limit") logger.debug("Setting default limit of {} to user {}".format(limit, user.userid)) balance_controller.set_limit(user, limit, override=False) + except KeyError: + pass def install(self): from flaschengeist.database import db