Browse Source

mail_digest: fix behavior w/ `force_send` enabled

`force_send` is an option in `mail` core module
that allows to send an email immediately.

Handling the message w/ digest is wrong and in any case
we gonna have a digest w/out the message inside it
since is going to be deleted right after send.

A typical use case is the notification sent to new followers.
pull/348/head
Simone Orsi 7 years ago
parent
commit
7fa2845e61
  1. 9
      mail_digest/README.rst
  2. 2
      mail_digest/__manifest__.py
  3. 25
      mail_digest/models/res_partner.py
  4. 14
      mail_digest/tests/test_partner_domains.py

9
mail_digest/README.rst

@ -64,6 +64,15 @@ a message with subtype assigned **will NOT be sent** via digest if:
NOTE: under the hood the digest notification logic excludes followers to be notified,
since you really want to notify only mail.digest's partner.
NOTE 2: Odoo's mail machinery has an option `force_send`
to send the email immediately without waiting for the mail queue to be processed.
When this option is used the email is sent right away
and the message record is deleted right after.
A typical use case is the reset password mail.
We assume that if you use that option you really want the email to go out "now"
so when `force_send` is used, digest machinery is completely bypassed.
Digest rendering preview
------------------------

2
mail_digest/__manifest__.py

@ -4,7 +4,7 @@
{
'name': 'Mail digest',
'summary': """Basic digest mail handling.""",
'version': '11.0.1.1.0',
'version': '11.0.1.1.2',
'license': 'AGPL-3',
'author': 'Camptocamp, Odoo Community Association (OCA)',
'website': 'https://github.com/OCA/social',

25
mail_digest/models/res_partner.py

@ -20,19 +20,24 @@ class ResPartner(models.Model):
force_send=False, send_after_commit=True, user_signature=True):
"""Override to delegate domain generation."""
# notify_by_email
email_domain = self._get_notify_by_email_domain(message)
email_domain = self._get_notify_by_email_domain(
message, force_send=force_send)
# `sudo` from original odoo method
# the reason should be that anybody can write messages to a partner
# and you really want to find all ppl to be notified
partners = self.sudo().search(email_domain)
super(ResPartner, partners)._notify(
message, force_send=force_send,
send_after_commit=send_after_commit,
user_signature=user_signature)
# notify_by_digest
digest_domain = self._get_notify_by_email_domain(message, digest=True)
partners = self.sudo().search(digest_domain)
partners._notify_by_digest(message)
send_after_commit=send_after_commit, user_signature=user_signature)
if not force_send:
# notify_by_digest
digest_domain = self._get_notify_by_email_domain(
message, force_send=force_send, digest=True)
partners = self.sudo().search(digest_domain)
partners._notify_by_digest(message)
# notify_by_chat
self._notify_by_chat(message)
return True
def _digest_enabled_message_types(self):
@ -55,10 +60,12 @@ class ResPartner(models.Model):
self.env['mail.digest'].sudo().create_or_update(self, message)
@api.model
def _get_notify_by_email_domain(self, message, digest=False):
def _get_notify_by_email_domain(self, message,
force_send=False, digest=False):
"""Return domain to collect partners to be notified by email.
:param message: instance of mail.message
:param force_send: whether the message should be sent immediately
:param digest: include/exclude digest enabled partners
NOTE: since mail.mail inherits from mail.message
@ -83,6 +90,8 @@ class ResPartner(models.Model):
('channel_ids', 'in', channels.ids),
('email', '!=', email),
]
if force_send:
return common_domain
# A bit hacky but we need to exclude / include partners
# that do not have any user and as such, they have no email settings.
# NOTE: using the following domain does not work,

14
mail_digest/tests/test_partner_domains.py

@ -110,6 +110,20 @@ class PartnerDomainCase(SavepointCase):
domain = partner._get_notify_by_email_domain(message, digest=True)
self._assert_found(partner, domain)
def test_notify_domains_digest_force_send(self):
# when `force_send` is true, digest machinery is bypassed
message = self.message_model.create({'body': 'My Body', })
partner = self.partner1
partner.notify_email = 'digest'
# even if we have digest mode on, we find the guy
domain = partner._get_notify_by_email_domain(message, force_send=True)
self._assert_found(partner, domain)
# when asking for digest domain we don't get digest-related leaves
# as digest domain part is bypassed
domain = partner._get_notify_by_email_domain(
message, force_send=True, digest=True)
self.assertNotIn('notify_email', [x[0] for x in domain])
def test_notify_domains_none(self):
message = self.message_model.create({'body': 'My Body', })
partner = self.partner1

Loading…
Cancel
Save