[IMP] mass_mailing_partner: black, isort

This commit is contained in:
Manuel Calero 2020-02-10 11:35:23 +01:00 committed by emagdalenaC2i
parent 38ce378348
commit eda99e3607
16 changed files with 353 additions and 311 deletions

View File

@ -6,21 +6,18 @@
{ {
"name": "Link partners with mass-mailing", "name": "Link partners with mass-mailing",
"version": "12.0.1.0.2", "version": "12.0.1.0.2",
"author": "Tecnativa, " "author": "Tecnativa, " "Odoo Community Association (OCA)",
"Odoo Community Association (OCA)",
"website": "https://github.com/OCA/social", "website": "https://github.com/OCA/social",
"license": "AGPL-3", "license": "AGPL-3",
"category": "Marketing", "category": "Marketing",
"depends": [ "depends": ["mass_mailing"],
'mass_mailing',
],
"post_init_hook": "post_init_hook", "post_init_hook": "post_init_hook",
'data': [ "data": [
'views/mail_mail_statistics_view.xml', "views/mail_mail_statistics_view.xml",
'views/mail_mass_mailing_contact_view.xml', "views/mail_mass_mailing_contact_view.xml",
'views/mail_mass_mailing_view.xml', "views/mail_mass_mailing_view.xml",
'views/res_partner_view.xml', "views/res_partner_view.xml",
'wizard/partner_mail_list_wizard.xml' "wizard/partner_mail_list_wizard.xml",
], ],
"installable": True, "installable": True,
} }

View File

@ -5,7 +5,8 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import logging import logging
from odoo import api, SUPERUSER_ID
from odoo import SUPERUSER_ID, api
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
@ -14,23 +15,18 @@ def post_init_hook(cr, registry):
with api.Environment.manage(): with api.Environment.manage():
env = api.Environment(cr, SUPERUSER_ID, {}) env = api.Environment(cr, SUPERUSER_ID, {})
# ACTION 1: Match existing contacts # ACTION 1: Match existing contacts
contact_model = env['mail.mass_mailing.contact'] contact_model = env["mail.mass_mailing.contact"]
partner_model = env['res.partner'] partner_model = env["res.partner"]
contacts = contact_model.search([('email', '!=', False)]) contacts = contact_model.search([("email", "!=", False)])
_logger.info('Trying to match %d contacts to partner by email', _logger.info("Trying to match %d contacts to partner by email", len(contacts))
len(contacts))
for contact in contacts: for contact in contacts:
partners = partner_model.search([ partners = partner_model.search(
('email', '=ilike', contact.email) [("email", "=ilike", contact.email)], limit=1
], limit=1) )
if partners: if partners:
contact.write({'partner_id': partners.id}) contact.write({"partner_id": partners.id})
# ACTION 2: Match existing statistics # ACTION 2: Match existing statistics
stat_model = env['mail.mail.statistics'] stat_model = env["mail.mail.statistics"]
stats = stat_model.search([ stats = stat_model.search([("model", "!=", False), ("res_id", "!=", False)])
('model', '!=', False), _logger.info("Trying to link %d mass mailing statistics to partner", len(stats))
('res_id', '!=', False),
])
_logger.info('Trying to link %d mass mailing statistics to partner',
len(stats))
stats.partner_link() stats.partner_link()

View File

@ -8,16 +8,17 @@ class MailMailStatistics(models.Model):
_inherit = "mail.mail.statistics" _inherit = "mail.mail.statistics"
partner_id = fields.Many2one( partner_id = fields.Many2one(
string="Partner", comodel_name='res.partner', readonly=True) string="Partner", comodel_name="res.partner", readonly=True
)
@api.model @api.model
def partner_id_from_obj(self, model, res_id): def partner_id_from_obj(self, model, res_id):
partner_id = False partner_id = False
obj = self.env[model].browse(res_id) obj = self.env[model].browse(res_id)
if obj.exists(): if obj.exists():
if model == 'res.partner': if model == "res.partner":
partner_id = res_id partner_id = res_id
elif 'partner_id' in obj._fields: elif "partner_id" in obj._fields:
partner_id = obj.partner_id.id partner_id = obj.partner_id.id
return partner_id return partner_id

View File

@ -3,29 +3,31 @@
# Copyright 2015 Javier Iniesta <javieria@antiun.com> # Copyright 2015 Javier Iniesta <javieria@antiun.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import api, fields, models, _ from odoo import _, api, fields, models
from odoo.exceptions import ValidationError from odoo.exceptions import ValidationError
class MailMassMailingList(models.Model): class MailMassMailingList(models.Model):
_inherit = 'mail.mass_mailing.list' _inherit = "mail.mass_mailing.list"
partner_mandatory = fields.Boolean(string="Mandatory Partner", partner_mandatory = fields.Boolean(string="Mandatory Partner", default=False)
default=False) partner_category = fields.Many2one(
partner_category = fields.Many2one(comodel_name='res.partner.category', comodel_name="res.partner.category", string="Partner Tag"
string="Partner Tag") )
@api.constrains('contact_ids') @api.constrains("contact_ids")
def _check_contact_ids_partner_id(self): def _check_contact_ids_partner_id(self):
contact_obj = self.env['mail.mass_mailing.contact'] contact_obj = self.env["mail.mass_mailing.contact"]
for mailing_list in self: for mailing_list in self:
data = contact_obj.read_group( data = contact_obj.read_group(
[ [
('id', 'in', mailing_list.contact_ids.ids), ("id", "in", mailing_list.contact_ids.ids),
('partner_id', '!=', False), ("partner_id", "!=", False),
], ],
['partner_id'], ['partner_id'] ["partner_id"],
["partner_id"],
) )
if len(list(filter(lambda r: r['partner_id_count'] > 1, data))): if len(list(filter(lambda r: r["partner_id_count"] > 1, data))):
raise ValidationError(_("A partner cannot be multiple times " raise ValidationError(
"in the same list")) _("A partner cannot be multiple times " "in the same list")
)

