Skip to content

Commit

Permalink
[MIG] web_notify: Migration to 18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
trisdoan committed Oct 14, 2024
1 parent 6997e59 commit 5161619
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 64 deletions.
6 changes: 6 additions & 0 deletions web_notify/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ Contributors
- Nikul Chaudhary <[email protected]>
- Tris Doan <[email protected]>

Other credits
-------------

The migration of this module from 17.0 to 18.0 was financially supported
by Camptocamp.

Maintainers
-----------

Expand Down
2 changes: 1 addition & 1 deletion web_notify/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"name": "Web Notify",
"summary": """
Send notification messages to user""",
"version": "17.0.1.0.0",
"version": "18.0.1.0.0",
"license": "AGPL-3",
"author": "ACSONE SA/NV," "AdaptiveCity," "Odoo Community Association (OCA)",
"development_status": "Production/Stable",
Expand Down
8 changes: 5 additions & 3 deletions web_notify/models/res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ def _notify_channel(
"action": action,
"params": dict(params or []),
}

notifications = [[partner, "web.notify", [bus_message]] for partner in target]
self.env["bus.bus"]._sendmany(notifications)
for partner in target:
partner._bus_send(
"web_notify",
bus_message,
)
1 change: 1 addition & 0 deletions web_notify/readme/CREDITS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The migration of this module from 17.0 to 18.0 was financially supported by Camptocamp.
10 changes: 8 additions & 2 deletions web_notify/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ <h1 class="title">Web Notify</h1>
<li><a class="reference internal" href="#credits" id="toc-entry-4">Credits</a><ul>
<li><a class="reference internal" href="#authors" id="toc-entry-5">Authors</a></li>
<li><a class="reference internal" href="#contributors" id="toc-entry-6">Contributors</a></li>
<li><a class="reference internal" href="#maintainers" id="toc-entry-7">Maintainers</a></li>
<li><a class="reference internal" href="#other-credits" id="toc-entry-7">Other credits</a></li>
<li><a class="reference internal" href="#maintainers" id="toc-entry-8">Maintainers</a></li>
</ul>
</li>
</ul>
Expand Down Expand Up @@ -483,8 +484,13 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<li>Tris Doan &lt;<a class="reference external" href="mailto:tridm&#64;trobz.com">tridm&#64;trobz.com</a>&gt;</li>
</ul>
</div>
<div class="section" id="other-credits">
<h2><a class="toc-backref" href="#toc-entry-7">Other credits</a></h2>
<p>The migration of this module from 17.0 to 18.0 was financially supported
by Camptocamp.</p>
</div>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h2>
<h2><a class="toc-backref" href="#toc-entry-8">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
Expand Down
1 change: 0 additions & 1 deletion web_notify/static/src/js/services/notification.esm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/** @odoo-module */
import {Notification} from "@web/core/notifications/notification";
import {patch} from "@web/core/utils/patch";

Expand Down
88 changes: 36 additions & 52 deletions web_notify/static/src/js/services/notification_services.esm.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,48 @@
/** @odoo-module **/

import {markup} from "@odoo/owl";
import {browser} from "@web/core/browser/browser";
import {registry} from "@web/core/registry";

export const webNotificationService = {
dependencies: ["bus_service", "notification", "action"],

start(env, {bus_service, notification, action}) {
let webNotifTimeouts = {};
/**
* Displays the web notification on user's screen
* @param {*} notifications
*/
function displaywebNotification(notifications) {
Object.values(webNotifTimeouts).forEach((notif) =>
browser.clearTimeout(notif)
start(env, {bus_service, notification: notificationService, action}) {
function displayWebNotification(notification) {
let buttons = [];
if (notification.action) {
const params = notification.action.context?.params || {};

buttons = [
{
name: params.button_name || env._t("Open"),
primary: true,
onClick: async () => {
await action.doAction(notification.action);
},
...(params.button_icon && {icon: params.button_icon}),
},
];
}

const notificationRemove = notificationService.add(
markup(notification.message),
{
title: notification.title,
type: notification.type,
sticky: notification.sticky,
className: notification.className,
buttons: buttons.map((button) => {
const onClick = button.onClick;
button.onClick = async () => {
await onClick();
notificationRemove();
};
return button;
}),
}
);
webNotifTimeouts = {};
notifications.forEach((notif) => {
browser.setTimeout(() => {
var buttons = [];
if (notif.action) {
const params =
(notif.action.context && notif.action.context.params) || {};
buttons = [
{
name: params.button_name || env._t("Open"),
primary: true,
onClick: async () => {
await action.doAction(notif.action);
},
...(params.button_icon && {icon: params.button_icon}),
},
];
}
const notificationRemove = notification.add(markup(notif.message), {
title: notif.title,
type: notif.type,
sticky: notif.sticky,
className: notif.className,
buttons: buttons.map((button) => {
const onClick = button.onClick;
button.onClick = async () => {
await onClick();
notificationRemove();
};
return button;
}),
});
});
});
}

bus_service.addEventListener("notification", ({detail: notifications}) => {
for (const {payload, type} of notifications) {
if (type === "web.notify") {
displaywebNotification(payload);
}
}
bus_service.subscribe("web_notify", (payload) => {
displayWebNotification(payload);
});
bus_service.start();
},
Expand Down
10 changes: 5 additions & 5 deletions web_notify/tests/test_res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_notify_success(self):
news = bus_bus.search(domain) - existing
self.assertEqual(1, len(news))
test_msg.update({"type": SUCCESS})
payload = json.loads(news.message)["payload"][0]
payload = json.loads(news.message)["payload"]
self.assertDictEqual(test_msg, payload)

def test_notify_danger(self):
Expand All @@ -45,7 +45,7 @@ def test_notify_danger(self):
news = bus_bus.search(domain) - existing
self.assertEqual(1, len(news))
test_msg.update({"type": DANGER})
payload = json.loads(news.message)["payload"][0]
payload = json.loads(news.message)["payload"]
self.assertDictEqual(test_msg, payload)

def test_notify_warning(self):
Expand All @@ -64,7 +64,7 @@ def test_notify_warning(self):
news = bus_bus.search(domain) - existing
self.assertEqual(1, len(news))
test_msg.update({"type": WARNING})
payload = json.loads(news.message)["payload"][0]
payload = json.loads(news.message)["payload"]
self.assertDictEqual(test_msg, payload)

def test_notify_info(self):
Expand All @@ -83,7 +83,7 @@ def test_notify_info(self):
news = bus_bus.search(domain) - existing
self.assertEqual(1, len(news))
test_msg.update({"type": INFO})
payload = json.loads(news.message)["payload"][0]
payload = json.loads(news.message)["payload"]
self.assertDictEqual(test_msg, payload)

def test_notify_default(self):
Expand All @@ -102,7 +102,7 @@ def test_notify_default(self):
news = bus_bus.search(domain) - existing
self.assertEqual(1, len(news))
test_msg.update({"type": DEFAULT})
payload = json.loads(news.message)["payload"][0]
payload = json.loads(news.message)["payload"]
self.assertDictEqual(test_msg, payload)

def test_notify_many(self):
Expand Down

0 comments on commit 5161619

Please sign in to comment.