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.

51 lines
1.7 KiB

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