View File

@ -9,24 +9,28 @@ from odoo.exceptions import ValidationError
class MailMassMailingContact(models.Model): class MailMassMailingContact(models.Model):
_inherit = 'mail.mass_mailing.contact' _inherit = "mail.mass_mailing.contact"
partner_id = fields.Many2one(comodel_name='res.partner', string="Partner", partner_id = fields.Many2one(
domain=[('email', '!=', False)]) comodel_name="res.partner", string="Partner", domain=[("email", "!=", False)]
)
@api.constrains('partner_id', 'list_ids') @api.constrains("partner_id", "list_ids")
def _check_partner_id_list_ids(self): def _check_partner_id_list_ids(self):
for contact in self: for contact in self:
if contact.partner_id: if contact.partner_id:
other_contact = self.search([ other_contact = self.search(
('partner_id', '=', contact.partner_id.id), [
('id', '!=', contact.id) ("partner_id", "=", contact.partner_id.id),
]) ("id", "!=", contact.id),
if contact.list_ids & other_contact.mapped('list_ids'): ]
raise ValidationError(_("Partner already exists in one of " )
"these mailing lists")) if contact.list_ids & other_contact.mapped("list_ids"):
raise ValidationError(
_("Partner already exists in one of " "these mailing lists")
)
@api.onchange('partner_id') @api.onchange("partner_id")
def _onchange_partner_mass_mailing_partner(self): def _onchange_partner_mass_mailing_partner(self):
if self.partner_id: if self.partner_id:
self.name = self.partner_id.name self.name = self.partner_id.name
@ -46,8 +50,8 @@ class MailMassMailingContact(models.Model):
record._onchange_partner_mass_mailing_partner() record._onchange_partner_mass_mailing_partner()
new_vals = record._convert_to_write(record._cache) new_vals = record._convert_to_write(record._cache)
new_vals.update( new_vals.update(
subscription_list_ids=vals.get('subscription_list_ids', False), subscription_list_ids=vals.get("subscription_list_ids", False),
list_ids=vals.get('list_ids', False) list_ids=vals.get("list_ids", False),
) )
return super(MailMassMailingContact, self).create(new_vals) return super(MailMassMailingContact, self).create(new_vals)
@ -60,8 +64,8 @@ class MailMassMailingContact(models.Model):
record._onchange_partner_mass_mailing_partner() record._onchange_partner_mass_mailing_partner()
new_vals = record._convert_to_write(record._cache) new_vals = record._convert_to_write(record._cache)
new_vals.update( new_vals.update(
subscription_list_ids=vals.get('subscription_list_ids', False), subscription_list_ids=vals.get("subscription_list_ids", False),
list_ids=vals.get('list_ids', False) list_ids=vals.get("list_ids", False),
) )
super(MailMassMailingContact, contact).write(new_vals) super(MailMassMailingContact, contact).write(new_vals)
return True return True
@ -69,40 +73,42 @@ class MailMassMailingContact(models.Model):
def _get_company(self): def _get_company(self):
company_id = False company_id = False
if self.company_name: if self.company_name:
company_id = self.env['res.company'].search( company_id = (
[('name', '=', self.company_name)]).id self.env["res.company"].search([("name", "=", self.company_name)]).id
)
if not company_id: if not company_id:
company_id = self.env['res.company'].create( company_id = (
{'name': self.company_name}).id self.env["res.company"].create({"name": self.company_name}).id
)
return company_id return company_id
def _get_categories(self): def _get_categories(self):
ca_ids = self.tag_ids.ids + self.list_ids.mapped('partner_category.id') ca_ids = self.tag_ids.ids + self.list_ids.mapped("partner_category.id")
return [[6, 0, ca_ids]] return [[6, 0, ca_ids]]
def _prepare_partner(self): def _prepare_partner(self):
return { return {
'name': self.name or self.email, "name": self.name or self.email,
'email': self.email, "email": self.email,
'country_id': self.country_id.id, "country_id": self.country_id.id,
'title': self.title_id.id, "title": self.title_id.id,
'company_id': self._get_company(), "company_id": self._get_company(),
'category_id': self._get_categories(), "category_id": self._get_categories(),
} }
@api.multi @api.multi
def _set_partner(self): def _set_partner(self):
self.ensure_one() self.ensure_one()
m_partner = self.env['res.partner'] m_partner = self.env["res.partner"]
# Look for a partner with that email # Look for a partner with that email
email = self.email.strip() email = self.email.strip()
partner = m_partner.search([('email', '=ilike', email)], limit=1) partner = m_partner.search([("email", "=ilike", email)], limit=1)
if partner: if partner:
# Partner found # Partner found
self.partner_id = partner self.partner_id = partner
else: else:
lts = self.subscription_list_ids.mapped('list_id') | self.list_ids lts = self.subscription_list_ids.mapped("list_id") | self.list_ids
if lts.filtered('partner_mandatory'): if lts.filtered("partner_mandatory"):
# Create partner # Create partner
partner_vals = self._prepare_partner() partner_vals = self._prepare_partner()
self.partner_id = m_partner.sudo().create(partner_vals) self.partner_id = m_partner.sudo().create(partner_vals)

