From 4cf6fb65f0719c83eb485b381736abec7541e035 Mon Sep 17 00:00:00 2001 From: Simone Orsi Date: Wed, 4 Apr 2018 17:54:05 +0200 Subject: [PATCH] mail_digest: fix notification domain for partners w/ no user --- mail_digest/__manifest__.py | 2 +- mail_digest/models/res_partner.py | 26 ++++++++++++++++++++--- mail_digest/tests/test_partner_domains.py | 15 +++++++++++-- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/mail_digest/__manifest__.py b/mail_digest/__manifest__.py index 07682265..0c0fcb8c 100644 --- a/mail_digest/__manifest__.py +++ b/mail_digest/__manifest__.py @@ -4,7 +4,7 @@ { 'name': 'Mail digest', 'summary': """Basic digest mail handling.""", - 'version': '11.0.1.0.1', + 'version': '11.0.1.0.2', 'license': 'AGPL-3', 'author': 'Camptocamp, Odoo Community Association (OCA)', 'website': 'https://github.com/OCA/social', diff --git a/mail_digest/models/res_partner.py b/mail_digest/models/res_partner.py index 7fa6bd9f..aa6e3fe5 100644 --- a/mail_digest/models/res_partner.py +++ b/mail_digest/models/res_partner.py @@ -77,17 +77,37 @@ class ResPartner(models.Model): ids = self.ids if self.env.context.get('notify_only_recipients'): ids = [x for x in ids if x in message.partner_ids.ids] - domain = [ + common_domain = [ '|', ('id', 'in', ids), ('channel_ids', 'in', channels.ids), ('email', '!=', email), + ] + # 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, + # so we do 2 searches in the middle and return a domain + # containing only the desired ids. + # + # '|', ('user_ids', '=', False), + # '&', ('user_ids.digest_mode', '=', False), + # ('user_ids.notification_type', '=', 'email') + without_users_ids = [] + if not digest: + # get partners w/ no users + without_users_ids = self.search( + common_domain + [('user_ids', '=', False)] + ).ids + digest_domain = [ ('user_ids.digest_mode', '=', digest), ('user_ids.notification_type', '=', 'email'), ] if message.subtype_id: - domain.extend(self._get_domain_subtype_leaf(message.subtype_id)) - return domain + digest_domain.extend( + self._get_domain_subtype_leaf(message.subtype_id)) + # get partners w/ users + with_users_ids = self.search(common_domain + digest_domain).ids + return [('id', 'in', without_users_ids + with_users_ids)] @api.model def _get_domain_subtype_leaf(self, subtype): diff --git a/mail_digest/tests/test_partner_domains.py b/mail_digest/tests/test_partner_domains.py index bdbe9c0e..05e25d12 100644 --- a/mail_digest/tests/test_partner_domains.py +++ b/mail_digest/tests/test_partner_domains.py @@ -34,6 +34,13 @@ class PartnerDomainCase(SavepointCase): cls.partner1 = cls.user1.partner_id cls.partner2 = cls.user2.partner_id cls.partner3 = cls.user3.partner_id + # a partner w/ no user that should not be excluded + # in non-digest notifications + cls.partner_nouser = cls.partner_model.with_context( + tracking_disable=True).create({ + 'name': 'No User Partner', + 'email': 'nouser@test.com', + }) cls.subtype1 = cls.subtype_model.create({'name': 'Type 1'}) cls.subtype2 = cls.subtype_model.create({'name': 'Type 2'}) @@ -60,7 +67,7 @@ class PartnerDomainCase(SavepointCase): # because we call `_get_notify_by_email_domain` directly self.partner1.real_user_id.notification_type = 'email' self.partner2.real_user_id.notification_type = 'email' - partners = self.partner1 + self.partner2 + partners = self.partner1 + self.partner2 + self.partner_nouser # followers self.partner3.message_subscribe(self.partner2.ids) # partner1 is the only recipient @@ -68,16 +75,20 @@ class PartnerDomainCase(SavepointCase): 'body': 'My Body', 'res_id': self.partner3.id, 'model': 'res.partner', - 'partner_ids': [(4, self.partner1.id)] + 'partner_ids': [(4, self.partner1.id), (4, self.partner_nouser.id)] }) domain = partners._get_notify_by_email_domain(message) # we find both of them since partner2 is a follower self._assert_found(self.partner1, domain) self._assert_found(self.partner2, domain) + # and we find also the partner w/ no user + self._assert_found(self.partner_nouser, domain) + # no one here in digest mode domain = partners._get_notify_by_email_domain(message, digest=True) self._assert_found(self.partner1, domain, not_found=True) self._assert_found(self.partner2, domain, not_found=True) + self._assert_found(self.partner_nouser, domain, not_found=True) # include only recipients domain = partners.with_context(