chore(core): Seperated logic from the plugin code, reduces imports
This commit is contained in:
parent
fb50ed05be
commit
90999bbefb
|
@ -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
|
|
@ -1,13 +1,7 @@
|
||||||
import sqlalchemy
|
|
||||||
import pkg_resources
|
import pkg_resources
|
||||||
from werkzeug.datastructures import FileStorage
|
from werkzeug.datastructures import FileStorage
|
||||||
from werkzeug.exceptions import MethodNotAllowed, NotFound
|
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.user import _Avatar, User
|
||||||
from flaschengeist.models.setting import _PluginSetting
|
|
||||||
from flaschengeist.utils.hook import HookBefore, HookAfter
|
from flaschengeist.utils.hook import HookBefore, HookAfter
|
||||||
|
|
||||||
plugins_installed = HookAfter("plugins.installed")
|
plugins_installed = HookAfter("plugins.installed")
|
||||||
|
@ -95,18 +89,9 @@ class Plugin:
|
||||||
Raises:
|
Raises:
|
||||||
`KeyError` if no such setting exists in the database
|
`KeyError` if no such setting exists in the database
|
||||||
"""
|
"""
|
||||||
try:
|
from ..controller import pluginController
|
||||||
setting = (
|
|
||||||
_PluginSetting.query.filter(_PluginSetting.plugin == self.name)
|
return pluginController.get_setting(self.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(self, name: str, value):
|
def set_setting(self, name: str, value):
|
||||||
"""Save setting in database
|
"""Save setting in database
|
||||||
|
@ -115,19 +100,9 @@ class Plugin:
|
||||||
name: String identifying the setting
|
name: String identifying the setting
|
||||||
value: Value to be stored
|
value: Value to be stored
|
||||||
"""
|
"""
|
||||||
setting = (
|
from ..controller import pluginController
|
||||||
_PluginSetting.query.filter(_PluginSetting.plugin == self.name)
|
|
||||||
.filter(_PluginSetting.name == name)
|
return pluginController.set_setting(self.id, name, value)
|
||||||
.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()
|
|
||||||
|
|
||||||
def notify(self, user, text: str, data=None):
|
def notify(self, user, text: str, data=None):
|
||||||
"""Create a new notification for an user
|
"""Create a new notification for an user
|
||||||
|
@ -141,11 +116,9 @@ class Plugin:
|
||||||
|
|
||||||
Hint: use the data for frontend actions.
|
Hint: use the data for frontend actions.
|
||||||
"""
|
"""
|
||||||
if not user.deleted:
|
from ..controller import pluginController
|
||||||
n = Notification(text=text, data=data, plugin=self.id, user_=user)
|
|
||||||
db.session.add(n)
|
return pluginController.notify(self.id, user, text, data)
|
||||||
db.session.commit()
|
|
||||||
return n.id
|
|
||||||
|
|
||||||
def serialize(self):
|
def serialize(self):
|
||||||
"""Serialize a plugin into a dict
|
"""Serialize a plugin into a dict
|
||||||
|
@ -245,6 +218,10 @@ class AuthPlugin(Plugin):
|
||||||
MethodNotAllowed: If not supported by Backend
|
MethodNotAllowed: If not supported by Backend
|
||||||
Any valid HTTP exception
|
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)
|
user.avatar_ = imageController.upload_image(file)
|
||||||
|
|
||||||
def delete_avatar(self, user: User):
|
def delete_avatar(self, user: User):
|
||||||
|
|
Loading…
Reference in New Issue