You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
3.0 KiB

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