diff --git a/mail_restrict_follower_selection/__manifest__.py b/mail_restrict_follower_selection/__manifest__.py index cba1fe6..a207322 100644 --- a/mail_restrict_follower_selection/__manifest__.py +++ b/mail_restrict_follower_selection/__manifest__.py @@ -9,12 +9,7 @@ "license": "AGPL-3", "category": "Social Network", "summary": "Define a domain from which followers can be selected", - "depends": [ - 'mail', - ], - "data": [ - "data/ir_config_parameter.xml", - "data/ir_actions.xml", - ], - 'installable': True, + "depends": ["mail"], + "data": ["data/ir_config_parameter.xml", "data/ir_actions.xml"], + "installable": True, } diff --git a/mail_restrict_follower_selection/models/mail_followers.py b/mail_restrict_follower_selection/models/mail_followers.py index c096f1f..a19a27e 100644 --- a/mail_restrict_follower_selection/models/mail_followers.py +++ b/mail_restrict_follower_selection/models/mail_followers.py @@ -6,23 +6,35 @@ from odoo.tools.safe_eval import safe_eval class MailFollowers(models.Model): - _inherit = 'mail.followers' + _inherit = "mail.followers" - def _add_followers(self, res_model, res_ids, partner_ids, partner_subtypes, - channel_ids, channel_subtypes, - check_existing=False, existing_policy='skip'): + def _add_followers( + self, + res_model, + res_ids, + partner_ids, + partner_subtypes, + channel_ids, + channel_subtypes, + check_existing=False, + existing_policy="skip", + ): domain = self.env[ - 'mail.wizard.invite' + "mail.wizard.invite" ]._mail_restrict_follower_selection_get_domain() - partners = self.env['res.partner'].search( - [('id', 'in', partner_ids)] + - safe_eval(domain) + partners = self.env["res.partner"].search( + [("id", "in", partner_ids)] + safe_eval(domain) ) _res_ids = res_ids.copy() or [0] new, update = super()._add_followers( - res_model, res_ids, partners.ids, partner_subtypes, channel_ids, - channel_subtypes, check_existing=check_existing, - existing_policy=existing_policy + res_model, + res_ids, + partners.ids, + partner_subtypes, + channel_ids, + channel_subtypes, + check_existing=check_existing, + existing_policy=existing_policy, ) for res_id in _res_ids: diff --git a/mail_restrict_follower_selection/models/mail_thread.py b/mail_restrict_follower_selection/models/mail_thread.py index c22dd3d..0bacf17 100644 --- a/mail_restrict_follower_selection/models/mail_thread.py +++ b/mail_restrict_follower_selection/models/mail_thread.py @@ -3,22 +3,24 @@ from odoo.tools.safe_eval import safe_eval class MailThread(models.AbstractModel): - _inherit = 'mail.thread' + _inherit = "mail.thread" @api.multi def _message_add_suggested_recipient( - self, result, partner=None, email=None, reason=''): + self, result, partner=None, email=None, reason="" + ): result = super(MailThread, self)._message_add_suggested_recipient( - result, partner=partner, email=email, reason=reason) + result, partner=partner, email=email, reason=reason + ) domain = self.env[ - 'mail.wizard.invite' + "mail.wizard.invite" ]._mail_restrict_follower_selection_get_domain() eval_domain = safe_eval(domain) for key in result: for partner_id, email, reason in result[key]: if partner_id: - partner = self.env['res.partner'].search( - [('id', '=', partner_id)] + eval_domain + partner = self.env["res.partner"].search( + [("id", "=", partner_id)] + eval_domain ) if not partner: result[key].remove((partner_id, email, reason)) diff --git a/mail_restrict_follower_selection/models/mail_wizard_invite.py b/mail_restrict_follower_selection/models/mail_wizard_invite.py index 1d8b31c..e04c486 100644 --- a/mail_restrict_follower_selection/models/mail_wizard_invite.py +++ b/mail_restrict_follower_selection/models/mail_wizard_invite.py @@ -3,31 +3,38 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from lxml import etree + from odoo import api, models class MailWizardInvite(models.TransientModel): - _inherit = 'mail.wizard.invite' + _inherit = "mail.wizard.invite" @api.model def _mail_restrict_follower_selection_get_domain(self): - parameter_name = 'mail_restrict_follower_selection.domain' - return self.env['ir.config_parameter'].sudo().get_param( - "{0}.{1}".format(parameter_name, - self.env.context.get('default_res_model')), - self.env['ir.config_parameter'].sudo().get_param( - parameter_name, default='[]') + parameter_name = "mail_restrict_follower_selection.domain" + return ( + self.env["ir.config_parameter"] + .sudo() + .get_param( + "{}.{}".format( + parameter_name, self.env.context.get("default_res_model") + ), + self.env["ir.config_parameter"] + .sudo() + .get_param(parameter_name, default="[]"), + ) ) @api.model - def fields_view_get(self, view_id=None, view_type='form', toolbar=False, - submenu=False): + def fields_view_get( + self, view_id=None, view_type="form", toolbar=False, submenu=False + ): result = super(MailWizardInvite, self).fields_view_get( - view_id=view_id, view_type=view_type, toolbar=toolbar, - submenu=submenu) - arch = etree.fromstring(result['arch']) + view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu + ) + arch = etree.fromstring(result["arch"]) for field in arch.xpath('//field[@name="partner_ids"]'): - field.attrib['domain'] = self\ - ._mail_restrict_follower_selection_get_domain() - result['arch'] = etree.tostring(arch) + field.attrib["domain"] = self._mail_restrict_follower_selection_get_domain() + result["arch"] = etree.tostring(arch) return result diff --git a/mail_restrict_follower_selection/tests/test_mail_restrict_follower_selection.py b/mail_restrict_follower_selection/tests/test_mail_restrict_follower_selection.py index 4eb0bb1..0b78d65 100644 --- a/mail_restrict_follower_selection/tests/test_mail_restrict_follower_selection.py +++ b/mail_restrict_follower_selection/tests/test_mail_restrict_follower_selection.py @@ -3,55 +3,59 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from lxml import etree + from odoo.tests.common import TransactionCase class TestMailRestrictFollowerSelection(TransactionCase): - def setUp(self): super().setUp() - self.partner = self.env['res.partner'].create({ - 'name': 'Partner', - 'customer': True, - 'email': 'test@test.com', - }) + self.partner = self.env["res.partner"].create( + {"name": "Partner", "customer": True, "email": "test@test.com"} + ) def test_fields_view_get(self): - result = self.env['mail.wizard.invite'].fields_view_get( - view_type='form') - for field in etree.fromstring(result['arch']).xpath( - '//field[@name="partner_ids"]'): - self.assertTrue(field.get('domain')) + result = self.env["mail.wizard.invite"].fields_view_get(view_type="form") + for field in etree.fromstring(result["arch"]).xpath( + '//field[@name="partner_ids"]' + ): + self.assertTrue(field.get("domain")) def send_action(self): - compose = self.env['mail.compose.message'].with_context({ - 'mail_post_autofollow': True, - 'default_composition_mode': 'comment', - 'default_model': 'res.partner', - 'default_use_active_domain': True, - }).create({ - 'subject': 'From Composer Test', - 'body': '${object.description}', - 'res_id': self.partner.id, - 'partner_ids': [(4, id) for id in self.partner.ids], - }) + compose = ( + self.env["mail.compose.message"] + .with_context( + { + "mail_post_autofollow": True, + "default_composition_mode": "comment", + "default_model": "res.partner", + "default_use_active_domain": True, + } + ) + .create( + { + "subject": "From Composer Test", + "body": "${object.description}", + "res_id": self.partner.id, + "partner_ids": [(4, id) for id in self.partner.ids], + } + ) + ) self.assertEqual(compose.partner_ids, self.partner) compose.action_send_mail() def test_followers_meet(self): - self.partner.write({'customer': True}) + self.partner.write({"customer": True}) self.assertTrue(self.partner.customer) self.send_action() self.assertIn( - self.partner, - self.partner.message_follower_ids.mapped('partner_id') + self.partner, self.partner.message_follower_ids.mapped("partner_id") ) def test_followers_not_meet(self): - self.partner.write({'customer': False}) + self.partner.write({"customer": False}) self.assertFalse(self.partner.customer) self.send_action() self.assertNotIn( - self.partner, - self.partner.message_follower_ids.mapped('partner_id') + self.partner, self.partner.message_follower_ids.mapped("partner_id") )