|
| 1 | +# pylint: disable=missing-docstring |
1 | 2 | # Copyright 2016 ACSONE SA/NV
|
2 | 3 | # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
3 | 4 |
|
4 |
| -from odoo import api, exceptions, fields, models, _ |
| 5 | +from odoo import _, api, exceptions, fields, models |
| 6 | + |
| 7 | +DEFAULT_MESSAGE = "Default message" |
| 8 | + |
| 9 | +SUCCESS = "success" |
| 10 | +DANGER = "danger" |
| 11 | +WARNING = "warning" |
| 12 | +INFO = "info" |
| 13 | +DEFAULT = "default" |
5 | 14 |
|
6 | 15 |
|
7 | 16 | class ResUsers(models.Model):
|
8 |
| - _inherit = 'res.users' |
| 17 | + _inherit = "res.users" |
9 | 18 |
|
10 |
| - @api.depends('create_date') |
| 19 | + @api.depends("create_date") |
11 | 20 | def _compute_channel_names(self):
|
12 | 21 | for record in self:
|
13 | 22 | res_id = record.id
|
14 |
| - record.notify_info_channel_name = 'notify_info_%s' % res_id |
15 |
| - record.notify_warning_channel_name = 'notify_warning_%s' % res_id |
| 23 | + record.notify_success_channel_name = "notify_success_%s" % res_id |
| 24 | + record.notify_danger_channel_name = "notify_danger_%s" % res_id |
| 25 | + record.notify_warning_channel_name = "notify_warning_%s" % res_id |
| 26 | + record.notify_info_channel_name = "notify_info_%s" % res_id |
| 27 | + record.notify_default_channel_name = "notify_default_%s" % res_id |
| 28 | + |
| 29 | + notify_success_channel_name = fields.Char(compute="_compute_channel_names") |
| 30 | + notify_danger_channel_name = fields.Char(compute="_compute_channel_names") |
| 31 | + notify_warning_channel_name = fields.Char(compute="_compute_channel_names") |
| 32 | + notify_info_channel_name = fields.Char(compute="_compute_channel_names") |
| 33 | + notify_default_channel_name = fields.Char(compute="_compute_channel_names") |
| 34 | + |
| 35 | + def notify_success( |
| 36 | + self, message="Default message", title=None, sticky=False |
| 37 | + ): |
| 38 | + title = title or _("Success") |
| 39 | + self._notify_channel(SUCCESS, message, title, sticky) |
16 | 40 |
|
17 |
| - notify_info_channel_name = fields.Char( |
18 |
| - compute='_compute_channel_names') |
19 |
| - notify_warning_channel_name = fields.Char( |
20 |
| - compute='_compute_channel_names') |
| 41 | + def notify_danger( |
| 42 | + self, message="Default message", title=None, sticky=False |
| 43 | + ): |
| 44 | + title = title or _("Danger") |
| 45 | + self._notify_channel(DANGER, message, title, sticky) |
| 46 | + |
| 47 | + def notify_warning( |
| 48 | + self, message="Default message", title=None, sticky=False |
| 49 | + ): |
| 50 | + title = title or _("Warning") |
| 51 | + self._notify_channel(WARNING, message, title, sticky) |
21 | 52 |
|
22 | 53 | def notify_info(self, message="Default message", title=None, sticky=False):
|
23 |
| - title = title or _('Information') |
24 |
| - self._notify_channel( |
25 |
| - 'notify_info_channel_name', message, title, sticky) |
26 |
| - |
27 |
| - def notify_warning(self, message="Default message", |
28 |
| - title=None, sticky=False): |
29 |
| - title = title or _('Warning') |
30 |
| - self._notify_channel( |
31 |
| - 'notify_warning_channel_name', message, title, sticky) |
32 |
| - |
33 |
| - def _notify_channel(self, channel_name_field, message, title, sticky): |
34 |
| - if (not self.env.user._is_admin() |
35 |
| - and any(user.id != self.env.uid for user in self)): |
| 54 | + title = title or _("Information") |
| 55 | + self._notify_channel(INFO, message, title, sticky) |
| 56 | + |
| 57 | + def notify_default( |
| 58 | + self, message="Default message", title=None, sticky=False |
| 59 | + ): |
| 60 | + title = title or _("Default") |
| 61 | + self._notify_channel(DEFAULT, message, title, sticky) |
| 62 | + |
| 63 | + def _notify_channel( |
| 64 | + self, |
| 65 | + type_message=DEFAULT, |
| 66 | + message=DEFAULT_MESSAGE, |
| 67 | + title=None, |
| 68 | + sticky=False, |
| 69 | + ): |
| 70 | + # pylint: disable=protected-access |
| 71 | + if not self.env.user._is_admin() and any( |
| 72 | + user.id != self.env.uid for user in self |
| 73 | + ): |
36 | 74 | raise exceptions.UserError(
|
37 |
| - _('Sending a notification to another user is forbidden.') |
| 75 | + _("Sending a notification to another user is forbidden.") |
38 | 76 | )
|
| 77 | + channel_name_field = "notify_{}_channel_name".format(type_message) |
39 | 78 | bus_message = {
|
40 |
| - 'message': message, |
41 |
| - 'title': title, |
42 |
| - 'sticky': sticky |
| 79 | + "type": type_message, |
| 80 | + "message": message, |
| 81 | + "title": title, |
| 82 | + "sticky": sticky, |
43 | 83 | }
|
44 |
| - notifications = [(record[channel_name_field], bus_message) |
45 |
| - for record in self] |
46 |
| - self.env['bus.bus'].sendmany(notifications) |
| 84 | + notifications = [ |
| 85 | + (record[channel_name_field], bus_message) for record in self |
| 86 | + ] |
| 87 | + self.env["bus.bus"].sendmany(notifications) |
0 commit comments