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.

127 lines
4.5 KiB

  1. # Copyright 2017-2018 Camptocamp - Simone Orsi
  2. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  3. from odoo import models, fields, api
  4. class Users(models.Model):
  5. _name = 'res.users'
  6. _inherit = ['res.users']
  7. def __init__(self, pool, cr):
  8. """ Override of __init__ to add access rights.
  9. Access rights are disabled by default, but allowed
  10. on some specific fields defined in self.SELF_{READ/WRITE}ABLE_FIELDS.
  11. [copied from mail.models.users]
  12. """
  13. super(Users, self).__init__(pool, cr)
  14. new_fields = [
  15. 'digest_mode',
  16. 'digest_frequency',
  17. 'notify_conf_ids',
  18. ]
  19. # duplicate list to avoid modifying the original reference
  20. type(self).SELF_WRITEABLE_FIELDS = list(self.SELF_WRITEABLE_FIELDS)
  21. type(self).SELF_WRITEABLE_FIELDS.extend(new_fields)
  22. # duplicate list to avoid modifying the original reference
  23. type(self).SELF_READABLE_FIELDS = list(self.SELF_READABLE_FIELDS)
  24. type(self).SELF_READABLE_FIELDS.extend(new_fields)
  25. digest_mode = fields.Boolean(
  26. default=False,
  27. help='If enabled, email notifications will be sent in digest mode.'
  28. )
  29. digest_frequency = fields.Selection(
  30. string='Frequency',
  31. selection=[
  32. ('daily', 'Daily'),
  33. ('weekly', 'Weekly')
  34. ],
  35. default='weekly',
  36. required=True,
  37. )
  38. notify_conf_ids = fields.One2many(
  39. string='Notifications',
  40. inverse_name='user_id',
  41. comodel_name='user.notification.conf',
  42. )
  43. enabled_notify_subtype_ids = fields.Many2many(
  44. string='User enabled subtypes',
  45. comodel_name='mail.message.subtype',
  46. compute='_compute_notify_subtype_ids',
  47. search='_search_enabled_notify_subtype_ids',
  48. )
  49. disabled_notify_subtype_ids = fields.Many2many(
  50. string='User disabled subtypes',
  51. comodel_name='mail.message.subtype',
  52. compute='_compute_notify_subtype_ids',
  53. search='_search_disabled_notify_subtype_ids',
  54. )
  55. def _notify_subtypes_by_state(self, enabled):
  56. self.ensure_one()
  57. return self.notify_conf_ids.filtered(
  58. lambda x: x.enabled == enabled).mapped('subtype_id')
  59. @api.multi
  60. @api.depends('notify_conf_ids.subtype_id', 'notify_conf_ids.enabled')
  61. def _compute_notify_subtype_ids(self):
  62. for rec in self:
  63. rec.enabled_notify_subtype_ids = \
  64. rec._notify_subtypes_by_state(True)
  65. rec.disabled_notify_subtype_ids = \
  66. rec._notify_subtypes_by_state(False)
  67. def _search_notify_subtype_ids_domain(self, operator, value, enabled):
  68. """Build domain to search notification subtypes by user conf."""
  69. if operator in ('in', 'not in') and \
  70. not isinstance(value, (tuple, list)):
  71. value = [value, ]
  72. conf_value = value
  73. if isinstance(conf_value, int):
  74. # we search conf records always w/ 'in'
  75. conf_value = [conf_value]
  76. _value = self.env['user.notification.conf'].search([
  77. ('subtype_id', 'in', conf_value),
  78. ('enabled', '=', enabled),
  79. ]).mapped('user_id').ids
  80. return [('id', operator, _value)]
  81. def _search_enabled_notify_subtype_ids(self, operator, value):
  82. return self._search_notify_subtype_ids_domain(operator, value, True)
  83. def _search_disabled_notify_subtype_ids(self, operator, value):
  84. return self._search_notify_subtype_ids_domain(operator, value, False)
  85. def _notify_update_subtype(self, subtype, enable):
  86. """Update notification settings by subtype.
  87. :param subtype: `mail.message.subtype` to enable or disable
  88. :param enable: boolean to enable or disable given subtype
  89. """
  90. self.ensure_one()
  91. exists = self.env['user.notification.conf'].search([
  92. ('subtype_id', '=', subtype.id),
  93. ('user_id', '=', self.id)
  94. ], limit=1)
  95. if exists:
  96. exists.enabled = enable
  97. else:
  98. self.write({
  99. 'notify_conf_ids': [
  100. (0, 0, {'enabled': enable, 'subtype_id': subtype.id})]
  101. })
  102. @api.multi
  103. def _notify_enable_subtype(self, subtype):
  104. """Enable given subtype."""
  105. for rec in self:
  106. rec._notify_update_subtype(subtype, True)
  107. @api.multi
  108. def _notify_disable_subtype(self, subtype):
  109. """Disable given subtype."""
  110. for rec in self:
  111. rec._notify_update_subtype(subtype, False)