add get notifications of plugin

This commit is contained in:
Tim Gröger 2023-05-01 21:53:48 +02:00
parent 47442fe211
commit f38fb334f1
3 changed files with 29 additions and 6 deletions

View File

@ -3,7 +3,7 @@
Used by plugins for setting and notification functionality. Used by plugins for setting and notification functionality.
""" """
from typing import Union from typing import Union, List
from flask import current_app from flask import current_app
from werkzeug.exceptions import NotFound, BadRequest from werkzeug.exceptions import NotFound, BadRequest
from sqlalchemy.exc import OperationalError, ProgrammingError from sqlalchemy.exc import OperationalError, ProgrammingError
@ -56,7 +56,7 @@ def get_enabled_plugins() -> list[Plugin]:
return enabled_plugins 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 """Create a new notification for an user
Args: Args:
@ -70,12 +70,23 @@ def notify(plugin_id: str, user, text: str, data=None):
Hint: use the data for frontend actions. Hint: use the data for frontend actions.
""" """
if not user.deleted: 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.add(n)
db.session.commit() db.session.commit()
return n.id 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") @Hook("plugins.installed")
def install_plugin(plugin_name: str): def install_plugin(plugin_name: str):
logger.debug(f"Installing plugin {plugin_name}") logger.debug(f"Installing plugin {plugin_name}")

View File

@ -21,7 +21,8 @@ class Notification(db.Model, ModelSerializeMixin):
plugin_: Plugin = db.relationship( plugin_: Plugin = db.relationship(
"Plugin", backref=db.backref("notifications_", cascade="all, delete, delete-orphan") "Plugin", backref=db.backref("notifications_", cascade="all, delete, delete-orphan")
) )
plugin: str
@property @property
def plugin(self): def plugin(self) -> str:
return self.plugin_.name return self.plugin_.name

View File

@ -4,7 +4,7 @@
""" """
from typing import Union from typing import Union, List
from importlib.metadata import entry_points from importlib.metadata import entry_points
from werkzeug.exceptions import NotFound from werkzeug.exceptions import NotFound
from werkzeug.datastructures import FileStorage from werkzeug.datastructures import FileStorage
@ -142,7 +142,18 @@ class Plugin(BasePlugin):
""" """
from ..controller import pluginController 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): def serialize(self):
"""Serialize a plugin into a dict """Serialize a plugin into a dict