View File

@ -1,18 +1,19 @@
# Copyright 2018 Tecnativa - Ernesto Tejeda # Copyright 2018 Tecnativa - Ernesto Tejeda
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import api, models, _ from odoo import _, api, models
from odoo.exceptions import ValidationError from odoo.exceptions import ValidationError
class MassMailingContactListRel(models.Model): class MassMailingContactListRel(models.Model):
_inherit = 'mail.mass_mailing.list_contact_rel' _inherit = "mail.mass_mailing.list_contact_rel"
@api.constrains('contact_id', 'list_id') @api.constrains("contact_id", "list_id")
def _check_contact_id_partner_id_list_id(self): def _check_contact_id_partner_id_list_id(self):
for rel in self: for rel in self:
if rel.contact_id.partner_id: if rel.contact_id.partner_id:
contacts = rel.list_id.contact_ids - rel.contact_id contacts = rel.list_id.contact_ids - rel.contact_id
if rel.contact_id.partner_id in contacts.mapped('partner_id'): if rel.contact_id.partner_id in contacts.mapped("partner_id"):
raise ValidationError(_("A partner cannot be multiple " raise ValidationError(
"times in the same list")) _("A partner cannot be multiple " "times in the same list")
)

View File

@ -9,75 +9,85 @@ from odoo.exceptions import ValidationError
class ResPartner(models.Model): class ResPartner(models.Model):
_inherit = 'res.partner' _inherit = "res.partner"
mass_mailing_contact_ids = fields.One2many( mass_mailing_contact_ids = fields.One2many(
string="Mailing contacts", string="Mailing contacts",
comodel_name='mail.mass_mailing.contact', comodel_name="mail.mass_mailing.contact",
inverse_name='partner_id') inverse_name="partner_id",
)
mass_mailing_contacts_count = fields.Integer( mass_mailing_contacts_count = fields.Integer(
string='Mailing contacts number', string="Mailing contacts number",
compute='_compute_mass_mailing_contacts_count', compute="_compute_mass_mailing_contacts_count",
store=True, store=True,
compute_sudo=True) compute_sudo=True,
)
mass_mailing_stats_ids = fields.One2many( mass_mailing_stats_ids = fields.One2many(
string="Mass mailing stats", string="Mass mailing stats",
comodel_name='mail.mail.statistics', comodel_name="mail.mail.statistics",
inverse_name='partner_id') inverse_name="partner_id",
)
mass_mailing_stats_count = fields.Integer( mass_mailing_stats_count = fields.Integer(
string='Mass mailing stats number', string="Mass mailing stats number",
compute='_compute_mass_mailing_stats_count', compute="_compute_mass_mailing_stats_count",
store=True) store=True,
)
@api.constrains('email') @api.constrains("email")
def _check_email_mass_mailing_contacts(self): def _check_email_mass_mailing_contacts(self):
for partner in self: for partner in self:
if not partner.email and partner.sudo().mass_mailing_contact_ids: if not partner.email and partner.sudo().mass_mailing_contact_ids:
raise ValidationError(_( raise ValidationError(
"This partner '%s' is linked to one or more mass " _(
"mailing contact. Email must be assigned." "This partner '%s' is linked to one or more mass "
) % partner.name) "mailing contact. Email must be assigned."
)
% partner.name
)
@api.depends('mass_mailing_contact_ids') @api.depends("mass_mailing_contact_ids")
def _compute_mass_mailing_contacts_count(self): def _compute_mass_mailing_contacts_count(self):
contact_data = self.env['mail.mass_mailing.contact'].read_group( contact_data = self.env["mail.mass_mailing.contact"].read_group(
[('partner_id', 'in', self.ids)], ['partner_id'], ['partner_id']) [("partner_id", "in", self.ids)], ["partner_id"], ["partner_id"]
mapped_data = dict( )
[(contact['partner_id'][0], contact['partner_id_count']) mapped_data = {
for contact in contact_data]) contact["partner_id"][0]: contact["partner_id_count"]
for contact in contact_data
}
for partner in self: for partner in self:
partner.mass_mailing_contacts_count = mapped_data.get(partner.id, partner.mass_mailing_contacts_count = mapped_data.get(partner.id, 0)
0)
@api.depends('mass_mailing_stats_ids') @api.depends("mass_mailing_stats_ids")
def _compute_mass_mailing_stats_count(self): def _compute_mass_mailing_stats_count(self):
contact_data = self.env['mail.mail.statistics'].read_group( contact_data = self.env["mail.mail.statistics"].read_group(
[('partner_id', 'in', self.ids)], ['partner_id'], ['partner_id']) [("partner_id", "in", self.ids)], ["partner_id"], ["partner_id"]
mapped_data = dict( )
[(contact['partner_id'][0], contact['partner_id_count']) mapped_data = {
for contact in contact_data]) contact["partner_id"][0]: contact["partner_id_count"]
for contact in contact_data
}
for partner in self: for partner in self:
partner.mass_mailing_stats_count = mapped_data.get(partner.id, 0) partner.mass_mailing_stats_count = mapped_data.get(partner.id, 0)
def write(self, vals): def write(self, vals):
res = super(ResPartner, self).write(vals) res = super(ResPartner, self).write(vals)
mm_vals = {} mm_vals = {}
if vals.get('name'): if vals.get("name"):
mm_vals['name'] = vals['name'] mm_vals["name"] = vals["name"]
if vals.get('email'): if vals.get("email"):
mm_vals['email'] = vals['email'] mm_vals["email"] = vals["email"]
if vals.get('title'): if vals.get("title"):
mm_vals['title_id'] = vals['title'] mm_vals["title_id"] = vals["title"]
if vals.get('company_id'): if vals.get("company_id"):
company = self.env['res.company'].browse(vals.get('company_id')) company = self.env["res.company"].browse(vals.get("company_id"))
mm_vals['company_name'] = company.name mm_vals["company_name"] = company.name
if vals.get('country_id'): if vals.get("country_id"):
mm_vals['country_id'] = vals['country_id'] mm_vals["country_id"] = vals["country_id"]
if vals.get('category_id'): if vals.get("category_id"):
mm_vals['tag_ids'] = vals['category_id'] mm_vals["tag_ids"] = vals["category_id"]
if mm_vals: if mm_vals:
# Using sudo because ACLs shouldn't produce data inconsistency # Using sudo because ACLs shouldn't produce data inconsistency
self.env["mail.mass_mailing.contact"].sudo().search([ self.env["mail.mass_mailing.contact"].sudo().search(
("partner_id", "in", self.ids), [("partner_id", "in", self.ids)]
]).write(mm_vals) ).write(mm_vals)
return res return res

