From 90999bbefbb447e57628a20b90f74822485fd605 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Sun, 13 Feb 2022 14:31:55 +0100 Subject: [PATCH] chore(core): Seperated logic from the plugin code, reduces imports --- flaschengeist/controller/pluginController.py | 76 ++++++++++++++++++++ flaschengeist/plugins/__init__.py | 49 ++++--------- 2 files changed, 89 insertions(+), 36 deletions(-) create mode 100644 flaschengeist/controller/pluginController.py diff --git a/flaschengeist/controller/pluginController.py b/flaschengeist/controller/pluginController.py new file mode 100644 index 0000000..7dbe678 --- /dev/null +++ b/flaschengeist/controller/pluginController.py @@ -0,0 +1,76 @@ +"""Controller for Plugin logic + +Used by plugins for setting and notification functionality. +""" + +import sqlalchemy +from ..database import db +from ..models.setting import _PluginSetting +from ..models.notification import Notification + + +def get_setting(plugin_id: str, name: str, **kwargs): + """Get plugin setting from database + + Args: + plugin_id: ID of the plugin + name: string identifying the setting + default: Default value + Returns: + Value stored in database (native python) + Raises: + `KeyError` if no such setting exists in the database + """ + try: + setting = ( + _PluginSetting.query.filter(_PluginSetting.plugin == plugin_id).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(plugin_id: str, name: str, value): + """Save setting in database + + Args: + plugin_id: ID of the plugin + name: String identifying the setting + value: Value to be stored + """ + setting = ( + _PluginSetting.query.filter(_PluginSetting.plugin == plugin_id) + .filter(_PluginSetting.name == name) + .one_or_none() + ) + if setting is not None: + if value is None: + db.session.delete(setting) + else: + setting.value = value + else: + db.session.add(_PluginSetting(plugin=plugin_id, name=name, value=value)) + db.session.commit() + + +def notify(plugin_id: str, user, text: str, data=None): + """Create a new notification for an user + + Args: + plugin_id: ID of the plugin + user: `flaschengeist.models.user.User` to notify + text: Visibile notification text + data: Optional data passed to the notificaton + Returns: + ID of the created `flaschengeist.models.notification.Notification` + + Hint: use the data for frontend actions. + """ + if not user.deleted: + n = Notification(text=text, data=data, plugin=plugin_id, user_=user) + db.session.add(n) + db.session.commit() + return n.id diff --git a/flaschengeist/plugins/__init__.py b/flaschengeist/plugins/__init__.py index bcd96b3..2db90c5 100644 --- a/flaschengeist/plugins/__init__.py +++ b/flaschengeist/plugins/__init__.py @@ -1,13 +1,7 @@ -import sqlalchemy import pkg_resources from werkzeug.datastructures import FileStorage from werkzeug.exceptions import MethodNotAllowed, NotFound -from flaschengeist.controller import imageController - -from flaschengeist.database import db -from flaschengeist.models.notification import Notification from flaschengeist.models.user import _Avatar, User -from flaschengeist.models.setting import _PluginSetting from flaschengeist.utils.hook import HookBefore, HookAfter plugins_installed = HookAfter("plugins.installed") @@ -95,18 +89,9 @@ class Plugin: Raises: `KeyError` if no such setting exists in the database """ - try: - setting = ( - _PluginSetting.query.filter(_PluginSetting.plugin == self.name) - .filter(_PluginSetting.name == name) - .one() - ) - return setting.value - except sqlalchemy.orm.exc.NoResultFound: - if "default" in kwargs: - return kwargs["default"] - else: - raise KeyError + from ..controller import pluginController + + return pluginController.get_setting(self.id) def set_setting(self, name: str, value): """Save setting in database @@ -115,19 +100,9 @@ class Plugin: name: String identifying the setting value: Value to be stored """ - setting = ( - _PluginSetting.query.filter(_PluginSetting.plugin == self.name) - .filter(_PluginSetting.name == name) - .one_or_none() - ) - if setting is not None: - if value is None: - db.session.delete(setting) - else: - setting.value = value - else: - db.session.add(_PluginSetting(plugin=self.name, name=name, value=value)) - db.session.commit() + from ..controller import pluginController + + return pluginController.set_setting(self.id, name, value) def notify(self, user, text: str, data=None): """Create a new notification for an user @@ -141,11 +116,9 @@ class Plugin: Hint: use the data for frontend actions. """ - if not user.deleted: - n = Notification(text=text, data=data, plugin=self.id, user_=user) - db.session.add(n) - db.session.commit() - return n.id + from ..controller import pluginController + + return pluginController.notify(self.id, user, text, data) def serialize(self): """Serialize a plugin into a dict @@ -245,6 +218,10 @@ class AuthPlugin(Plugin): MethodNotAllowed: If not supported by Backend Any valid HTTP exception """ + # By default save the image to the avatar, + # deleting would happen by unsetting it + from ..controller import imageController + user.avatar_ = imageController.upload_image(file) def delete_avatar(self, user: User):