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
5.0 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, api
  4. class ResPartner(models.Model):
  5. _inherit = 'res.partner'
  6. # Shortcut to bypass this weird thing of odoo:
  7. # `partner.user_id` is the "saleman"
  8. # while the user is stored into `user_ids`
  9. # but in the majority of the cases we have one real user per partner.
  10. @property
  11. def real_user_id(self):
  12. return self.user_ids[0] if self.user_ids else False
  13. @api.multi
  14. def _notify(self, message,
  15. force_send=False, send_after_commit=True, user_signature=True):
  16. """Override to delegate domain generation."""
  17. # notify_by_email
  18. email_domain = self._get_notify_by_email_domain(
  19. message, force_send=force_send)
  20. # `sudo` from original odoo method
  21. # the reason should be that anybody can write messages to a partner
  22. # and you really want to find all ppl to be notified
  23. partners = self.sudo().search(email_domain)
  24. super(ResPartner, partners)._notify(
  25. message, force_send=force_send,
  26. send_after_commit=send_after_commit, user_signature=user_signature)
  27. if not force_send:
  28. # notify_by_digest
  29. digest_domain = self._get_notify_by_email_domain(
  30. message, force_send=force_send, digest=True)
  31. partners = self.sudo().search(digest_domain)
  32. partners._notify_by_digest(message)
  33. # notify_by_chat
  34. self._notify_by_chat(message)
  35. return True
  36. def _digest_enabled_message_types(self):
  37. """Return a list of enabled message types for digest.
  38. In `_notify_by_digest` we check if digest mode is enabled
  39. for given message's type. Here we retrieve global settings
  40. from a config param that you can customize to second your needs.
  41. """
  42. param = self.env['ir.config_parameter'].sudo().get_param(
  43. 'mail_digest.enabled_message_types', default='')
  44. return [x.strip() for x in param.split(',') if x.strip()]
  45. @api.multi
  46. def _notify_by_digest(self, message):
  47. message_sudo = message.sudo()
  48. if message_sudo.message_type \
  49. not in self._digest_enabled_message_types():
  50. return
  51. self.env['mail.digest'].sudo().create_or_update(self, message)
  52. @api.model
  53. def _get_notify_by_email_domain(self, message,
  54. force_send=False, digest=False):
  55. """Return domain to collect partners to be notified by email.
  56. :param message: instance of mail.message
  57. :param force_send: whether the message should be sent immediately
  58. :param digest: include/exclude digest enabled partners
  59. NOTE: since mail.mail inherits from mail.message
  60. this method is called even when
  61. we create the final email for mail.digest object.
  62. Here we introduce a new context flag `notify_only_recipients`
  63. to explicitely retrieve only partners among message's recipients.
  64. """
  65. message_sudo = message.sudo()
  66. channels = message.channel_ids.filtered(
  67. lambda channel: channel.email_send)
  68. email = message_sudo.author_id \
  69. and message_sudo.author_id.email or message.email_from
  70. ids = self.ids
  71. if self.env.context.get('notify_only_recipients'):
  72. ids = [x for x in ids if x in message.partner_ids.ids]
  73. common_domain = [
  74. '|',
  75. ('id', 'in', ids),
  76. ('channel_ids', 'in', channels.ids),
  77. ('email', '!=', email),
  78. ]
  79. if force_send:
  80. return common_domain
  81. # A bit hacky but we need to exclude / include partners
  82. # that do not have any user and as such, they have no email settings.
  83. # NOTE: using the following domain does not work,
  84. # so we do 2 searches in the middle and return a domain
  85. # containing only the desired ids.
  86. #
  87. # '|', ('user_ids', '=', False),
  88. # '&', ('user_ids.digest_mode', '=', False),
  89. # ('user_ids.notification_type', '=', 'email')
  90. without_users_ids = []
  91. if not digest:
  92. # get partners w/ no users
  93. without_users_ids = self.search(
  94. common_domain + [('user_ids', '=', False)]
  95. ).ids
  96. digest_domain = [
  97. ('user_ids.digest_mode', '=', digest),
  98. ('user_ids.notification_type', '=', 'email'),
  99. ]
  100. if message.subtype_id:
  101. digest_domain.extend(
  102. self._get_domain_subtype_leaf(message.subtype_id))
  103. # get partners w/ users
  104. with_users_ids = self.search(common_domain + digest_domain).ids
  105. return [('id', 'in', without_users_ids + with_users_ids)]
  106. @api.model
  107. def _get_domain_subtype_leaf(self, subtype):
  108. return [
  109. '|',
  110. ('user_ids.disabled_notify_subtype_ids', 'not in', (subtype.id, )),
  111. ('user_ids.enabled_notify_subtype_ids', 'in', (subtype.id, )),
  112. ]