View File

@ -7,4 +7,4 @@ from . import test_mail_mass_mailing_contact, test_res_partner
from . import test_mail_mail_statistics from . import test_mail_mail_statistics
from . import test_partner_mail_list_wizard from . import test_partner_mail_list_wizard
from . import test_mail_mass_mailing_list from . import test_mail_mass_mailing_list
from . import test_mail_mass_mailing_list_contact_rel from . import test_mail_mass_mailing_list_contact_rel

View File

@ -10,47 +10,57 @@ class BaseCase(TransactionCase):
def setUp(self): def setUp(self):
super(BaseCase, self).setUp() super(BaseCase, self).setUp()
self.main_company = self.env.ref('base.main_company') self.main_company = self.env.ref("base.main_company")
self.country_es = self.env.ref('base.es') self.country_es = self.env.ref("base.es")
self.category_0 = self.env.ref('base.res_partner_category_0') self.category_0 = self.env.ref("base.res_partner_category_0")
self.category_2 = self.env.ref('base.res_partner_category_2') self.category_2 = self.env.ref("base.res_partner_category_2")
self.title_mister = self.env.ref('base.res_partner_title_mister') self.title_mister = self.env.ref("base.res_partner_title_mister")
self.partner = self.create_partner( self.partner = self.create_partner(
{'name': 'Partner test', 'email': 'partner@test.com', {
'title': self.title_mister.id, 'company_id': self.main_company.id, "name": "Partner test",
'country_id': self.country_es.id, "email": "partner@test.com",
'category_id': [(6, 0, (self.category_0 | self.category_2).ids)]}) "title": self.title_mister.id,
"company_id": self.main_company.id,
"country_id": self.country_es.id,
"category_id": [(6, 0, (self.category_0 | self.category_2).ids)],
}
)
self.category_3 = self.env.ref('base.res_partner_category_3') self.category_3 = self.env.ref("base.res_partner_category_3")
self.mailing_list = self.create_mailing_list({'name': 'List test'}) self.mailing_list = self.create_mailing_list({"name": "List test"})
self.mailing_list2 = self.create_mailing_list( self.mailing_list2 = self.create_mailing_list(
{'name': 'List test 2', 'partner_mandatory': True, {
'partner_category': self.category_3.id}) "name": "List test 2",
"partner_mandatory": True,
"partner_category": self.category_3.id,
}
)
def create_partner(self, vals): def create_partner(self, vals):
m_partner = self.env['res.partner'] m_partner = self.env["res.partner"]
return m_partner.create(vals) return m_partner.create(vals)
def create_mailing_contact(self, vals): def create_mailing_contact(self, vals):
m_mailing_contact = self.env['mail.mass_mailing.contact'] m_mailing_contact = self.env["mail.mass_mailing.contact"]
return m_mailing_contact.create(vals) return m_mailing_contact.create(vals)
def create_mailing_list(self, vals): def create_mailing_list(self, vals):
m_mailing_list = self.env['mail.mass_mailing.list'] m_mailing_list = self.env["mail.mass_mailing.list"]
return m_mailing_list.create(vals) return m_mailing_list.create(vals)
def check_mailing_contact_partner(self, mailing_contact): def check_mailing_contact_partner(self, mailing_contact):
if mailing_contact.partner_id: if mailing_contact.partner_id:
self.assertEqual(mailing_contact.partner_id.email, self.assertEqual(mailing_contact.partner_id.email, mailing_contact.email)
mailing_contact.email) self.assertEqual(mailing_contact.partner_id.name, mailing_contact.name)
self.assertEqual(mailing_contact.partner_id.name, self.assertEqual(mailing_contact.partner_id.title, mailing_contact.title_id)
mailing_contact.name)
self.assertEqual(mailing_contact.partner_id.title,
mailing_contact.title_id)
if mailing_contact.partner_id.company_id: if mailing_contact.partner_id.company_id:
self.assertEqual(mailing_contact.partner_id.company_id.name, self.assertEqual(
mailing_contact.company_name) mailing_contact.partner_id.company_id.name,
self.assertEqual(mailing_contact.partner_id.country_id, mailing_contact.company_name,
mailing_contact.country_id) )
self.assertEqual(mailing_contact.partner_id.category_id, self.assertEqual(
mailing_contact.tag_ids) mailing_contact.partner_id.country_id, mailing_contact.country_id
)
self.assertEqual(
mailing_contact.partner_id.category_id, mailing_contact.tag_ids
)

