From 9a71d7c4f22fcdc1e770b5965d886f8a3b80d45f Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (ACSONE)" Date: Fri, 6 Dec 2019 11:49:27 +0100 Subject: [PATCH] [MIG] mail_optional_follower_notification: Migration to 13.0 --- .../__init__.py | 2 - .../__manifest__.py | 2 +- .../models/__init__.py | 5 +- .../models/mail_message.py | 43 ------- .../models/mail_thread.py | 25 ++++ .../models/res_partner.py | 32 ----- .../tests/__init__.py | 2 - ...st_mail_optional_follower_notifications.py | 119 +++++++++++------- .../wizard/mail_compose_message.py | 9 +- .../wizard/mail_compose_message_view.xml | 10 +- 10 files changed, 109 insertions(+), 140 deletions(-) delete mode 100644 mail_optional_follower_notification/models/mail_message.py create mode 100644 mail_optional_follower_notification/models/mail_thread.py delete mode 100644 mail_optional_follower_notification/models/res_partner.py diff --git a/mail_optional_follower_notification/__init__.py b/mail_optional_follower_notification/__init__.py index 93aa2c1..9b42961 100644 --- a/mail_optional_follower_notification/__init__.py +++ b/mail_optional_follower_notification/__init__.py @@ -1,4 +1,2 @@ -# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). - from . import models from . import wizard diff --git a/mail_optional_follower_notification/__manifest__.py b/mail_optional_follower_notification/__manifest__.py index 6c4769b..f55cd4b 100644 --- a/mail_optional_follower_notification/__manifest__.py +++ b/mail_optional_follower_notification/__manifest__.py @@ -6,7 +6,7 @@ "author": "ACSONE SA/NV," "Odoo Community Association (OCA)", "website": "https://github.com/OCA/social", "category": "Social Network", - "version": "12.0.1.0.0", + "version": "13.0.1.0.0", "license": "AGPL-3", "depends": ["mail"], "data": ["wizard/mail_compose_message_view.xml"], diff --git a/mail_optional_follower_notification/models/__init__.py b/mail_optional_follower_notification/models/__init__.py index b3d15f4..b70a9f2 100644 --- a/mail_optional_follower_notification/models/__init__.py +++ b/mail_optional_follower_notification/models/__init__.py @@ -1,4 +1 @@ -# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). - -from . import mail_message -from . import res_partner +from . import mail_thread diff --git a/mail_optional_follower_notification/models/mail_message.py b/mail_optional_follower_notification/models/mail_message.py deleted file mode 100644 index 4eeda7b..0000000 --- a/mail_optional_follower_notification/models/mail_message.py +++ /dev/null @@ -1,43 +0,0 @@ -# Copyright 2016 ACSONE SA/NV () -# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). - -from odoo import api, models - - -class MailMessage(models.Model): - _inherit = "mail.message" - - @api.model - def create(self, values): - ctx = self.env.context.copy() - if not ctx.get("notify_followers") and values.get("partner_ids"): - partner_list = self.resolve_2many_commands( - "partner_ids", values.get("partner_ids"), fields=["id"] - ) - ctx["force_partners_to_notify"] = [d["id"] for d in partner_list] - return super(MailMessage, self.with_context(ctx)).create(values) - - @api.multi - def _notify( - self, - record, - msg_vals, - force_send=False, - send_after_commit=True, - model_description=False, - mail_auto_delete=True, - ): - res = super()._notify( - record, - msg_vals, - force_send=force_send, - send_after_commit=send_after_commit, - model_description=model_description, - mail_auto_delete=mail_auto_delete, - ) - if self.env.context.get("force_partners_to_notify"): - # Needaction only for recipients - self.needaction_partner_ids = [ - (6, 0, self.env.context.get("force_partners_to_notify")) - ] - return res diff --git a/mail_optional_follower_notification/models/mail_thread.py b/mail_optional_follower_notification/models/mail_thread.py new file mode 100644 index 0000000..db9dd9a --- /dev/null +++ b/mail_optional_follower_notification/models/mail_thread.py @@ -0,0 +1,25 @@ +# Copyright 2019 ACSONE SA/NV () +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + + +class MailThread(models.AbstractModel): + _inherit = "mail.thread" + + def _notify_compute_recipients(self, message, msg_vals): + """ Compute recipients to notify based on subtype and followers. This + method returns data structured as expected for ``_notify_recipients``. """ + recipient_data = super()._notify_compute_recipients(message, msg_vals) + if not self.env.context.get("notify_followers", False): + # filter out all the followers + pids = ( + msg_vals.get("partner_ids", []) + if msg_vals + else message.sudo().partner_ids.ids + ) + recipient_data = { + "partners": [d for d in recipient_data["partners"] if d["id"] in pids], + "channels": [], + } + return recipient_data diff --git a/mail_optional_follower_notification/models/res_partner.py b/mail_optional_follower_notification/models/res_partner.py deleted file mode 100644 index f91c52b..0000000 --- a/mail_optional_follower_notification/models/res_partner.py +++ /dev/null @@ -1,32 +0,0 @@ -# Copyright 2016 ACSONE SA/NV () -# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). - -from odoo import api, models - - -class ResPartner(models.Model): - _inherit = "res.partner" - - @api.model - def _notify( - self, - message, - rdata, - record, - force_send=False, - send_after_commit=True, - model_description=False, - mail_auto_delete=True, - ): - if self.env.context.get("force_partners_to_notify"): - partners_to_notify = self.env.context.get("force_partners_to_notify") - record = self.filtered(lambda p: p.id in partners_to_notify) - return super()._notify( - message, - rdata, - record, - force_send=force_send, - send_after_commit=send_after_commit, - model_description=model_description, - mail_auto_delete=mail_auto_delete, - ) diff --git a/mail_optional_follower_notification/tests/__init__.py b/mail_optional_follower_notification/tests/__init__.py index 17a2936..b58d6f2 100644 --- a/mail_optional_follower_notification/tests/__init__.py +++ b/mail_optional_follower_notification/tests/__init__.py @@ -1,3 +1 @@ -# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). - from . import test_mail_optional_follower_notifications diff --git a/mail_optional_follower_notification/tests/test_mail_optional_follower_notifications.py b/mail_optional_follower_notification/tests/test_mail_optional_follower_notifications.py index 224503d..439e002 100644 --- a/mail_optional_follower_notification/tests/test_mail_optional_follower_notifications.py +++ b/mail_optional_follower_notification/tests/test_mail_optional_follower_notifications.py @@ -1,59 +1,88 @@ -# Copyright 2016 ACSONE SA/NV () +# Copyright 2019 ACSONE SA/NV () # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from odoo.tests import common -class TestMailOptionalFollowernotifications(common.TransactionCase): - def setUp(self): - super().setUp() - self.partner_obj = self.env["res.partner"] - self.partner_01 = self.env.ref("base.res_partner_2") - self.demo_user = self.env.ref("base.user_demo") - self.partner_03 = self.demo_user.copy().partner_id - - def test_send_email_optional_follower_notifications(self): - ctx = self.env.context.copy() +class TestMailOptionalFollowernotifications(common.SavepointCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.partner_obj = cls.env["res.partner"] + cls.partner_01 = cls.env.ref("base.res_partner_2") + demo_user = cls.env.ref("base.user_demo") + cls.partner_follower = demo_user.partner_id + cls.partner_no_follower = demo_user.copy().partner_id + cls.partner_01.message_subscribe(partner_ids=[cls.partner_follower.id]) + ctx = cls.env.context.copy() ctx.update( { "default_model": "res.partner", - "default_res_id": self.partner_01.id, + "default_res_id": cls.partner_01.id, "default_composition_mode": "comment", } ) - mail_compose = self.env["mail.compose.message"] - self.partner_01.message_subscribe(partner_ids=[self.demo_user.partner_id.id]) - values = mail_compose.with_context(ctx).onchange_template_id( - False, "comment", "res.partner", self.partner_01.id - )["value"] - values["partner_ids"] = [ - (4, self.demo_user.partner_id.id), - (4, self.partner_03.id), + cls.mail_compose_context = ctx + cls.MailCompose = cls.env["mail.compose.message"] + + def _send_mail(self, recipients, notify_followers): + old_messages = self.env["mail.message"].search([]) + values = self.MailCompose.with_context( + self.mail_compose_context + ).onchange_template_id(False, "comment", "res.partner", self.partner_01.id)[ + "value" ] - compose_id = mail_compose.with_context(ctx).create(values) - compose_id.with_context(ctx).send_mail() - res = self.env["mail.message"].search( - [("model", "=", "res.partner"), ("res_id", "=", self.partner_01.id)] + values["partner_ids"] = [(6, 0, recipients.ids)] + values["notify_followers"] = notify_followers + composer = self.MailCompose.with_context(self.mail_compose_context).create( + values ) - self.assertEqual(len(res.ids), 1) - message = self.env["mail.message"] - for record in res: - if record.notification_ids.mapped("res_partner_id").ids == [ - self.partner_03.id - ] and record.partner_ids.ids == [self.partner_03.id]: - message += record - self.assertEqual(len(message.ids), 0) - values["partner_ids"] = [(6, 0, [self.partner_03.id])] - compose_id = mail_compose.with_context(ctx).create(values) - compose_id.notify_followers = False - compose_id.with_context(ctx).send_mail() - res = self.env["mail.message"].search( - [("model", "=", "res.partner"), ("res_id", "=", self.partner_01.id)] + composer.send_mail() + return self.env["mail.message"].search([]) - old_messages + + def test_1(self): + """ + Data: + One partner follower of partner_01 + Test case: + Send message to the follower and a non follower partner + Expected result: + Both are notified + """ + message = self._send_mail( + self.partner_follower + self.partner_no_follower, notify_followers=True + ) + self.assertEqual( + message.notification_ids.mapped("res_partner_id"), + self.partner_no_follower + self.partner_follower, + ) + + def test_2(self): + """ + Data: + One partner follower of partner_01 + Test case: + Send message to the non follower partner + Expected result: + Both are notified + """ + message = self._send_mail(self.partner_no_follower, notify_followers=True) + self.assertEqual( + message.notification_ids.mapped("res_partner_id"), + self.partner_no_follower + self.partner_follower, + ) + + def test_3(self): + """ + Data: + One partner follower of partner_01 + Test case: + Send message to the non follower partner and disable the + notification to followers + Expected result: + Only the non follower partner is notified + """ + message = self._send_mail(self.partner_no_follower, notify_followers=False) + self.assertEqual( + message.notification_ids.mapped("res_partner_id"), self.partner_no_follower ) - message = self.env["mail.message"] - for record in res: - if record.notification_ids.mapped("res_partner_id").ids == [ - self.partner_03.id - ] and record.partner_ids.ids == [self.partner_03.id]: - message += record - self.assertEqual(len(message.ids), 1) diff --git a/mail_optional_follower_notification/wizard/mail_compose_message.py b/mail_optional_follower_notification/wizard/mail_compose_message.py index 843c86f..7a32f10 100644 --- a/mail_optional_follower_notification/wizard/mail_compose_message.py +++ b/mail_optional_follower_notification/wizard/mail_compose_message.py @@ -1,7 +1,7 @@ # Copyright 2016 ACSONE SA/NV () # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo import api, fields, models +from odoo import fields, models class MailComposeMessage(models.TransientModel): @@ -9,11 +9,8 @@ class MailComposeMessage(models.TransientModel): notify_followers = fields.Boolean(default=True) - @api.multi def send_mail(self, auto_commit=False): - ctx = self.env.context.copy() for wizard in self: - ctx["notify_followers"] = wizard.notify_followers - wizard = wizard.with_context(ctx) + wizard = wizard.with_context(notify_followers=wizard.notify_followers) super(MailComposeMessage, wizard).send_mail(auto_commit=auto_commit) - return {"type": "ir.actions.act_window_close"} + return True diff --git a/mail_optional_follower_notification/wizard/mail_compose_message_view.xml b/mail_optional_follower_notification/wizard/mail_compose_message_view.xml index b252a0e..e5b3d39 100644 --- a/mail_optional_follower_notification/wizard/mail_compose_message_view.xml +++ b/mail_optional_follower_notification/wizard/mail_compose_message_view.xml @@ -5,12 +5,12 @@ mail.compose.message - + - - - - Warning : Followers will not be notified but they can access the notification directly from the document (if they are allowed to) - + + + - Warning : Followers will not be notified but they can access the notification directly from the document (if they are allowed to) +