[IMP] mass_mailing_list_dynamic: black, isort

pull/1368/head
Jared Kipe 2020-03-10 08:40:19 -07:00 committed by Pedro Evaristo Gonzalez Sanchez
parent f076a75696
commit fa2fc83522
6 changed files with 78 additions and 85 deletions

View File

@ -12,9 +12,7 @@
"license": "AGPL-3",
"application": False,
"installable": True,
"depends": [
"mass_mailing_partner",
],
"depends": ["mass_mailing_partner"],
"data": [
# This should go first
"wizards/mail_mass_mailing_load_filter_views.xml",

View File

@ -13,9 +13,15 @@ class MassMailingContact(models.Model):
def _check_no_manual_edits_on_fully_synced_lists(self):
if self.env.context.get("syncing"):
return
if any(self.mapped('list_ids').filtered(
lambda x: x.dynamic and x.sync_method == "full")):
if any(
self.mapped("list_ids").filtered(
lambda x: x.dynamic and x.sync_method == "full"
)
):
raise ValidationError(
_("Cannot edit manually contacts in a fully "
_(
"Cannot edit manually contacts in a fully "
"synchronized list. Change its sync method or execute "
"a manual sync instead."))
"a manual sync instead."
)
)

View File

@ -10,17 +10,14 @@ class MassMailingList(models.Model):
dynamic = fields.Boolean(
help="Set this list as dynamic, to make it autosynchronized with "
"partners from a given criteria.",
"partners from a given criteria."
)
sync_method = fields.Selection(
[
("add", "Only add new records"),
("full", "Add and remove records as needed"),
],
[("add", "Only add new records"), ("full", "Add and remove records as needed")],
default="add",
required=True,
help="Choose the syncronization method for this list if you want to "
"make it dynamic",
"make it dynamic",
)
sync_domain = fields.Char(
string="Synchronization critera",
@ -29,15 +26,12 @@ class MassMailingList(models.Model):
help="Filter partners to sync in this list",
)
is_synced = fields.Boolean(
help="Helper field to make the user aware of unsynced changes",
default=True,
help="Helper field to make the user aware of unsynced changes", default=True
)
def action_sync(self):
"""Sync contacts in dynamic lists."""
Contact = self.env["mail.mass_mailing.contact"].with_context(
syncing=True,
)
Contact = self.env["mail.mass_mailing.contact"].with_context(syncing=True)
Partner = self.env["res.partner"]
# Skip non-dynamic lists
dynamic = self.filtered("dynamic").with_context(syncing=True)
@ -47,11 +41,12 @@ class MassMailingList(models.Model):
# Detach or remove undesired contacts when synchronization is full
if one.sync_method == "full":
contact_to_detach = one.contact_ids.filtered(
lambda r: r.partner_id not in desired_partners)
lambda r: r.partner_id not in desired_partners
)
one.contact_ids -= contact_to_detach
contact_to_detach.filtered(lambda r: not r.list_ids).unlink()
# Add new contacts
current_partners = one.contact_ids.mapped('partner_id')
current_partners = one.contact_ids.mapped("partner_id")
contact_to_list = self.env["mail.mass_mailing.contact"]
vals_list = []
for partner in desired_partners - current_partners:
@ -59,10 +54,9 @@ class MassMailingList(models.Model):
if contacts_in_partner:
contact_to_list |= contacts_in_partner[0]
else:
vals_list.append({
"list_ids": [(4, one.id)],
"partner_id": partner.id,
})
vals_list.append(
{"list_ids": [(4, one.id)], "partner_id": partner.id}
)
one.contact_ids |= contact_to_list
Contact.create(vals_list)
one.is_synced = True

View File

@ -5,7 +5,7 @@ from odoo import models
class ResPartner(models.Model):
_inherit = 'res.partner'
_inherit = "res.partner"
def write(self, vals):
"""Allow to write values in mass mailing contact."""

View File

