[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 models
from . import wizard from . import wizard

View File

@ -6,7 +6,7 @@
"author": "ACSONE SA/NV," "Odoo Community Association (OCA)", "author": "ACSONE SA/NV," "Odoo Community Association (OCA)",
"website": "https://github.com/OCA/social", "website": "https://github.com/OCA/social",
"category": "Social Network", "category": "Social Network",
"version": "12.0.1.0.0", "version": "13.0.1.0.0",
"license": "AGPL-3", "license": "AGPL-3",
"depends": ["mail"], "depends": ["mail"],
"data": ["wizard/mail_compose_message_view.xml"], "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_thread
from . import mail_message
from . import res_partner

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 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). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo.tests import common from odoo.tests import common
class TestMailOptionalFollowernotifications(common.TransactionCase): class TestMailOptionalFollowernotifications(common.SavepointCase):
def setUp(self): @classmethod
super().setUp() def setUpClass(cls):
self.partner_obj = self.env["res.partner"] super().setUpClass()
self.partner_01 = self.env.ref("base.res_partner_2") cls.partner_obj = cls.env["res.partner"]
self.demo_user = self.env.ref("base.user_demo") cls.partner_01 = cls.env.ref("base.res_partner_2")
self.partner_03 = self.demo_user.copy().partner_id demo_user = cls.env.ref("base.user_demo")
cls.partner_follower = demo_user.partner_id
def test_send_email_optional_follower_notifications(self): cls.partner_no_follower = demo_user.copy().partner_id
ctx = self.env.context.copy() cls.partner_01.message_subscribe(partner_ids=[cls.partner_follower.id])
ctx = cls.env.context.copy()
ctx.update( ctx.update(
{ {
"default_model": "res.partner", "default_model": "res.partner",
"default_res_id": self.partner_01.id, "default_res_id": cls.partner_01.id,
"default_composition_mode": "comment", "default_composition_mode": "comment",
} }
) )
mail_compose = self.env["mail.compose.message"] cls.mail_compose_context = ctx
self.partner_01.message_subscribe(partner_ids=[self.demo_user.partner_id.id]) cls.MailCompose = cls.env["mail.compose.message"]
values = mail_compose.with_context(ctx).onchange_template_id(
False, "comment", "res.partner", self.partner_01.id def _send_mail(self, recipients, notify_followers):
)["value"] old_messages = self.env["mail.message"].search([])
values["partner_ids"] = [ values = self.MailCompose.with_context(
(4, self.demo_user.partner_id.id), self.mail_compose_context
(4, self.partner_03.id), ).onchange_template_id(False, "comment", "res.partner", self.partner_01.id)[
"value"
] ]
compose_id = mail_compose.with_context(ctx).create(values) values["partner_ids"] = [(6, 0, recipients.ids)]
compose_id.with_context(ctx).send_mail() values["notify_followers"] = notify_followers
res = self.env["mail.message"].search( composer = self.MailCompose.with_context(self.mail_compose_context).create(
[("model", "=", "res.partner"), ("res_id", "=", self.partner_01.id)] values
) )
self.assertEqual(len(res.ids), 1) composer.send_mail()
message = self.env["mail.message"] return self.env["mail.message"].search([]) - old_messages
for record in res:
if record.notification_ids.mapped("res_partner_id").ids == [ def test_1(self):
self.partner_03.id """
] and record.partner_ids.ids == [self.partner_03.id]: Data:
message += record One partner follower of partner_01
self.assertEqual(len(message.ids), 0) Test case:
values["partner_ids"] = [(6, 0, [self.partner_03.id])] Send message to the follower and a non follower partner
compose_id = mail_compose.with_context(ctx).create(values) Expected result:
compose_id.notify_followers = False Both are notified
compose_id.with_context(ctx).send_mail() """
res = self.env["mail.message"].search( message = self._send_mail(
[("model", "=", "res.partner"), ("res_id", "=", self.partner_01.id)] 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>) # Copyright 2016 ACSONE SA/NV (<http://acsone.eu>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). # 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): class MailComposeMessage(models.TransientModel):
@ -9,11 +9,8 @@ class MailComposeMessage(models.TransientModel):
notify_followers = fields.Boolean(default=True) notify_followers = fields.Boolean(default=True)
@api.multi
def send_mail(self, auto_commit=False): def send_mail(self, auto_commit=False):
ctx = self.env.context.copy()
for wizard in self: for wizard in self:
ctx["notify_followers"] = wizard.notify_followers wizard = wizard.with_context(notify_followers=wizard.notify_followers)
wizard = wizard.with_context(ctx)
super(MailComposeMessage, wizard).send_mail(auto_commit=auto_commit) 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="model">mail.compose.message</field>
<field name="inherit_id" ref="mail.email_compose_message_wizard_form"/> <field name="inherit_id" ref="mail.email_compose_message_wizard_form"/>
<field name="arch" type="xml"> <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')]}"/> <field name="notify_followers" attrs="{'invisible': [('composition_mode', '=', 'mass_mail')]}"/>
</xpath> </span>
<xpath expr="//div[field[@name='partner_ids']]/span[2]" position="inside"> <span name="document_followers_text" position="after">
<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> <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>
</xpath> </span>
</field> </field>
</record> </record>