View File

@ -7,26 +7,23 @@ from . import base
class MailMailStatisticsCase(base.BaseCase): class MailMailStatisticsCase(base.BaseCase):
def test_link_partner(self): def test_link_partner(self):
partner = self.create_partner( partner = self.create_partner({"name": "Test partner"})
{'name': 'Test partner'}) stat = self.env["mail.mail.statistics"].create(
stat = self.env['mail.mail.statistics'].create({ {"model": "res.partner", "res_id": partner.id}
'model': 'res.partner', )
'res_id': partner.id,
})
self.assertEqual(partner.id, stat.partner_id.id) self.assertEqual(partner.id, stat.partner_id.id)
def test_link_mail_contact(self): def test_link_mail_contact(self):
partner = self.create_partner( partner = self.create_partner(
{'name': 'Test partner', 'email': 'test@domain.com'}) {"name": "Test partner", "email": "test@domain.com"}
)
contact_vals = { contact_vals = {
'partner_id': partner.id, "partner_id": partner.id,
'list_ids': [[6, 0, [self.mailing_list.id]]] "list_ids": [[6, 0, [self.mailing_list.id]]],
} }
contact = self.create_mailing_contact(contact_vals) contact = self.create_mailing_contact(contact_vals)
stat = self.env['mail.mail.statistics'].create({ stat = self.env["mail.mail.statistics"].create(
'model': 'mail.mass_mailing.contact', {"model": "mail.mass_mailing.contact", "res_id": contact.id}
'res_id': contact.id, )
})
self.assertEqual(partner.id, stat.partner_id.id) self.assertEqual(partner.id, stat.partner_id.id)

View File

