[MIG] mail_optional_follower_notification: Migration to 13.0

This commit is contained in:
Laurent Mignon (ACSONE) 2019-12-06 11:49:27 +01:00 committed by Robin Goots
parent cedbe63549
commit 9a71d7c4f2
10 changed files with 109 additions and 140 deletions

View File

@ -1,4 +1,2 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from . import models
from . import wizard

View File

@ -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"],

View File

@ -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

View File

@ -1,43 +0,0 @@
# Copyright 2016 ACSONE SA/NV (<http://acsone.eu>)
# 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

View File

@ -0,0 +1,25 @@
# Copyright 2019 ACSONE SA/NV (<http://acsone.eu>)
# 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

View File

@ -1,32 +0,0 @@
# Copyright 2016 ACSONE SA/NV (<http://acsone.eu>)
# 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,
)

View File

@ -1,3 +1 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from . import test_mail_optional_follower_notifications

View File

@ -1,59 +1,88 @@
# Copyright 2016 ACSONE SA/NV (<http://acsone.eu>)
# Copyright 2019 ACSONE SA/NV (<http://acsone.eu>)
# 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)

View File

@ -1,7 +1,7 @@
# Copyright 2016 ACSONE SA/NV (<http://acsone.eu>)
# 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

View File

@ -5,12 +5,12 @@
<field name="model">mail.compose.message</field>
<field name="inherit_id" ref="mail.email_compose_message_wizard_form"/>
<field name="arch" type="xml">
<xpath expr="//div[field[@name='partner_ids']]/span[2]" position="before">
<span name="document_followers_text" position="before">
<field name="notify_followers" attrs="{'invisible': [('composition_mode', '=', 'mass_mail')]}"/>
</xpath>
<xpath expr="//div[field[@name='partner_ids']]/span[2]" position="inside">
<span attrs="{'invisible': [('notify_followers', '=', True)]}" style="color: red;"> - Warning : Followers will not be notified but they can access the notification directly from the document (if they are allowed to)</span>
</xpath>
</span>
<span name="document_followers_text" position="after">
<span name="no_followers_text" attrs="{'invisible': [('notify_followers', '=', True)]}" style="color: red;"> - Warning : Followers will not be notified but they can access the notification directly from the document (if they are allowed to)</span>
</span>
</field>
</record>