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.

101 lines
3.3 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (c) 2017-2019 MuK IT GmbH.
  4. #
  5. # This file is part of MuK Web Notification
  6. # (see https://mukit.at).
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ###################################################################################
  22. import logging
  23. from odoo import _, api, fields, models
  24. _logger = logging.getLogger(__name__)
  25. class NotifyWizard(models.TransientModel):
  26. _name = "muk_web_client_notification.send_notifications"
  27. def _default_user_ids(self):
  28. user_ids = self._context.get('active_model') == 'res.users' and self._context.get('active_ids') or []
  29. return [(6, 0, user_ids)]
  30. user_ids = fields.Many2many(
  31. comodel_name='res.users',
  32. relation='muk_web_client_notification_user_rel',
  33. column1='wizard_id',
  34. column2='user_id',
  35. string='Users',
  36. default=_default_user_ids,
  37. help="If no user is selected, the message is sent globally to all users.")
  38. type = fields.Selection(
  39. selection=[('info', 'Information'), ('warning', 'Warning')],
  40. string='Type',
  41. required=True,
  42. default='info')
  43. title = fields.Char(
  44. string="Title",
  45. required=True)
  46. message = fields.Text(
  47. string="Message",
  48. required=True)
  49. sticky = fields.Boolean(
  50. string="Sticky")
  51. action_id = fields.Many2one(
  52. comodel_name='ir.actions.act_window',
  53. string='Action',
  54. help="If an action is set a button to call it is added to the notification.")
  55. close = fields.Boolean(
  56. string="Close",
  57. default=True)
  58. @api.multi
  59. def send_notifications(self):
  60. for record in self:
  61. params = {
  62. 'type': record.type,
  63. 'title': record.title,
  64. 'message': record.message,
  65. 'sticky': record.sticky,
  66. }
  67. if record.action_id.exists():
  68. buttons = [{
  69. 'text': _("Action"),
  70. 'primary': True,
  71. 'action': record.action_id.id,
  72. }]
  73. if record.close:
  74. buttons.append({
  75. 'text': _("Close"),
  76. 'primary': False,
  77. 'action': None,
  78. })
  79. params.update({'buttons': buttons})
  80. record.user_ids.notify(**params)
  81. return {
  82. 'type': 'ir.actions.act_window_close'
  83. }
  84. @api.onchange('action_id')
  85. def check_change(self):
  86. if self.action_id:
  87. self.sticky = True