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.

65 lines
2.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 ACSONE SA/NV
  3. # Copyright 2018 Camptocamp
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  5. from odoo import api, exceptions, fields, models, _, SUPERUSER_ID
  6. from odoo.addons.web.controllers.main import clean_action
  7. class ResUsers(models.Model):
  8. _inherit = 'res.users'
  9. @api.multi
  10. @api.depends('create_date')
  11. def _compute_channel_names(self):
  12. for record in self:
  13. 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
  16. notify_info_channel_name = fields.Char(
  17. compute='_compute_channel_names')
  18. notify_warning_channel_name = fields.Char(
  19. compute='_compute_channel_names')
  20. @api.multi
  21. def notify_info(self, message, title=None, sticky=False,
  22. show_reload=False, action=None,
  23. action_link_name=None, **options):
  24. title = title or _('Information')
  25. self._notify_channel(
  26. 'notify_info_channel_name', message, title,
  27. sticky=sticky, show_reload=show_reload, action=action,
  28. action_link_name=action_link_name, **options
  29. )
  30. @api.multi
  31. def notify_warning(self, message, title=None, sticky=False,
  32. show_reload=False, action=None,
  33. action_link_name=None, **options):
  34. title = title or _('Warning')
  35. self._notify_channel(
  36. 'notify_warning_channel_name', message, title,
  37. sticky=sticky, show_reload=show_reload, action=action,
  38. action_link_name=action_link_name, **options
  39. )
  40. @api.multi
  41. def _notify_channel(self, channel_name_field, message, title, **options):
  42. if (self.env.uid != SUPERUSER_ID
  43. and any(user.id != self.env.uid for user in self)):
  44. raise exceptions.UserError(
  45. _('Sending a notification to another user is forbidden.')
  46. )
  47. if options.get('action'):
  48. options['action'] = clean_action(options['action'])
  49. bus_message = {
  50. 'message': message,
  51. 'title': title,
  52. }
  53. bus_message.update(options)
  54. notifications = [(getattr(record, channel_name_field), bus_message)
  55. for record in self]
  56. self.env['bus.bus'].sendmany(notifications)