[IMP] mail_optional_follower_notification: black, isort

This commit is contained in:
Laurent Mignon (ACSONE) 2019-12-06 11:37:20 +01:00 committed by Robin Goots
parent 7ea0295439
commit cedbe63549
5 changed files with 84 additions and 67 deletions

View File

@ -3,16 +3,11 @@
{ {
"name": "Mail optional follower notification", "name": "Mail optional follower notification",
"summary": "Choose to notify followers on mail.compose.message", "summary": "Choose to notify followers on mail.compose.message",
"author": "ACSONE SA/NV," "author": "ACSONE SA/NV," "Odoo Community Association (OCA)",
"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": "12.0.1.0.0",
"license": "AGPL-3", "license": "AGPL-3",
"depends": [ "depends": ["mail"],
"mail", "data": ["wizard/mail_compose_message_view.xml"],
],
"data": [
"wizard/mail_compose_message_view.xml",
],
} }

View File

@ -1,32 +1,43 @@
# 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 models, api from odoo import api, models
class MailMessage(models.Model): class MailMessage(models.Model):
_inherit = 'mail.message' _inherit = "mail.message"
@api.model @api.model
def create(self, values): def create(self, values):
ctx = self.env.context.copy() ctx = self.env.context.copy()
if not ctx.get('notify_followers') and values.get('partner_ids'): if not ctx.get("notify_followers") and values.get("partner_ids"):
partner_list = self.resolve_2many_commands( partner_list = self.resolve_2many_commands(
'partner_ids', values.get('partner_ids'), fields=['id']) "partner_ids", values.get("partner_ids"), fields=["id"]
ctx['force_partners_to_notify'] = [d['id'] for d in partner_list] )
ctx["force_partners_to_notify"] = [d["id"] for d in partner_list]
return super(MailMessage, self.with_context(ctx)).create(values) return super(MailMessage, self.with_context(ctx)).create(values)
@api.multi @api.multi
def _notify(self, record, msg_vals, force_send=False, def _notify(
send_after_commit=True, model_description=False, self,
mail_auto_delete=True): record,
msg_vals,
force_send=False,
send_after_commit=True,
model_description=False,
mail_auto_delete=True,
):
res = super()._notify( res = super()._notify(
record, msg_vals, force_send=force_send, record,
msg_vals,
force_send=force_send,
send_after_commit=send_after_commit, send_after_commit=send_after_commit,
model_description=model_description, model_description=model_description,
mail_auto_delete=mail_auto_delete) mail_auto_delete=mail_auto_delete,
if self.env.context.get('force_partners_to_notify'): )
if self.env.context.get("force_partners_to_notify"):
# Needaction only for recipients # Needaction only for recipients
self.needaction_partner_ids = [ self.needaction_partner_ids = [
(6, 0, self.env.context.get('force_partners_to_notify'))] (6, 0, self.env.context.get("force_partners_to_notify"))
]
return res return res

View File

@ -1,22 +1,32 @@
# 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 models, api from odoo import api, models
class ResPartner(models.Model): class ResPartner(models.Model):
_inherit = 'res.partner' _inherit = "res.partner"
@api.model @api.model
def _notify(self, message, rdata, record, force_send=False, def _notify(
send_after_commit=True, model_description=False, self,
mail_auto_delete=True): message,
if self.env.context.get('force_partners_to_notify'): rdata,
partners_to_notify =\ record,
self.env.context.get('force_partners_to_notify') 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) record = self.filtered(lambda p: p.id in partners_to_notify)
return super()._notify( return super()._notify(
message, rdata, record, message,
force_send=force_send, send_after_commit=send_after_commit, rdata,
record,
force_send=force_send,
send_after_commit=send_after_commit,
model_description=model_description, model_description=model_description,
mail_auto_delete=mail_auto_delete) mail_auto_delete=mail_auto_delete,
)

View File

