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.

46 lines
1.7 KiB

  1. # Copyright 2016 ACSONE SA/NV
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import api, exceptions, fields, models, _
  4. class ResUsers(models.Model):
  5. _inherit = 'res.users'
  6. @api.depends('create_date')
  7. def _compute_channel_names(self):
  8. for record in self:
  9. res_id = record.id
  10. record.notify_info_channel_name = 'notify_info_%s' % res_id
  11. record.notify_warning_channel_name = 'notify_warning_%s' % res_id
  12. notify_info_channel_name = fields.Char(
  13. compute='_compute_channel_names')
  14. notify_warning_channel_name = fields.Char(
  15. compute='_compute_channel_names')
  16. def notify_info(self, message="Default message", title=None, sticky=False):
  17. title = title or _('Information')
  18. self._notify_channel(
  19. 'notify_info_channel_name', message, title, sticky)
  20. def notify_warning(self, message="Default message",
  21. title=None, sticky=False):
  22. title = title or _('Warning')
  23. self._notify_channel(
  24. 'notify_warning_channel_name', message, title, sticky)
  25. def _notify_channel(self, channel_name_field, message, title, sticky):
  26. if (not self.env.user._is_admin()
  27. and any(user.id != self.env.uid for user in self)):
  28. raise exceptions.UserError(
  29. _('Sending a notification to another user is forbidden.')
  30. )
  31. bus_message = {
  32. 'message': message,
  33. 'title': title,
  34. 'sticky': sticky
  35. }
  36. notifications = [(record[channel_name_field], bus_message)
  37. for record in self]
  38. self.env['bus.bus'].sendmany(notifications)