mirror of
https://github.com/OCA/web.git
synced 2025-02-22 13:21:25 +02:00
[12.0] web_notify: improve popup UI (#1231)
* [ADD]: all available bootstrap notifications (success/danger/warning/info/default) * [IMP] use black color for text for default notification. * [FIX] reverted require string for `bus.Longpolling` and rename `on_message_received` to `on_message` to prevent collisions.
This commit is contained in:
committed by
David
parent
af2656ba00
commit
3db7072296
@@ -1,3 +1,4 @@
|
||||
# pylint: disable=missing-docstring
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||
|
||||
from . import res_users
|
||||
|
||||
@@ -1,46 +1,87 @@
|
||||
# pylint: disable=missing-docstring
|
||||
# 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 _, api, exceptions, fields, models
|
||||
|
||||
DEFAULT_MESSAGE = "Default message"
|
||||
|
||||
SUCCESS = "success"
|
||||
DANGER = "danger"
|
||||
WARNING = "warning"
|
||||
INFO = "info"
|
||||
DEFAULT = "default"
|
||||
|
||||
|
||||
class ResUsers(models.Model):
|
||||
_inherit = 'res.users'
|
||||
_inherit = "res.users"
|
||||
|
||||
@api.depends('create_date')
|
||||
@api.depends("create_date")
|
||||
def _compute_channel_names(self):
|
||||
for record in self:
|
||||
res_id = record.id
|
||||
record.notify_info_channel_name = 'notify_info_%s' % res_id
|
||||
record.notify_warning_channel_name = 'notify_warning_%s' % res_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_info_channel_name = fields.Char(
|
||||
compute='_compute_channel_names')
|
||||
notify_warning_channel_name = fields.Char(
|
||||
compute='_compute_channel_names')
|
||||
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)
|
||||
|
||||
def notify_danger(
|
||||
self, message="Default message", title=None, sticky=False
|
||||
):
|
||||
title = title or _("Danger")
|
||||
self._notify_channel(DANGER, message, title, sticky)
|
||||
|
||||
def notify_warning(
|
||||
self, message="Default message", title=None, sticky=False
|
||||
):
|
||||
title = title or _("Warning")
|
||||
self._notify_channel(WARNING, message, title, sticky)
|
||||
|
||||
def notify_info(self, message="Default message", title=None, sticky=False):
|
||||
title = title or _('Information')
|
||||
self._notify_channel(
|
||||
'notify_info_channel_name', message, title, sticky)
|
||||
title = title or _("Information")
|
||||
self._notify_channel(INFO, message, title, sticky)
|
||||
|
||||
def notify_warning(self, message="Default message",
|
||||
title=None, sticky=False):
|
||||
title = title or _('Warning')
|
||||
self._notify_channel(
|
||||
'notify_warning_channel_name', 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, channel_name_field, message, title, sticky):
|
||||
if (not self.env.user._is_admin()
|
||||
and any(user.id != self.env.uid for user in self)):
|
||||
def _notify_channel(
|
||||
self,
|
||||
type_message=DEFAULT,
|
||||
message=DEFAULT_MESSAGE,
|
||||
title=None,
|
||||
sticky=False,
|
||||
):
|
||||
# pylint: disable=protected-access
|
||||
if not self.env.user._is_admin() and any(
|
||||
user.id != self.env.uid for user in self
|
||||
):
|
||||
raise exceptions.UserError(
|
||||
_('Sending a notification to another user is forbidden.')
|
||||
_("Sending a notification to another user is forbidden.")
|
||||
)
|
||||
channel_name_field = "notify_{}_channel_name".format(type_message)
|
||||
bus_message = {
|
||||
'message': message,
|
||||
'title': title,
|
||||
'sticky': sticky
|
||||
"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 = [
|
||||
(record[channel_name_field], bus_message) for record in self
|
||||
]
|
||||
self.env["bus.bus"].sendmany(notifications)
|
||||
|
||||
Reference in New Issue
Block a user