From 5d1c3d5fd71960504e8ee4c201f4da04e2f5da5c Mon Sep 17 00:00:00 2001
From: Guewen Baconnier
Date: Wed, 27 Jun 2018 15:52:14 +0200
Subject: [PATCH] [IMP] web_notify: Add possibility to return an action in a
notification
---
web_notify/README.rst | 23 ++++++++++++++++++-
web_notify/models/res_users.py | 20 ++++++++++++----
web_notify/readme/USAGE.rst | 21 +++++++++++++++++
web_notify/static/description/index.html | 18 ++++++++++++++-
.../js/services/notification_services.esm.js | 23 +++++++++++++++----
web_notify/tests/test_res_users.py | 5 ++++
6 files changed, 98 insertions(+), 12 deletions(-)
diff --git a/web_notify/README.rst b/web_notify/README.rst
index 256f69bb8..f15d74a2d 100644
--- a/web_notify/README.rst
+++ b/web_notify/README.rst
@@ -7,7 +7,7 @@ Web Notify
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- !! source digest: sha256:5939f6a4cc411dab0ff9e45a43676cbcf8ecafcd7718961aee386fefc24e189d
+ !! source digest: sha256:1a956defc8dc9d2b860d2c19783b48bc95edb7578e8178737badb6bae2b29cf6
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png
@@ -83,6 +83,27 @@ or
self.env.user.notify_default(message='My default message')
+
+The notifications can bring interactivity with some buttons.
+
+* One allowing to refresh the active view
+* Another allowing to send a window / client action
+
+The reload button is activated when sending the notification with:
+
+
+The action can be used using the ``action`` keyword:
+
+.. code-block:: python
+
+ action = self.env["ir.actions.act_window"]._for_xml_id('sale.action_orders')
+ action.update({
+ 'res_id': self.id,
+ 'views': [(False, 'form')],
+ })
+ self.env.user.notify_info('My information message', action=action)
+
+
.. figure:: https://raw.githubusercontent.com/OCA/web/15.0/web_notify/static/description/notifications_screenshot.gif
:scale: 80 %
:alt: Sample notifications
diff --git a/web_notify/models/res_users.py b/web_notify/models/res_users.py
index 860b6a6ff..89486f251 100644
--- a/web_notify/models/res_users.py
+++ b/web_notify/models/res_users.py
@@ -3,6 +3,7 @@
from odoo import _, api, exceptions, fields, models
from odoo.addons.bus.models.bus import channel_with_db, json_dump
+from odoo.addons.web.controllers.main import clean_action
DEFAULT_MESSAGE = "Default message"
@@ -48,9 +49,10 @@ class ResUsers(models.Model):
sticky=False,
target=None,
html=False,
+ action=None,
):
title = title or _("Success")
- self._notify_channel(SUCCESS, message, title, sticky, target, html)
+ self._notify_channel(SUCCESS, message, title, sticky, target, html, action)
def notify_danger(
self,
@@ -59,9 +61,10 @@ class ResUsers(models.Model):
sticky=False,
target=None,
html=False,
+ action=None,
):
title = title or _("Danger")
- self._notify_channel(DANGER, message, title, sticky, target, html)
+ self._notify_channel(DANGER, message, title, sticky, target, html, action)
def notify_warning(
self,
@@ -70,9 +73,10 @@ class ResUsers(models.Model):
sticky=False,
target=None,
html=False,
+ action=None,
):
title = title or _("Warning")
- self._notify_channel(WARNING, message, title, sticky, target, html)
+ self._notify_channel(WARNING, message, title, sticky, target, html, action)
def notify_info(
self,
@@ -81,9 +85,10 @@ class ResUsers(models.Model):
sticky=False,
target=None,
html=False,
+ action=None,
):
title = title or _("Information")
- self._notify_channel(INFO, message, title, sticky, target, html)
+ self._notify_channel(INFO, message, title, sticky, target, html, action)
def notify_default(
self,
@@ -92,9 +97,10 @@ class ResUsers(models.Model):
sticky=False,
target=None,
html=False,
+ action=None,
):
title = title or _("Default")
- self._notify_channel(DEFAULT, message, title, sticky, target, html)
+ self._notify_channel(DEFAULT, message, title, sticky, target, html, action)
def _notify_channel(
self,
@@ -104,6 +110,7 @@ class ResUsers(models.Model):
sticky=False,
target=None,
html=False,
+ action=None,
):
if not (self.env.user._is_admin() or self.env.su) and any(
user.id != self.env.uid for user in self
@@ -113,12 +120,15 @@ class ResUsers(models.Model):
)
if not target:
target = self.env.user.partner_id
+ if action:
+ action = clean_action(action, self.env)
bus_message = {
"type": type_message,
"message": message,
"title": title,
"sticky": sticky,
"html": html,
+ "action": action,
}
notifications = [[partner, "web.notify", [bus_message]] for partner in target]
diff --git a/web_notify/readme/USAGE.rst b/web_notify/readme/USAGE.rst
index f31a5341f..c86b1ad3a 100644
--- a/web_notify/readme/USAGE.rst
+++ b/web_notify/readme/USAGE.rst
@@ -29,6 +29,27 @@ or
self.env.user.notify_default(message='My default message')
+
+The notifications can bring interactivity with some buttons.
+
+* One allowing to refresh the active view
+* Another allowing to send a window / client action
+
+The reload button is activated when sending the notification with:
+
+
+The action can be used using the ``action`` keyword:
+
+.. code-block:: python
+
+ action = self.env["ir.actions.act_window"]._for_xml_id('sale.action_orders')
+ action.update({
+ 'res_id': self.id,
+ 'views': [(False, 'form')],
+ })
+ self.env.user.notify_info('My information message', action=action)
+
+
.. figure:: static/description/notifications_screenshot.gif
:scale: 80 %
:alt: Sample notifications
diff --git a/web_notify/static/description/index.html b/web_notify/static/description/index.html
index 4199d1995..13f1d7e13 100644
--- a/web_notify/static/description/index.html
+++ b/web_notify/static/description/index.html
@@ -1,3 +1,4 @@
+
@@ -366,7 +367,7 @@ ul.auto-toc {
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-!! source digest: sha256:5939f6a4cc411dab0ff9e45a43676cbcf8ecafcd7718961aee386fefc24e189d
+!! source digest: sha256:1a956defc8dc9d2b860d2c19783b48bc95edb7578e8178737badb6bae2b29cf6
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->

Send instant notification messages to the user in live.
@@ -419,6 +420,21 @@ Two kinds of notification are supported.
self.env.user.notify_default(message='My default message')
+The notifications can bring interactivity with some buttons.
+
+- One allowing to refresh the active view
+- Another allowing to send a window / client action
+
+The reload button is activated when sending the notification with:
+The action can be used using the action keyword:
+
+ action = self.env["ir.actions.act_window"]._for_xml_id('sale.action_orders')
+ action.update({
+ 'res_id': self.id,
+ 'views': [(False, 'form')],
+ })
+self.env.user.notify_info('My information message', action=action)
+
diff --git a/web_notify/static/src/js/services/notification_services.esm.js b/web_notify/static/src/js/services/notification_services.esm.js
index c6454abff..259b1e4fc 100644
--- a/web_notify/static/src/js/services/notification_services.esm.js
+++ b/web_notify/static/src/js/services/notification_services.esm.js
@@ -1,30 +1,43 @@
/** @odoo-module **/
+import {Markup} from "web.utils";
import {browser} from "@web/core/browser/browser";
import {registry} from "@web/core/registry";
export const webNotificationService = {
- dependencies: ["notification"],
+ dependencies: ["notification", "action"],
- start(env, {notification}) {
+ start(env, {notification, action}) {
let webNotifTimeouts = {};
/**
* Displays the web notification on user's screen
*/
-
function displaywebNotification(notifications) {
Object.values(webNotifTimeouts).forEach((notif) =>
browser.clearTimeout(notif)
);
webNotifTimeouts = {};
-
notifications.forEach(function (notif) {
browser.setTimeout(function () {
- notification.add(notif.message, {
+ let buttons = [];
+
+ if (notif.action) {
+ buttons = [
+ {
+ name: env._t("Open"),
+ primary: true,
+ onClick: async () => {
+ await action.doAction(notif.action);
+ },
+ },
+ ];
+ }
+ notification.add(Markup(notif.message), {
title: notif.title,
type: notif.type,
sticky: notif.sticky,
className: notif.className,
messageIsHtml: notif.html,
+ buttons: buttons,
});
});
});
diff --git a/web_notify/tests/test_res_users.py b/web_notify/tests/test_res_users.py
index 4d922ee31..4d65973da 100644
--- a/web_notify/tests/test_res_users.py
+++ b/web_notify/tests/test_res_users.py
@@ -19,6 +19,7 @@ class TestResUsers(common.TransactionCase):
"title": "title",
"sticky": True,
"html": False,
+ "action": None,
}
self.env.user.notify_success(**test_msg)
news = bus_bus.search(domain) - existing
@@ -36,6 +37,7 @@ class TestResUsers(common.TransactionCase):
"title": "title",
"sticky": True,
"html": False,
+ "action": None,
}
self.env.user.notify_danger(**test_msg)
news = bus_bus.search(domain) - existing
@@ -53,6 +55,7 @@ class TestResUsers(common.TransactionCase):
"title": "title",
"sticky": True,
"html": False,
+ "action": None,
}
self.env.user.notify_warning(**test_msg)
news = bus_bus.search(domain) - existing
@@ -70,6 +73,7 @@ class TestResUsers(common.TransactionCase):
"title": "title",
"sticky": True,
"html": False,
+ "action": None,
}
self.env.user.notify_info(**test_msg)
news = bus_bus.search(domain) - existing
@@ -87,6 +91,7 @@ class TestResUsers(common.TransactionCase):
"title": "title",
"sticky": True,
"html": False,
+ "action": None,
}
self.env.user.notify_default(**test_msg)
news = bus_bus.search(domain) - existing