Files
allwatch/base/basemodule.py

41 lines
1.2 KiB
Python

from flask import jsonify, render_template, Blueprint, app, Response
from abc import abstractmethod, ABC
from dataclasses import dataclass
from .notificationmanager import Notification, NOTIFICATION_MANAGER
@dataclass
class SyncResult:
success: bool
msg: str
class BaseModule(ABC):
def __init__(self, app: App, name: str, faIcon: str, blueprint: Blueprint):
self.name: str = name
self.faIcon = faIcon
self.blueprint: Blueprint = blueprint
self.blueprint.add_url_rule("/page", view_func=self.get_page)
self.blueprint.add_url_rule("/sync", view_func=self.sync_wrapper, methods=["POST"])
app.register_blueprint(self.blueprint, url_prefix=f"/modules/{name}")
def sync_wrapper(self):
result = self.sync()
NOTIFICATION_MANAGER.notify(Notification(
module=self.name,
status=Notification.Status.INFO if result.success else Notification.Status.ERROR,
msg=result.msg
));
return jsonify({"success":result.success, "msg":result.msg}), 200
@abstractmethod
def get_page(self) -> Response:
# return flask response with modules html fragment
pass
@abstractmethod
def sync(self) -> SyncResult:
pass