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.

50 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, _, SUPERUSER_ID
  4. class ResUsers(models.Model):
  5. _inherit = 'res.users'
  6. @api.multi
  7. @api.depends('create_date')
  8. def _compute_channel_names(self):
  9. for record in self:
  10. res_id = record.id
  11. record.notify_info_channel_name = 'notify_info_%s' % res_id
  12. record.notify_warning_channel_name = 'notify_warning_%s' % res_id
  13. notify_info_channel_name = fields.Char(
  14. compute='_compute_channel_names')
  15. notify_warning_channel_name = fields.Char(
  16. compute='_compute_channel_names')
  17. @api.multi
  18. def notify_info(self, message, title=None, sticky=False):
  19. title = title or _('Information')
  20. self._notify_channel(
  21. 'notify_info_channel_name', message, title, sticky)
  22. @api.multi
  23. def notify_warning(self, message, title=None, sticky=False):
  24. title = title or _('Warning')
  25. self._notify_channel(
  26. 'notify_warning_channel_name', message, title, sticky)
  27. @api.multi
  28. def _notify_channel(self, channel_name_field, message, title, sticky):
  29. if (self.env.uid != SUPERUSER_ID
  30. and any(user.id != self.env.uid for user in self)):
  31. raise exceptions.UserError(
  32. _('Sending a notification to another user is forbidden.')
  33. )
  34. bus_message = {
  35. 'message': message,
  36. 'title': title,
  37. 'sticky': sticky
  38. }
  39. notifications = [(getattr(record, channel_name_field), bus_message)
  40. for record in self]
  41. self.env['bus.bus'].sendmany(notifications)