From dd92d61f3069550b7057e20e7c59a4435a0a41e1 Mon Sep 17 00:00:00 2001 From: Aiendry Sarkar Date: Mon, 15 Aug 2022 10:47:32 +0530 Subject: [PATCH] [MIG] web_notify: Migration to 15.0 --- web_notify/README.rst | 4 +- web_notify/__manifest__.py | 11 ++- web_notify/models/res_users.py | 69 +++++++++++++------ web_notify/static/description/index.html | 2 +- .../static/src/js/services/notification.js | 14 ++++ .../src/js/services/notification_services.js | 47 +++++++++++++ web_notify/static/src/js/web_client.js | 61 ---------------- .../static/src/js/widgets/notification.js | 26 ------- web_notify/static/src/scss/webclient.scss | 24 ------- web_notify/tests/test_res_users.py | 52 +++++--------- web_notify/views/res_users_demo.xml | 34 +++++---- web_notify/views/web_notify.xml | 31 --------- 12 files changed, 158 insertions(+), 217 deletions(-) create mode 100644 web_notify/static/src/js/services/notification.js create mode 100644 web_notify/static/src/js/services/notification_services.js delete mode 100644 web_notify/static/src/js/web_client.js delete mode 100644 web_notify/static/src/js/widgets/notification.js delete mode 100644 web_notify/static/src/scss/webclient.scss delete mode 100644 web_notify/views/web_notify.xml diff --git a/web_notify/README.rst b/web_notify/README.rst index 3f2c841f9..3e9441b0c 100644 --- a/web_notify/README.rst +++ b/web_notify/README.rst @@ -50,7 +50,7 @@ Usage ===== -To send a notification to the user you just need to call one of the new methods defined on res.users: +To send a notification to the logged in user or target audience you just need to call one of the new methods defined on res.users: .. code-block:: python @@ -85,7 +85,7 @@ or :alt: Sample notifications You can test the behaviour of the notifications by installing this module in a demo database. -Access the users form through Settings -> Users & Companies. You'll see a tab called "Test web notify", here you'll find two buttons that'll allow you test the module. +Access the users form through Settings -> Users & Companies. You'll see a tab called "Test web notify", here you'll find five buttons that'll allow you test the module. .. figure:: https://raw.githubusercontent.com/OCA/web/14.0/web_notify/static/description/test_notifications_demo.png :scale: 80 % diff --git a/web_notify/__manifest__.py b/web_notify/__manifest__.py index 4ee5bd93e..1cdd47143 100644 --- a/web_notify/__manifest__.py +++ b/web_notify/__manifest__.py @@ -6,13 +6,18 @@ "name": "Web Notify", "summary": """ Send notification messages to user""", - "version": "14.0.1.0.1", + "version": "15.0.1.0.0", "license": "AGPL-3", "author": "ACSONE SA/NV," "AdaptiveCity," "Odoo Community Association (OCA)", "development_status": "Production/Stable", "website": "https://github.com/OCA/web", - "depends": ["web", "bus", "base"], - "data": ["views/web_notify.xml"], + "depends": ["web", "bus", "base", "mail"], + "assets": { + "web.assets_backend": [ + "web_notify/static/src/js/services/notification.js", + "web_notify/static/src/js/services/notification_services.js", + ] + }, "demo": ["views/res_users_demo.xml"], "installable": True, } diff --git a/web_notify/models/res_users.py b/web_notify/models/res_users.py index d973940ea..9b1bbddec 100644 --- a/web_notify/models/res_users.py +++ b/web_notify/models/res_users.py @@ -2,8 +2,11 @@ # Copyright 2016 ACSONE SA/NV # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + from odoo import _, api, exceptions, fields, models +from odoo.addons.bus.models.bus import channel_with_db, json_dump + DEFAULT_MESSAGE = "Default message" SUCCESS = "success" @@ -19,12 +22,21 @@ class ResUsers(models.Model): @api.depends("create_date") def _compute_channel_names(self): for record in self: - res_id = record.id - record.notify_success_channel_name = "notify_success_%s" % res_id - record.notify_danger_channel_name = "notify_danger_%s" % res_id - record.notify_warning_channel_name = "notify_warning_%s" % res_id - record.notify_info_channel_name = "notify_info_%s" % res_id - record.notify_default_channel_name = "notify_default_%s" % res_id + record.notify_success_channel_name = json_dump( + channel_with_db(self.env.cr.dbname, record.partner_id) + ) + record.notify_danger_channel_name = json_dump( + channel_with_db(self.env.cr.dbname, record.partner_id) + ) + record.notify_warning_channel_name = json_dump( + channel_with_db(self.env.cr.dbname, record.partner_id) + ) + record.notify_info_channel_name = json_dump( + channel_with_db(self.env.cr.dbname, record.partner_id) + ) + record.notify_default_channel_name = json_dump( + channel_with_db(self.env.cr.dbname, record.partner_id) + ) notify_success_channel_name = fields.Char(compute="_compute_channel_names") notify_danger_channel_name = fields.Char(compute="_compute_channel_names") @@ -32,28 +44,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, target=None + ): title = title or _("Success") - self._notify_channel(SUCCESS, message, title, sticky) + self._notify_channel(SUCCESS, message, title, sticky, target) - def notify_danger(self, message="Default message", title=None, sticky=False): + def notify_danger( + self, message="Default message", title=None, sticky=False, target=None + ): title = title or _("Danger") - self._notify_channel(DANGER, message, title, sticky) + self._notify_channel(DANGER, message, title, sticky, target) - def notify_warning(self, message="Default message", title=None, sticky=False): + def notify_warning( + self, message="Default message", title=None, sticky=False, target=None + ): title = title or _("Warning") - self._notify_channel(WARNING, message, title, sticky) + self._notify_channel(WARNING, message, title, sticky, target) - def notify_info(self, message="Default message", title=None, sticky=False): + def notify_info( + self, message="Default message", title=None, sticky=False, target=None + ): title = title or _("Information") - self._notify_channel(INFO, message, title, sticky) + self._notify_channel(INFO, message, title, sticky, target) - def notify_default(self, message="Default message", title=None, sticky=False): + def notify_default( + self, message="Default message", title=None, sticky=False, target=None + ): title = title or _("Default") - self._notify_channel(DEFAULT, message, title, sticky) + self._notify_channel(DEFAULT, message, title, sticky, target) 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, + target=None, ): # pylint: disable=protected-access if not self.env.user._is_admin() and any( @@ -62,12 +89,14 @@ class ResUsers(models.Model): raise exceptions.UserError( _("Sending a notification to another user is forbidden.") ) - channel_name_field = "notify_{}_channel_name".format(type_message) + if not target: + target = self.env.user.partner_id bus_message = { "type": type_message, "message": message, "title": title, "sticky": sticky, } - notifications = [(record[channel_name_field], bus_message) for record in self] - self.env["bus.bus"].sendmany(notifications) + + notifications = [[partner, "web.notify", [bus_message]] for partner in target] + self.env["bus.bus"]._sendmany(notifications) diff --git a/web_notify/static/description/index.html b/web_notify/static/description/index.html index e10497a0f..d80b3c550 100644 --- a/web_notify/static/description/index.html +++ b/web_notify/static/description/index.html @@ -370,7 +370,7 @@ ul.auto-toc {

Production/Stable License: AGPL-3 OCA/web Translate me on Weblate Try me on Runbot

Send instant notification messages to the user in live.

This technical module allows you to send instant notification messages from the server to the user in live. -Two kinds of notification are supported.

+Five kinds of notification are supported.