From f38fb334f19e50818fa1228d12cf992a56e25b0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Mon, 1 May 2023 21:53:48 +0200 Subject: [PATCH] add get notifications of plugin --- flaschengeist/controller/pluginController.py | 17 ++++++++++++++--- flaschengeist/models/notification.py | 3 ++- flaschengeist/plugins/__init__.py | 15 +++++++++++++-- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/flaschengeist/controller/pluginController.py b/flaschengeist/controller/pluginController.py index 0c0b22f..486ffea 100644 --- a/flaschengeist/controller/pluginController.py +++ b/flaschengeist/controller/pluginController.py @@ -3,7 +3,7 @@ Used by plugins for setting and notification functionality. """ -from typing import Union +from typing import Union, List from flask import current_app from werkzeug.exceptions import NotFound, BadRequest from sqlalchemy.exc import OperationalError, ProgrammingError @@ -56,7 +56,7 @@ def get_enabled_plugins() -> list[Plugin]: return enabled_plugins -def notify(plugin_id: str, user, text: str, data=None): +def notify(plugin_id: int, user, text: str, data=None): """Create a new notification for an user Args: @@ -70,12 +70,23 @@ def notify(plugin_id: str, user, text: str, data=None): Hint: use the data for frontend actions. """ if not user.deleted: - n = Notification(text=text, data=data, plugin=plugin_id, user_=user) + n = Notification(text=text, data=data, plugin_id_=plugin_id, user_=user) db.session.add(n) db.session.commit() return n.id +def get_notifications(plugin_id) -> List[Notification]: + """Get all notifications for a plugin + + Args: + plugin_id: ID of the plugin + Returns: + List of `flaschengeist.models.notification.Notification` + """ + return db.session.execute(db.select(Notification).where(Notification.plugin_id_ == plugin_id)).scalars().all() + + @Hook("plugins.installed") def install_plugin(plugin_name: str): logger.debug(f"Installing plugin {plugin_name}") diff --git a/flaschengeist/models/notification.py b/flaschengeist/models/notification.py index 33b1d8d..c126a84 100644 --- a/flaschengeist/models/notification.py +++ b/flaschengeist/models/notification.py @@ -21,7 +21,8 @@ class Notification(db.Model, ModelSerializeMixin): plugin_: Plugin = db.relationship( "Plugin", backref=db.backref("notifications_", cascade="all, delete, delete-orphan") ) + plugin: str @property - def plugin(self): + def plugin(self) -> str: return self.plugin_.name diff --git a/flaschengeist/plugins/__init__.py b/flaschengeist/plugins/__init__.py index dadcb85..c9c5cd1 100644 --- a/flaschengeist/plugins/__init__.py +++ b/flaschengeist/plugins/__init__.py @@ -4,7 +4,7 @@ """ -from typing import Union +from typing import Union, List from importlib.metadata import entry_points from werkzeug.exceptions import NotFound from werkzeug.datastructures import FileStorage @@ -142,7 +142,18 @@ class Plugin(BasePlugin): """ from ..controller import pluginController - return pluginController.notify(self.name, user, text, data) + return pluginController.notify(self.id, user, text, data) + + @property + def notifications(self) -> List["Notification"]: + """Get all notifications for this plugin + + Returns: + List of `flaschengeist.models.notification.Notification` + """ + from ..controller import pluginController + + return pluginController.get_notifications(self.id) def serialize(self): """Serialize a plugin into a dict