@ -3,90 +3,105 @@
# Copyright 2015 Javier Iniesta <javieria@antiun.com> # Copyright 2015 Javier Iniesta <javieria@antiun.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import base
from ..hooks import post_init_hook
from odoo.exceptions import ValidationError from odoo.exceptions import ValidationError
from ..hooks import post_init_hook
from . import base
class MailMassMailingContactCase(base.BaseCase): class MailMassMailingContactCase(base.BaseCase):
def test_match_existing_contacts(self): def test_match_existing_contacts(self):
contact = self.create_mailing_contact( contact = self.create_mailing_contact(
{'email': 'partner@test.com', {"email": "partner@test.com", "list_ids": [(6, 0, self.mailing_list.ids)]}
'list_ids': [(6, 0, self.mailing_list.ids)]}) )
post_init_hook(self.cr, self.registry) post_init_hook(self.cr, self.registry)
self.assertEqual(contact.partner_id.id, self.partner.id) self.assertEqual(contact.partner_id.id, self.partner.id)
self.check_mailing_contact_partner(contact) self.check_mailing_contact_partner(contact)
def test_create_mass_mailing_contact(self): def test_create_mass_mailing_contact(self):
title_doctor = self.env.ref('base.res_partner_title_doctor') title_doctor = self.env.ref("base.res_partner_title_doctor")
country_cu = self.env.ref('base.cu') country_cu = self.env.ref("base.cu")
category_8 = self.env.ref('base.res_partner_category_8') category_8 = self.env.ref("base.res_partner_category_8")
category_11 = self.env.ref('base.res_partner_category_11') category_11 = self.env.ref("base.res_partner_category_11")
contact_vals = { contact_vals = {
'name': 'Partner test 2', 'email': 'partner2@test.com', "name": "Partner test 2",
'title_id': title_doctor.id, 'company_name': "TestCompany", "email": "partner2@test.com",
'country_id': country_cu.id, "title_id": title_doctor.id,
'tag_ids': [(6, 0, (category_8 | category_11).ids)], "company_name": "TestCompany",
'list_ids': [(6, 0, (self.mailing_list | self.mailing_list2).ids)], "country_id": country_cu.id,
"tag_ids": [(6, 0, (category_8 | category_11).ids)],
"list_ids": [(6, 0, (self.mailing_list | self.mailing_list2).ids)],
} }
contact = self.create_mailing_contact(contact_vals) contact = self.create_mailing_contact(contact_vals)
self.check_mailing_contact_partner(contact) self.check_mailing_contact_partner(contact)
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
self.create_mailing_contact( self.create_mailing_contact(
{'email': 'partner2@test.com', {
'list_ids': [[6, 0, [self.mailing_list2.id]]]}) "email": "partner2@test.com",
"list_ids": [[6, 0, [self.mailing_list2.id]]],
}
)
def test_create_mass_mailing_contact_with_subscription(self): def test_create_mass_mailing_contact_with_subscription(self):
title_doctor = self.env.ref('base.res_partner_title_doctor') title_doctor = self.env.ref("base.res_partner_title_doctor")
country_cu = self.env.ref('base.cu') country_cu = self.env.ref("base.cu")
category_8 = self.env.ref('base.res_partner_category_8') category_8 = self.env.ref("base.res_partner_category_8")
category_11 = self.env.ref('base.res_partner_category_11') category_11 = self.env.ref("base.res_partner_category_11")
contact_vals = { contact_vals = {
'name': 'Partner test 2', 'email': 'partner2@test.com', "name": "Partner test 2",
'title_id': title_doctor.id, 'company_name': "TestCompany", "email": "partner2@test.com",
'country_id': country_cu.id, "title_id": title_doctor.id,
'tag_ids': [(6, 0, (category_8 | category_11).ids)], "company_name": "TestCompany",
'subscription_list_ids': [ "country_id": country_cu.id,
(0, 0, {'list_id': self.mailing_list.id}), "tag_ids": [(6, 0, (category_8 | category_11).ids)],
(0, 0, {'list_id': self.mailing_list2.id}), "subscription_list_ids": [
(0, 0, {"list_id": self.mailing_list.id}),
(0, 0, {"list_id": self.mailing_list2.id}),
], ],
} }
contact = self.create_mailing_contact(contact_vals) contact = self.create_mailing_contact(contact_vals)
self.check_mailing_contact_partner(contact) self.check_mailing_contact_partner(contact)
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
self.create_mailing_contact({ self.create_mailing_contact(
'email': 'partner2@test.com', {
'subscription_list_ids': [ "email": "partner2@test.com",
(0, 0, {'list_id': self.mailing_list2.id}), "subscription_list_ids": [
], (0, 0, {"list_id": self.mailing_list2.id})
}) ],
}
)
def test_write_mass_mailing_contact(self): def test_write_mass_mailing_contact(self):
contact = self.create_mailing_contact( contact = self.create_mailing_contact(
{'email': 'partner@test.com', {"email": "partner@test.com", "list_ids": [(6, 0, self.mailing_list.ids)]}
'list_ids': [(6, 0, self.mailing_list.ids)]}) )
contact.write({'partner_id': False}) contact.write({"partner_id": False})
self.check_mailing_contact_partner(contact) self.check_mailing_contact_partner(contact)
contact2 = self.create_mailing_contact( contact2 = self.create_mailing_contact(
{'email': 'partner2@test.com', 'name': 'Partner test 2', {
'list_ids': [(6, 0, self.mailing_list.ids)]}) "email": "partner2@test.com",
contact2.write({'partner_id': False}) "name": "Partner test 2",
"list_ids": [(6, 0, self.mailing_list.ids)],
}
)
contact2.write({"partner_id": False})
self.assertFalse(contact2.partner_id) self.assertFalse(contact2.partner_id)
def test_onchange_partner(self): def test_onchange_partner(self):
contact = self.create_mailing_contact( contact = self.create_mailing_contact(
{'email': 'partner@test.com', {"email": "partner@test.com", "list_ids": [[6, 0, [self.mailing_list.id]]]}
'list_ids': [[6, 0, [self.mailing_list.id]]]}) )
title_doctor = self.env.ref('base.res_partner_title_doctor') title_doctor = self.env.ref("base.res_partner_title_doctor")
country_cu = self.env.ref('base.cu') country_cu = self.env.ref("base.cu")
category_8 = self.env.ref('base.res_partner_category_8') category_8 = self.env.ref("base.res_partner_category_8")
category_11 = self.env.ref('base.res_partner_category_11') category_11 = self.env.ref("base.res_partner_category_11")
partner_vals = { partner_vals = {
'name': 'Partner test 2', 'email': 'partner2@test.com', "name": "Partner test 2",
'title': title_doctor.id, 'company_id': self.main_company.id, "email": "partner2@test.com",
'country_id': country_cu.id, "title": title_doctor.id,
'category_id': [(6, 0, (category_8 | category_11).ids)], "company_id": self.main_company.id,
"country_id": country_cu.id,
"category_id": [(6, 0, (category_8 | category_11).ids)],
} }
partner = self.create_partner(partner_vals) partner = self.create_partner(partner_vals)
with self.env.do_in_onchange(): with self.env.do_in_onchange():

View File