@ -5,53 +5,55 @@ from odoo.tests import common
class TestMailOptionalFollowernotifications(common.TransactionCase): class TestMailOptionalFollowernotifications(common.TransactionCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.partner_obj = self.env['res.partner'] self.partner_obj = self.env["res.partner"]
self.partner_01 = self.env.ref('base.res_partner_2') self.partner_01 = self.env.ref("base.res_partner_2")
self.demo_user = self.env.ref('base.user_demo') self.demo_user = self.env.ref("base.user_demo")
self.partner_03 = self.demo_user.copy().partner_id self.partner_03 = self.demo_user.copy().partner_id
def test_send_email_optional_follower_notifications(self): def test_send_email_optional_follower_notifications(self):
ctx = self.env.context.copy() ctx = self.env.context.copy()
ctx.update({ ctx.update(
'default_model': 'res.partner', {
'default_res_id': self.partner_01.id, "default_model": "res.partner",
'default_composition_mode': 'comment', "default_res_id": self.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]) mail_compose = self.env["mail.compose.message"]
values = mail_compose.with_context(ctx)\ self.partner_01.message_subscribe(partner_ids=[self.demo_user.partner_id.id])
.onchange_template_id(False, 'comment', 'res.partner', values = mail_compose.with_context(ctx).onchange_template_id(
self.partner_01.id)['value'] False, "comment", "res.partner", self.partner_01.id
values['partner_ids'] = [(4, self.demo_user.partner_id.id), )["value"]
(4, self.partner_03.id)] values["partner_ids"] = [
(4, self.demo_user.partner_id.id),
(4, self.partner_03.id),
]
compose_id = mail_compose.with_context(ctx).create(values) compose_id = mail_compose.with_context(ctx).create(values)
compose_id.with_context(ctx).send_mail() compose_id.with_context(ctx).send_mail()
res = self.env["mail.message"].search( res = self.env["mail.message"].search(
[('model', '=', 'res.partner'), [("model", "=", "res.partner"), ("res_id", "=", self.partner_01.id)]
('res_id', '=', self.partner_01.id)]) )
self.assertEqual(len(res.ids), 1) self.assertEqual(len(res.ids), 1)
message = self.env['mail.message'] message = self.env["mail.message"]
for record in res: for record in res:
if record.notification_ids.mapped('res_partner_id').ids == \ if record.notification_ids.mapped("res_partner_id").ids == [
[self.partner_03.id] and \ self.partner_03.id
record.partner_ids.ids == [self.partner_03.id]: ] and record.partner_ids.ids == [self.partner_03.id]:
message += record message += record
self.assertEqual(len(message.ids), 0) self.assertEqual(len(message.ids), 0)
values['partner_ids'] = [(6, 0, [self.partner_03.id])] values["partner_ids"] = [(6, 0, [self.partner_03.id])]
compose_id = mail_compose.with_context(ctx).create(values) compose_id = mail_compose.with_context(ctx).create(values)
compose_id.notify_followers = False compose_id.notify_followers = False
compose_id.with_context(ctx).send_mail() compose_id.with_context(ctx).send_mail()
res = self.env["mail.message"].search( res = self.env["mail.message"].search(
[('model', '=', 'res.partner'), [("model", "=", "res.partner"), ("res_id", "=", self.partner_01.id)]
('res_id', '=', self.partner_01.id)]) )
message = self.env['mail.message'] message = self.env["mail.message"]
for record in res: for record in res:
if record.notification_ids.mapped('res_partner_id').ids == \ if record.notification_ids.mapped("res_partner_id").ids == [
[self.partner_03.id] and\ self.partner_03.id
record.partner_ids.ids == [self.partner_03.id]: ] and record.partner_ids.ids == [self.partner_03.id]:
message += record message += record
self.assertEqual(len(message.ids), 1) self.assertEqual(len(message.ids), 1)

View File

@ -1,11 +1,11 @@
# 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 models, fields, api from odoo import api, fields, models
class MailComposeMessage(models.TransientModel): class MailComposeMessage(models.TransientModel):
_inherit = 'mail.compose.message' _inherit = "mail.compose.message"
notify_followers = fields.Boolean(default=True) notify_followers = fields.Boolean(default=True)
@ -13,8 +13,7 @@ class MailComposeMessage(models.TransientModel):
def send_mail(self, auto_commit=False): def send_mail(self, auto_commit=False):
ctx = self.env.context.copy() ctx = self.env.context.copy()
for wizard in self: for wizard in self:
ctx['notify_followers'] = wizard.notify_followers ctx["notify_followers"] = wizard.notify_followers
wizard = wizard.with_context(ctx) wizard = wizard.with_context(ctx)
super(MailComposeMessage, wizard).send_mail( super(MailComposeMessage, wizard).send_mail(auto_commit=auto_commit)
auto_commit=auto_commit) return {"type": "ir.actions.act_window_close"}
return {'type': 'ir.actions.act_window_close'}