[MIG] web_notify: Migration to 15.0

This commit is contained in:
Aiendry Sarkar
2022-08-15 10:47:32 +05:30
committed by David
parent b44b23499e
commit dd92d61f30
12 changed files with 158 additions and 217 deletions

View File

@@ -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)