@ -1,41 +1,41 @@
# Copyright 2018 Tecnativa - Ernesto tejeda # Copyright 2018 Tecnativa - Ernesto tejeda
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import base
from odoo.exceptions import ValidationError from odoo.exceptions import ValidationError
from . import base
class MailMassMailingListCase(base.BaseCase): class MailMassMailingListCase(base.BaseCase):
def test_create_mass_mailing_list(self): def test_create_mass_mailing_list(self):
contact_test_1 = self.create_mailing_contact({ contact_test_1 = self.create_mailing_contact(
'name': 'Contact test 1', {"name": "Contact test 1", "partner_id": self.partner.id}
'partner_id': self.partner.id, )
}) contact_test_2 = self.create_mailing_contact(
contact_test_2 = self.create_mailing_contact({ {"name": "Contact test 2", "partner_id": self.partner.id}
'name': 'Contact test 2', )
'partner_id': self.partner.id,
})
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
self.create_mailing_list({ self.create_mailing_list(
'name': 'List test 3', {
'contact_ids': [(6, 0, (contact_test_1 | contact_test_2).ids)] "name": "List test 3",
}) "contact_ids": [(6, 0, (contact_test_1 | contact_test_2).ids)],
}
)
def test_create_mass_mailing_list_with_subscription(self): def test_create_mass_mailing_list_with_subscription(self):
contact_test_1 = self.create_mailing_contact({ contact_test_1 = self.create_mailing_contact(
'name': 'Contact test 1', {"name": "Contact test 1", "partner_id": self.partner.id}
'partner_id': self.partner.id, )
}) contact_test_2 = self.create_mailing_contact(
contact_test_2 = self.create_mailing_contact({ {"name": "Contact test 2", "partner_id": self.partner.id}
'name': 'Contact test 2', )
'partner_id': self.partner.id,
})
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
self.create_mailing_list({ self.create_mailing_list(
'name': 'List test 3', {
'subscription_contact_ids': [ "name": "List test 3",
(0, 0, {'contact_id': contact_test_1.id}), "subscription_contact_ids": [
(0, 0, {'contact_id': contact_test_2.id}), (0, 0, {"contact_id": contact_test_1.id}),
] (0, 0, {"contact_id": contact_test_2.id}),
}) ],
}
)

View File

@ -1,30 +1,26 @@
# Copyright 2018 Tecnativa - Ernesto tejeda # Copyright 2018 Tecnativa - Ernesto tejeda
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import base
from odoo.exceptions import ValidationError from odoo.exceptions import ValidationError
from . import base
class MailMassMailingListContactRelCase(base.BaseCase): class MailMassMailingListContactRelCase(base.BaseCase):
def test_create_mass_mailing_list(self): def test_create_mass_mailing_list(self):
contact_test_1 = self.create_mailing_contact({ contact_test_1 = self.create_mailing_contact(
'name': 'Contact test 1', {"name": "Contact test 1", "partner_id": self.partner.id}
'partner_id': self.partner.id, )
}) contact_test_2 = self.create_mailing_contact(
contact_test_2 = self.create_mailing_contact({ {"name": "Contact test 2", "partner_id": self.partner.id}
'name': 'Contact test 2', )
'partner_id': self.partner.id, list_3 = self.create_mailing_list({"name": "List test 3"})
})
list_3 = self.create_mailing_list({'name': 'List test 3'})
self.env['mail.mass_mailing.list_contact_rel'].create({ self.env["mail.mass_mailing.list_contact_rel"].create(
'list_id': list_3.id, {"list_id": list_3.id, "contact_id": contact_test_1.id}
'contact_id': contact_test_1.id, )
})
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
self.env['mail.mass_mailing.list_contact_rel'].create({ self.env["mail.mass_mailing.list_contact_rel"].create(
'list_id': list_3.id, {"list_id": list_3.id, "contact_id": contact_test_2.id}
'contact_id': contact_test_2.id, )
})

View File

@ -3,36 +3,40 @@
# Copyright 2015 Javier Iniesta <javieria@antiun.com> # Copyright 2015 Javier Iniesta <javieria@antiun.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import base
from odoo.exceptions import UserError from odoo.exceptions import UserError
from . import base
class PartnerMailListWizardCase(base.BaseCase): class PartnerMailListWizardCase(base.BaseCase):
def test_add_to_mail_list(self): def test_add_to_mail_list(self):
wizard = self.env['partner.mail.list.wizard'].create( wizard = self.env["partner.mail.list.wizard"].create(
{'mail_list_id': self.mailing_list.id}) {"mail_list_id": self.mailing_list.id}
)
wizard.partner_ids = [self.partner.id] wizard.partner_ids = [self.partner.id]
wizard.add_to_mail_list() wizard.add_to_mail_list()
contacts = self.env['mail.mass_mailing.contact'].search([ contacts = self.env["mail.mass_mailing.contact"].search(
('partner_id', '=', self.partner.id)]) [("partner_id", "=", self.partner.id)]
)
cont = contacts.filtered(lambda r: wizard.mail_list_id in r.list_ids) cont = contacts.filtered(lambda r: wizard.mail_list_id in r.list_ids)
self.assertEqual(len(cont), 1) self.assertEqual(len(cont), 1)
self.check_mailing_contact_partner(cont) self.check_mailing_contact_partner(cont)
# This line does not create a new contact # This line does not create a new contact
wizard.add_to_mail_list() wizard.add_to_mail_list()
self.assertEqual(len(self.partner.mass_mailing_contact_ids), 1) self.assertEqual(len(self.partner.mass_mailing_contact_ids), 1)
self.assertEqual(self.partner.mass_mailing_contact_ids.list_ids, self.assertEqual(
self.mailing_list) self.partner.mass_mailing_contact_ids.list_ids, self.mailing_list
)
list_2 = self.create_mailing_list({'name': 'New list'}) list_2 = self.create_mailing_list({"name": "New list"})
wizard.mail_list_id = list_2 wizard.mail_list_id = list_2
wizard.add_to_mail_list() wizard.add_to_mail_list()
self.assertEqual(len(self.partner.mass_mailing_contact_ids), 1) self.assertEqual(len(self.partner.mass_mailing_contact_ids), 1)
self.assertEqual(self.partner.mass_mailing_contact_ids.list_ids, self.assertEqual(
self.mailing_list | list_2) self.partner.mass_mailing_contact_ids.list_ids, self.mailing_list | list_2
)
partner = self.env['res.partner'].create({'name': 'No email partner'}) partner = self.env["res.partner"].create({"name": "No email partner"})
wizard.partner_ids = [partner.id] wizard.partner_ids = [partner.id]
with self.assertRaises(UserError): with self.assertRaises(UserError):
wizard.add_to_mail_list() wizard.add_to_mail_list()

