Skip to content

Commit

Permalink
[MIG] web_notify: Migration to 15.0
Browse files Browse the repository at this point in the history
Porting notes:
1. Removed notify_default, since it does not pass validation in [1]
2. Modifications to notificaation widgets are removed in prefer of original widget implementation
3. Odoo improved notification usage in [2], so no need to add extra channels that depend on user's id

[1] https://github.com/odoo/odoo/blob/21be7ca0c9eef18184d06755ff83337be1e4752e/addons/web/static/src/core/notifications/notification.js#L49
[2] odoo/odoo@543af27
  • Loading branch information
em230418 committed Mar 31, 2022
1 parent 51e7710 commit c7557d2
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 178 deletions.
9 changes: 7 additions & 2 deletions web_notify/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
"data": [],
"demo": ["views/res_users_demo.xml"],
"assets": {
"web.assets_backend": [
"web_notify/static/src/js/services/web_notification_service.esm.js",
],
},
"installable": True,
}
33 changes: 7 additions & 26 deletions web_notify/models/res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,19 @@
# 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 import _, exceptions, models

DEFAULT_MESSAGE = "Default message"

SUCCESS = "success"
DANGER = "danger"
WARNING = "warning"
INFO = "info"
DEFAULT = "default"


class ResUsers(models.Model):
_inherit = "res.users"

@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

notify_success_channel_name = fields.Char(compute="_compute_channel_names")
notify_danger_channel_name = fields.Char(compute="_compute_channel_names")
notify_warning_channel_name = fields.Char(compute="_compute_channel_names")
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):
title = title or _("Success")
self._notify_channel(SUCCESS, message, title, sticky)
Expand All @@ -48,12 +31,8 @@ def notify_info(self, message="Default message", title=None, sticky=False):
title = title or _("Information")
self._notify_channel(INFO, message, title, sticky)

def notify_default(self, message="Default message", title=None, sticky=False):
title = title or _("Default")
self._notify_channel(DEFAULT, message, title, sticky)

def _notify_channel(
self, type_message=DEFAULT, message=DEFAULT_MESSAGE, title=None, sticky=False
self, type_message, message=DEFAULT_MESSAGE, title=None, sticky=False
):
# pylint: disable=protected-access
if not self.env.user._is_admin() and any(
Expand All @@ -62,12 +41,14 @@ def _notify_channel(
raise exceptions.UserError(
_("Sending a notification to another user is forbidden.")
)
channel_name_field = "notify_{}_channel_name".format(type_message)
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 = [
(self.partner_id, "web_notify_" + type_message, bus_message)
for record in self
]
self.env["bus.bus"]._sendmany(notifications)
47 changes: 47 additions & 0 deletions web_notify/static/src/js/services/web_notification_service.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/** @odoo-module **/

import {registry} from "@web/core/registry";

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

start(env, {notification}) {
const channel_success = "web_notify_success";
const channel_danger = "web_notify_danger";
const channel_warning = "web_notify_warning";
const channel_info = "web_notify_info";
const channel_default = "web_notify_default";
const all_channels = [
channel_success,
channel_danger,
channel_warning,
channel_info,
channel_default,
];

function onMessage(message) {
return notification.add(message.message, {
type: message.type,
title: message.title,
sticky: message.sticky,
});
}

function onNotification(notifications) {
for (const {type, payload} of notifications) {
if (all_channels.indexOf(type) > -1) {
onMessage(payload);
}
}
}

env.bus.on("WEB_CLIENT_READY", null, async () => {
const legacyEnv = owl.Component.env;

legacyEnv.services.bus_service.startPolling();
legacyEnv.services.bus_service.onNotification(this, onNotification);
});
},
};

registry.category("services").add("webNotification", webNotificationService);
61 changes: 0 additions & 61 deletions web_notify/static/src/js/web_client.js

This file was deleted.

26 changes: 0 additions & 26 deletions web_notify/static/src/js/widgets/notification.js

This file was deleted.

24 changes: 0 additions & 24 deletions web_notify/static/src/scss/webclient.scss

This file was deleted.

8 changes: 0 additions & 8 deletions web_notify/views/res_users_demo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@
class="oe_highlight"
/>
</group>
<group>
<button
name="notify_default"
type="object"
string="Test default notification"
class="oe_highlight"
/>
</group>
</group>
</page>
</xpath>
Expand Down
31 changes: 0 additions & 31 deletions web_notify/views/web_notify.xml

This file was deleted.

0 comments on commit c7557d2

Please sign in to comment.