@ -2,6 +2,7 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from mock import patch
from odoo.exceptions import ValidationError
from odoo.tests import common
@ -12,25 +13,26 @@ class DynamicListCase(common.SavepointCase):
@classmethod
def setUpClass(cls):
super(DynamicListCase, cls).setUpClass()
cls.tag = cls.env["res.partner.category"].create({
"name": "testing tag",
})
cls.tag = cls.env["res.partner.category"].create({"name": "testing tag"})
cls.partners = cls.env["res.partner"]
for number in range(5):
cls.partners |= cls.partners.create({
"name": "partner %d" % number,
"category_id": [(4, cls.tag.id, False)],
"email": "%d@example.com" % number,
})
cls.list = cls.env["mail.mass_mailing.list"].create({
"name": "test list",
"dynamic": True,
"sync_domain": repr([("category_id", "in", cls.tag.ids)]),
})
cls.mail = cls.env["mail.mass_mailing"].create({
"name": "test mass mailing",
"contact_list_ids": [(4, cls.list.id, False)],
})
cls.partners |= cls.partners.create(
{
"name": "partner %d" % number,
"category_id": [(4, cls.tag.id, False)],
"email": "%d@example.com" % number,
}
)
cls.list = cls.env["mail.mass_mailing.list"].create(
{
"name": "test list",
"dynamic": True,
"sync_domain": repr([("category_id", "in", cls.tag.ids)]),
}
)
cls.mail = cls.env["mail.mass_mailing"].create(
{"name": "test mass mailing", "contact_list_ids": [(4, cls.list.id, False)]}
)
cls.mail._onchange_model_and_list()
def test_list_sync(self):
@ -43,10 +45,9 @@ class DynamicListCase(common.SavepointCase):
# Set list as unsynced
self.list.dynamic = False
# Create contact for partner 0 in unsynced list
contact0 = Contact.create({
"list_ids": [(4, self.list.id)],
"partner_id": self.partners[0].id,
})
contact0 = Contact.create(
{"list_ids": [(4, self.list.id)], "partner_id": self.partners[0].id}
)
self.assertEqual(self.list.contact_nbr, 1)
# Set list as add-synced
self.list.dynamic = True
@ -55,22 +56,21 @@ class DynamicListCase(common.SavepointCase):
self.assertTrue(contact0.exists())
# Set list as full-synced
self.list.sync_method = "full"
Contact.search([
("list_ids", "in", self.list.ids),
("partner_id", "=", self.partners[2].id),
]).unlink()
Contact.search(
[
("list_ids", "in", self.list.ids),
("partner_id", "=", self.partners[2].id),
]
).unlink()
self.list.action_sync()
self.assertEqual(self.list.contact_nbr, 3)
self.assertFalse(contact0.exists())
# Cannot add or edit contacts in fully synced lists
with self.assertRaises(ValidationError):
Contact.create({
"list_ids": [(4, self.list.id)],
"partner_id": self.partners[0].id,
})
contact1 = Contact.search([
("list_ids", "in", self.list.ids),
], limit=1)
Contact.create(
{"list_ids": [(4, self.list.id)], "partner_id": self.partners[0].id}
)
contact1 = Contact.search([("list_ids", "in", self.list.ids)], limit=1)
with self.assertRaises(ValidationError):
contact1.name = "other"
with self.assertRaises(ValidationError):
@ -80,10 +80,7 @@ class DynamicListCase(common.SavepointCase):
# Unset dynamic list
self.list.dynamic = False
# Now the contact is created without exception
Contact.create({
"list_ids": [(4, self.list.id)],
"email": "test@example.com",
})
Contact.create({"list_ids": [(4, self.list.id)], "email": "test@example.com"})
# Contacts can now be changed
contact1.name = "other"
@ -92,11 +89,13 @@ class DynamicListCase(common.SavepointCase):
self.list.action_sync()
self.assertEqual(self.list.contact_nbr, 5)
# Create a new partner
self.partners.create({
"name": "extra partner",
"category_id": [(4, self.tag.id, False)],
"email": "extra@example.com",
})
self.partners.create(
{
"name": "extra partner",
"category_id": [(4, self.tag.id, False)],
"email": "extra@example.com",
}
)
# Mock sending low level method, because an auto-commit happens there
with patch("odoo.addons.mail.models.mail_mail.MailMail.send") as s:
self.mail.send_mail()
@ -105,26 +104,22 @@ class DynamicListCase(common.SavepointCase):
def test_load_filter(self):
domain = "[('id', '=', 1)]"
ir_filter = self.env['ir.filters'].create({
'name': 'Test filter',
'model_id': 'res.partner',
'domain': domain,
})
wizard = self.env['mail.mass_mailing.load.filter'].with_context(
active_id=self.list.id,
).create({
'filter_id': ir_filter.id,
})
ir_filter = self.env["ir.filters"].create(
{"name": "Test filter", "model_id": "res.partner", "domain": domain}
)
wizard = (
self.env["mail.mass_mailing.load.filter"]
.with_context(active_id=self.list.id)
.create({"filter_id": ir_filter.id})
)
wizard.load_filter()
self.assertEqual(self.list.sync_domain, domain)
def test_change_partner(self):
self.list.sync_method = 'full'
self.list.sync_method = "full"
self.list.action_sync()
# This shouldn't fail
self.partners[:1].write({
'email': 'test_mass_mailing_list_dynamic@example.org',
})
self.partners[:1].write({"email": "test_mass_mailing_list_dynamic@example.org"})
def test_is_synced(self):
self.list.dynamic = False

View File

@ -9,17 +9,17 @@ class MassMailingLoadFilter(models.TransientModel):
_description = "Mail Mass Mailing Load Filter"
filter_id = fields.Many2one(
comodel_name='ir.filters',
comodel_name="ir.filters",
string="Filter to load",
required=True,
domain="[('model_id', '=', 'res.partner'), '|', "
"('user_id', '=', uid), ('user_id','=',False)]",
ondelete='cascade',
"('user_id', '=', uid), ('user_id','=',False)]",
ondelete="cascade",
)
def load_filter(self):
self.ensure_one()
mass_list = self.env['mail.mass_mailing.list'].browse(
self.env.context['active_id']
mass_list = self.env["mail.mass_mailing.list"].browse(
self.env.context["active_id"]
)
mass_list.sync_domain = self.filter_id.domain