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/255/head
Simone Orsi 6 years ago
parent
commit
004e0edee3
  1. 9
      mail_digest/README.rst
  2. 2
      mail_digest/__manifest__.py
  3. 20
      mail_digest/models/res_partner.py
  4. 14
      mail_digest/tests/test_partner_domains.py

9
mail_digest/README.rst

@ -42,6 +42,15 @@ Specifically:
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.
Global settings
---------------

2
mail_digest/__manifest__.py

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

20
mail_digest/models/res_partner.py

@ -89,7 +89,8 @@ 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
@ -97,11 +98,12 @@ class ResPartner(models.Model):
partners._notify_by_email(
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)
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)
@ -127,10 +129,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
@ -155,6 +159,8 @@ class ResPartner(models.Model):
('channel_ids', 'in', channels.ids),
('email', '!=', email)
]
if force_send:
return domain
if not digest:
domain.append(('notify_email', 'not in', ('none', 'digest')))
else:

14
mail_digest/tests/test_partner_domains.py

@ -91,6 +91,20 @@ class PartnerDomainCase(SavepointCase):
domain = partner._get_notify_by_email_domain(message, digest=1)
self._assert_found(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(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