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.

98 lines
3.8 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(message)
  19. # `sudo` from original odoo method
  20. # the reason should be that anybody can write messages to a partner
  21. # and you really want to find all ppl to be notified
  22. partners = self.sudo().search(email_domain)
  23. super(ResPartner, partners)._notify(
  24. message, force_send=force_send,
  25. send_after_commit=send_after_commit,
  26. user_signature=user_signature)
  27. # notify_by_digest
  28. digest_domain = self._get_notify_by_email_domain(message, digest=True)
  29. partners = self.sudo().search(digest_domain)
  30. partners._notify_by_digest(message)
  31. return True
  32. def _digest_enabled_message_types(self):
  33. """Return a list of enabled message types for digest.
  34. In `_notify_by_digest` we check if digest mode is enabled
  35. for given message's type. Here we retrieve global settings
  36. from a config param that you can customize to second your needs.
  37. """
  38. param = self.env['ir.config_parameter'].sudo().get_param(
  39. 'mail_digest.enabled_message_types', default='')
  40. return [x.strip() for x in param.split(',') if x.strip()]
  41. @api.multi
  42. def _notify_by_digest(self, message):
  43. message_sudo = message.sudo()
  44. if message_sudo.message_type \
  45. not in self._digest_enabled_message_types():
  46. return
  47. self.env['mail.digest'].sudo().create_or_update(self, message)
  48. @api.model
  49. def _get_notify_by_email_domain(self, message, digest=False):
  50. """Return domain to collect partners to be notified by email.
  51. :param message: instance of mail.message
  52. :param digest: include/exclude digest enabled partners
  53. NOTE: since mail.mail inherits from mail.message
  54. this method is called even when
  55. we create the final email for mail.digest object.
  56. Here we introduce a new context flag `notify_only_recipients`
  57. to explicitely retrieve only partners among message's recipients.
  58. """
  59. message_sudo = message.sudo()
  60. channels = message.channel_ids.filtered(
  61. lambda channel: channel.email_send)
  62. email = message_sudo.author_id \
  63. and message_sudo.author_id.email or message.email_from
  64. ids = self.ids
  65. if self.env.context.get('notify_only_recipients'):
  66. ids = [x for x in ids if x in message.partner_ids.ids]
  67. domain = [
  68. '|',
  69. ('id', 'in', ids),
  70. ('channel_ids', 'in', channels.ids),
  71. ('email', '!=', email),
  72. ('user_ids.digest_mode', '=', digest),
  73. ('user_ids.notification_type', '=', 'email'),
  74. ]
  75. if message.subtype_id:
  76. domain.extend(self._get_domain_subtype_leaf(message.subtype_id))
  77. return domain
  78. @api.model
  79. def _get_domain_subtype_leaf(self, subtype):
  80. return [
  81. '|',
  82. ('user_ids.disabled_notify_subtype_ids', 'not in', (subtype.id, )),
  83. ('user_ids.enabled_notify_subtype_ids', 'in', (subtype.id, )),
  84. ]