From 2a5e51457e17eba177d81abe550c531a6e68a786 Mon Sep 17 00:00:00 2001 From: SilvioC2C Date: Fri, 16 Feb 2024 12:58:43 +0100 Subject: [PATCH] [IMP] web_notify: allow passing custom parameters to notifications --- web_notify/models/res_users.py | 38 +++++++++++++++++++++--------- web_notify/tests/test_res_users.py | 35 +++++++++++++++++++++++---- 2 files changed, 57 insertions(+), 16 deletions(-) diff --git a/web_notify/models/res_users.py b/web_notify/models/res_users.py index 7e5660a78..ed464ec1e 100644 --- a/web_notify/models/res_users.py +++ b/web_notify/models/res_users.py @@ -32,28 +32,43 @@ class ResUsers(models.Model): notify_info_channel_name = fields.Char(compute="_compute_channel_names") notify_default_channel_name = fields.Char(compute="_compute_channel_names") - def notify_success(self, message="Default message", title=None, sticky=False): + def notify_success( + self, message="Default message", title=None, sticky=False, params=None + ): title = title or _("Success") - self._notify_channel(SUCCESS, message, title, sticky) + self._notify_channel(SUCCESS, message, title, sticky, params) - def notify_danger(self, message="Default message", title=None, sticky=False): + def notify_danger( + self, message="Default message", title=None, sticky=False, params=None + ): title = title or _("Danger") - self._notify_channel(DANGER, message, title, sticky) + self._notify_channel(DANGER, message, title, sticky, params) - def notify_warning(self, message="Default message", title=None, sticky=False): + def notify_warning( + self, message="Default message", title=None, sticky=False, params=None + ): title = title or _("Warning") - self._notify_channel(WARNING, message, title, sticky) + self._notify_channel(WARNING, message, title, sticky, params) - def notify_info(self, message="Default message", title=None, sticky=False): + def notify_info( + self, message="Default message", title=None, sticky=False, params=None + ): title = title or _("Information") - self._notify_channel(INFO, message, title, sticky) + self._notify_channel(INFO, message, title, sticky, params) - def notify_default(self, message="Default message", title=None, sticky=False): + def notify_default( + self, message="Default message", title=None, sticky=False, params=None + ): title = title or _("Default") - self._notify_channel(DEFAULT, message, title, sticky) + self._notify_channel(DEFAULT, message, title, sticky, params) def _notify_channel( - self, type_message=DEFAULT, message=DEFAULT_MESSAGE, title=None, sticky=False + self, + type_message=DEFAULT, + message=DEFAULT_MESSAGE, + title=None, + sticky=False, + params=None, ): # pylint: disable=protected-access if not (self.env.user._is_admin() or self.env.su) and any( @@ -68,6 +83,7 @@ class ResUsers(models.Model): "message": message, "title": title, "sticky": sticky, + "params": dict(params or []), } notifications = [(record[channel_name_field], bus_message) for record in self] self.env["bus.bus"].sendmany(notifications) diff --git a/web_notify/tests/test_res_users.py b/web_notify/tests/test_res_users.py index ba11546ee..06f777c78 100644 --- a/web_notify/tests/test_res_users.py +++ b/web_notify/tests/test_res_users.py @@ -19,7 +19,12 @@ class TestResUsers(common.TransactionCase): ("channel", "=", json_dump(self.env.user.notify_success_channel_name)) ] existing = bus_bus.search(domain) - test_msg = {"message": "message", "title": "title", "sticky": True} + test_msg = { + "message": "message", + "title": "title", + "sticky": True, + "params": {}, + } self.env.user.notify_success(**test_msg) news = bus_bus.search(domain) - existing self.assertEqual(1, len(news)) @@ -30,7 +35,12 @@ class TestResUsers(common.TransactionCase): bus_bus = self.env["bus.bus"] domain = [("channel", "=", json_dump(self.env.user.notify_danger_channel_name))] existing = bus_bus.search(domain) - test_msg = {"message": "message", "title": "title", "sticky": True} + test_msg = { + "message": "message", + "title": "title", + "sticky": True, + "params": {}, + } self.env.user.notify_danger(**test_msg) news = bus_bus.search(domain) - existing self.assertEqual(1, len(news)) @@ -43,7 +53,12 @@ class TestResUsers(common.TransactionCase): ("channel", "=", json_dump(self.env.user.notify_warning_channel_name)) ] existing = bus_bus.search(domain) - test_msg = {"message": "message", "title": "title", "sticky": True} + test_msg = { + "message": "message", + "title": "title", + "sticky": True, + "params": {}, + } self.env.user.notify_warning(**test_msg) news = bus_bus.search(domain) - existing self.assertEqual(1, len(news)) @@ -54,7 +69,12 @@ class TestResUsers(common.TransactionCase): bus_bus = self.env["bus.bus"] domain = [("channel", "=", json_dump(self.env.user.notify_info_channel_name))] existing = bus_bus.search(domain) - test_msg = {"message": "message", "title": "title", "sticky": True} + test_msg = { + "message": "message", + "title": "title", + "sticky": True, + "params": {}, + } self.env.user.notify_info(**test_msg) news = bus_bus.search(domain) - existing self.assertEqual(1, len(news)) @@ -67,7 +87,12 @@ class TestResUsers(common.TransactionCase): ("channel", "=", json_dump(self.env.user.notify_default_channel_name)) ] existing = bus_bus.search(domain) - test_msg = {"message": "message", "title": "title", "sticky": True} + test_msg = { + "message": "message", + "title": "title", + "sticky": True, + "params": {}, + } self.env.user.notify_default(**test_msg) news = bus_bus.search(domain) - existing self.assertEqual(1, len(news))