From 59aa6f15c1ba29c591a99f0a27b38e2e8ae022f6 Mon Sep 17 00:00:00 2001 From: Jatin Saini <67009939+jatin-47@users.noreply.github.com> Date: Mon, 2 Oct 2023 19:56:27 +0530 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Ability=20to=20ignore=20Printer=20N?= =?UTF-8?q?otifications=20based=20on=20regex=20(#4886)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Added regex filtering feature Added new regex setting, new Filtering regex setting UI and filter on the incoming messages. * Fixed a wrong variable name in the template. * Changed settings name from 'regex' to 'filter' --- AUTHORS.md | 1 + .../action_command_notification/__init__.py | 24 +++++++++++++++++-- .../static/js/action_command_notification.js | 16 ++++++++++++- ...ction_command_notification_settings.jinja2 | 11 +++++++-- 4 files changed, 47 insertions(+), 5 deletions(-) diff --git a/AUTHORS.md b/AUTHORS.md index 535abed6f..9f2d07e97 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -178,6 +178,7 @@ date of first contribution): * [Shyam Sunder](https://github.com/sgsunder) * [Conny Rasch](https://github.com/CMR-DEV) * [Christopher Perrin](https://github.com/cperrin88) + * [Jatin Saini](https://github.com/jatin-47) * [Martin Bartsch](https://github.com/kaenguruhs) * [Matthias Weis](https://github.com/mad73923) diff --git a/src/octoprint/plugins/action_command_notification/__init__.py b/src/octoprint/plugins/action_command_notification/__init__.py index 9ed83c20f..282a8ecb8 100644 --- a/src/octoprint/plugins/action_command_notification/__init__.py +++ b/src/octoprint/plugins/action_command_notification/__init__.py @@ -1,6 +1,7 @@ __license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html" __copyright__ = "Copyright (C) 2020 The OctoPrint Project - Released under terms of the AGPLv3 License" +import re import time import flask @@ -21,6 +22,7 @@ class ActionCommandNotificationPlugin( ): def __init__(self): self._notifications = [] + self._filter = "" # Additional permissions hook @@ -60,7 +62,20 @@ class ActionCommandNotificationPlugin( # ~ SettingsPlugin def get_settings_defaults(self): - return {"enable": True, "enable_popups": False} + return {"enable": True, "enable_popups": False, "filter": ""} + + def on_settings_initialized(self): + try: + self._filter = re.compile(self._settings.get(["filter"])) + except re.error: + self._filter = "" + + def on_settings_save(self, data): + octoprint.plugin.SettingsPlugin.on_settings_save(self, data) + try: + self._filter = re.compile(self._settings.get(["filter"])) + except re.error: + self._filter = "" # ~ SimpleApiPlugin @@ -90,7 +105,7 @@ class ActionCommandNotificationPlugin( { "type": "settings", "name": gettext("Printer Notifications"), - "custom_bindings": False, + "custom_bindings": True, }, { "type": "sidebar", @@ -120,6 +135,11 @@ class ActionCommandNotificationPlugin( return message = parameter.strip() + + if self._filter.search(message): + self._logger.debug(f"Notification matches filter regex: {message}") + return + self._notifications.append((time.time(), message)) self._plugin_manager.send_plugin_message(self._identifier, {"message": message}) diff --git a/src/octoprint/plugins/action_command_notification/static/js/action_command_notification.js b/src/octoprint/plugins/action_command_notification/static/js/action_command_notification.js index 1e7dd8441..8a00592db 100644 --- a/src/octoprint/plugins/action_command_notification/static/js/action_command_notification.js +++ b/src/octoprint/plugins/action_command_notification/static/js/action_command_notification.js @@ -12,6 +12,17 @@ $(function () { self._toLocalStorage(); }); + self.regexValid = function () { + try { + new RegExp( + self.settings.settings.plugins.action_command_notification.filter() + ); + return true; + } catch (e) { + return false; + } + }; + self.toDateTimeString = function (timestamp) { return formatDate(timestamp); }; @@ -106,6 +117,9 @@ $(function () { OCTOPRINT_VIEWMODELS.push({ construct: ActionCommandNotificationViewModel, dependencies: ["loginStateViewModel", "accessViewModel", "settingsViewModel"], - elements: ["#sidebar_plugin_action_command_notification_wrapper"] + elements: [ + "#sidebar_plugin_action_command_notification_wrapper", + "#settings_plugin_action_command_notification" + ] }); }); diff --git a/src/octoprint/plugins/action_command_notification/templates/action_command_notification_settings.jinja2 b/src/octoprint/plugins/action_command_notification/templates/action_command_notification_settings.jinja2 index a5d079b9b..ffa07b888 100644 --- a/src/octoprint/plugins/action_command_notification/templates/action_command_notification_settings.jinja2 +++ b/src/octoprint/plugins/action_command_notification/templates/action_command_notification_settings.jinja2 @@ -4,16 +4,23 @@