View File

@ -3,37 +3,41 @@
# Copyright 2015 Javier Iniesta <javieria@antiun.com> # Copyright 2015 Javier Iniesta <javieria@antiun.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import base
from odoo.exceptions import ValidationError from odoo.exceptions import ValidationError
from . import base
class ResPartnerCase(base.BaseCase): class ResPartnerCase(base.BaseCase):
def test_count_mass_mailing_contacts(self): def test_count_mass_mailing_contacts(self):
self.create_mailing_contact( self.create_mailing_contact(
{'email': 'partner@test.com', {"email": "partner@test.com", "list_ids": [[6, 0, [self.mailing_list.id]]]}
'list_ids': [[6, 0, [self.mailing_list.id]]]}) )
self.create_mailing_contact( self.create_mailing_contact(
{'email': 'partner@test.com', {"email": "partner@test.com", "list_ids": [[6, 0, [self.mailing_list2.id]]]}
'list_ids': [[6, 0, [self.mailing_list2.id]]]}) )
self.assertEqual(self.partner.mass_mailing_contacts_count, 2) self.assertEqual(self.partner.mass_mailing_contacts_count, 2)
def test_write_res_partner(self): def test_write_res_partner(self):
contact = self.create_mailing_contact( contact = self.create_mailing_contact(
{'email': 'partner@test.com', {"email": "partner@test.com", "list_ids": [[6, 0, [self.mailing_list.id]]]}
'list_ids': [[6, 0, [self.mailing_list.id]]]}) )
self.assertEqual(self.partner, contact.partner_id) self.assertEqual(self.partner, contact.partner_id)
title_doctor = self.env.ref('base.res_partner_title_doctor') title_doctor = self.env.ref("base.res_partner_title_doctor")
country_cu = self.env.ref('base.cu') country_cu = self.env.ref("base.cu")
category_8 = self.env.ref('base.res_partner_category_8') category_8 = self.env.ref("base.res_partner_category_8")
category_11 = self.env.ref('base.res_partner_category_11') category_11 = self.env.ref("base.res_partner_category_11")
self.partner.write({ self.partner.write(
'name': 'Changed', 'email': 'partner@changed.com', {
'title': title_doctor.id, 'company_id': self.main_company.id, "name": "Changed",
'country_id': country_cu.id, "email": "partner@changed.com",
'category_id': [(6, 0, (category_8 | category_11).ids)], "title": title_doctor.id,
}) "company_id": self.main_company.id,
"country_id": country_cu.id,
"category_id": [(6, 0, (category_8 | category_11).ids)],
}
)
self.check_mailing_contact_partner(contact) self.check_mailing_contact_partner(contact)
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
self.partner.write({'email': False}) self.partner.write({"email": False})

View File

@ -11,18 +11,21 @@ class PartnerMailListWizard(models.TransientModel):
_name = "partner.mail.list.wizard" _name = "partner.mail.list.wizard"
_description = "Create contact mailing list" _description = "Create contact mailing list"
mail_list_id = fields.Many2one(comodel_name="mail.mass_mailing.list", mail_list_id = fields.Many2one(
string="Mailing List") comodel_name="mail.mass_mailing.list", string="Mailing List"
)
partner_ids = fields.Many2many( partner_ids = fields.Many2many(
comodel_name="res.partner", relation="mail_list_wizard_partner", comodel_name="res.partner",
default=lambda self: self.env.context.get("active_ids")) relation="mail_list_wizard_partner",
default=lambda self: self.env.context.get("active_ids"),
)
@api.multi @api.multi
def add_to_mail_list(self): def add_to_mail_list(self):
contact_obj = self.env['mail.mass_mailing.contact'] contact_obj = self.env["mail.mass_mailing.contact"]
partners = self.partner_ids partners = self.partner_ids
add_list = partners.filtered('mass_mailing_contact_ids') add_list = partners.filtered("mass_mailing_contact_ids")
for partner in add_list: for partner in add_list:
partner.mass_mailing_contact_ids[0].list_ids |= self.mail_list_id partner.mass_mailing_contact_ids[0].list_ids |= self.mail_list_id
@ -31,11 +34,11 @@ class PartnerMailListWizard(models.TransientModel):
if not partner.email: if not partner.email:
raise UserError(_("Partner '%s' has no email.") % partner.name) raise UserError(_("Partner '%s' has no email.") % partner.name)
contact_vals = { contact_vals = {
'partner_id': partner.id, "partner_id": partner.id,
'list_ids': [[6, 0, [self.mail_list_id.id]]], "list_ids": [[6, 0, [self.mail_list_id.id]]],
'title_id': partner.title or False, "title_id": partner.title or False,
'company_name': partner.company_id.name or False, "company_name": partner.company_id.name or False,
'country_id': partner.country_id or False, "country_id": partner.country_id or False,
'tag_ids': partner.category_id or False, "tag_ids": partner.category_id or False,
} }
contact_obj.create(contact_vals) contact_obj.create(contact_vals)