[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",
"version": "12.0.1.0.2",
"author": "Tecnativa, "
"Odoo Community Association (OCA)",
"author": "Tecnativa, " "Odoo Community Association (OCA)",
"website": "https://github.com/OCA/social",
"license": "AGPL-3",
"category": "Marketing",
"depends": [
'mass_mailing',
],
"depends": ["mass_mailing"],
"post_init_hook": "post_init_hook",
'data': [
'views/mail_mail_statistics_view.xml',
'views/mail_mass_mailing_contact_view.xml',
'views/mail_mass_mailing_view.xml',
'views/res_partner_view.xml',
'wizard/partner_mail_list_wizard.xml'
"data": [
"views/mail_mail_statistics_view.xml",
"views/mail_mass_mailing_contact_view.xml",
"views/mail_mass_mailing_view.xml",
"views/res_partner_view.xml",
"wizard/partner_mail_list_wizard.xml",
],
"installable": True,
}

View File

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

View File

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

View File

@ -3,29 +3,31 @@
# Copyright 2015 Javier Iniesta <javieria@antiun.com>
# 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
class MailMassMailingList(models.Model):
_inherit = 'mail.mass_mailing.list'
_inherit = "mail.mass_mailing.list"
partner_mandatory = fields.Boolean(string="Mandatory Partner",
default=False)
partner_category = fields.Many2one(comodel_name='res.partner.category',
string="Partner Tag")
partner_mandatory = fields.Boolean(string="Mandatory Partner", default=False)
partner_category = fields.Many2one(
comodel_name="res.partner.category", string="Partner Tag"
)
@api.constrains('contact_ids')
@api.constrains("contact_ids")
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:
data = contact_obj.read_group(
[
('id', 'in', mailing_list.contact_ids.ids),
('partner_id', '!=', False),
("id", "in", mailing_list.contact_ids.ids),
("partner_id", "!=", False),
],
['partner_id'], ['partner_id']
["partner_id"],
["partner_id"],
)
if len(list(filter(lambda r: r['partner_id_count'] > 1, data))):
raise ValidationError(_("A partner cannot be multiple times "
"in the same list"))
if len(list(filter(lambda r: r["partner_id_count"] > 1, data))):
raise ValidationError(
_("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):
_inherit = 'mail.mass_mailing.contact'
_inherit = "mail.mass_mailing.contact"
partner_id = fields.Many2one(comodel_name='res.partner', string="Partner",
domain=[('email', '!=', False)])
partner_id = fields.Many2one(
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):
for contact in self:
if contact.partner_id:
other_contact = self.search([
('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"))
other_contact = self.search(
[
("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")
)
@api.onchange('partner_id')
@api.onchange("partner_id")
def _onchange_partner_mass_mailing_partner(self):
if self.partner_id:
self.name = self.partner_id.name
@ -46,8 +50,8 @@ class MailMassMailingContact(models.Model):
record._onchange_partner_mass_mailing_partner()
new_vals = record._convert_to_write(record._cache)
new_vals.update(
subscription_list_ids=vals.get('subscription_list_ids', False),
list_ids=vals.get('list_ids', False)
subscription_list_ids=vals.get("subscription_list_ids", False),
list_ids=vals.get("list_ids", False),
)
return super(MailMassMailingContact, self).create(new_vals)
@ -60,8 +64,8 @@ class MailMassMailingContact(models.Model):
record._onchange_partner_mass_mailing_partner()
new_vals = record._convert_to_write(record._cache)
new_vals.update(
subscription_list_ids=vals.get('subscription_list_ids', False),
list_ids=vals.get('list_ids', False)
subscription_list_ids=vals.get("subscription_list_ids", False),
list_ids=vals.get("list_ids", False),
)
super(MailMassMailingContact, contact).write(new_vals)
return True
@ -69,40 +73,42 @@ class MailMassMailingContact(models.Model):
def _get_company(self):
company_id = False
if self.company_name:
company_id = self.env['res.company'].search(
[('name', '=', self.company_name)]).id
company_id = (
self.env["res.company"].search([("name", "=", self.company_name)]).id
)
if not company_id:
company_id = self.env['res.company'].create(
{'name': self.company_name}).id
company_id = (
self.env["res.company"].create({"name": self.company_name}).id
)
return company_id
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]]
def _prepare_partner(self):
return {
'name': self.name or self.email,
'email': self.email,
'country_id': self.country_id.id,
'title': self.title_id.id,
'company_id': self._get_company(),
'category_id': self._get_categories(),
"name": self.name or self.email,
"email": self.email,
"country_id": self.country_id.id,
"title": self.title_id.id,
"company_id": self._get_company(),
"category_id": self._get_categories(),
}
@api.multi
def _set_partner(self):
self.ensure_one()
m_partner = self.env['res.partner']
m_partner = self.env["res.partner"]
# Look for a partner with that email
email = self.email.strip()
partner = m_partner.search([('email', '=ilike', email)], limit=1)
partner = m_partner.search([("email", "=ilike", email)], limit=1)
if partner:
# Partner found
self.partner_id = partner
else:
lts = self.subscription_list_ids.mapped('list_id') | self.list_ids
if lts.filtered('partner_mandatory'):
lts = self.subscription_list_ids.mapped("list_id") | self.list_ids
if lts.filtered("partner_mandatory"):
# Create partner
partner_vals = self._prepare_partner()
self.partner_id = m_partner.sudo().create(partner_vals)

View File

@ -1,18 +1,19 @@
# Copyright 2018 Tecnativa - Ernesto Tejeda
# 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
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):
for rel in self:
if rel.contact_id.partner_id:
contacts = rel.list_id.contact_ids - rel.contact_id
if rel.contact_id.partner_id in contacts.mapped('partner_id'):
raise ValidationError(_("A partner cannot be multiple "
"times in the same list"))
if rel.contact_id.partner_id in contacts.mapped("partner_id"):
raise ValidationError(
_("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):
_inherit = 'res.partner'
_inherit = "res.partner"
mass_mailing_contact_ids = fields.One2many(
string="Mailing contacts",
comodel_name='mail.mass_mailing.contact',
inverse_name='partner_id')
comodel_name="mail.mass_mailing.contact",
inverse_name="partner_id",
)
mass_mailing_contacts_count = fields.Integer(
string='Mailing contacts number',
compute='_compute_mass_mailing_contacts_count',
string="Mailing contacts number",
compute="_compute_mass_mailing_contacts_count",
store=True,
compute_sudo=True)
compute_sudo=True,
)
mass_mailing_stats_ids = fields.One2many(
string="Mass mailing stats",
comodel_name='mail.mail.statistics',
inverse_name='partner_id')
comodel_name="mail.mail.statistics",
inverse_name="partner_id",
)
mass_mailing_stats_count = fields.Integer(
string='Mass mailing stats number',
compute='_compute_mass_mailing_stats_count',
store=True)
string="Mass mailing stats number",
compute="_compute_mass_mailing_stats_count",
store=True,
)
@api.constrains('email')
@api.constrains("email")
def _check_email_mass_mailing_contacts(self):
for partner in self:
if not partner.email and partner.sudo().mass_mailing_contact_ids:
raise ValidationError(_(
"This partner '%s' is linked to one or more mass "
"mailing contact. Email must be assigned."
) % partner.name)
raise ValidationError(
_(
"This partner '%s' is linked to one or more mass "
"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):
contact_data = self.env['mail.mass_mailing.contact'].read_group(
[('partner_id', 'in', self.ids)], ['partner_id'], ['partner_id'])
mapped_data = dict(
[(contact['partner_id'][0], contact['partner_id_count'])
for contact in contact_data])
contact_data = self.env["mail.mass_mailing.contact"].read_group(
[("partner_id", "in", self.ids)], ["partner_id"], ["partner_id"]
)
mapped_data = {
contact["partner_id"][0]: contact["partner_id_count"]
for contact in contact_data
}
for partner in self:
partner.mass_mailing_contacts_count = mapped_data.get(partner.id,
0)
partner.mass_mailing_contacts_count = mapped_data.get(partner.id, 0)
@api.depends('mass_mailing_stats_ids')
@api.depends("mass_mailing_stats_ids")
def _compute_mass_mailing_stats_count(self):
contact_data = self.env['mail.mail.statistics'].read_group(
[('partner_id', 'in', self.ids)], ['partner_id'], ['partner_id'])
mapped_data = dict(
[(contact['partner_id'][0], contact['partner_id_count'])
for contact in contact_data])
contact_data = self.env["mail.mail.statistics"].read_group(
[("partner_id", "in", self.ids)], ["partner_id"], ["partner_id"]
)
mapped_data = {
contact["partner_id"][0]: contact["partner_id_count"]
for contact in contact_data
}
for partner in self:
partner.mass_mailing_stats_count = mapped_data.get(partner.id, 0)
def write(self, vals):
res = super(ResPartner, self).write(vals)
mm_vals = {}
if vals.get('name'):
mm_vals['name'] = vals['name']
if vals.get('email'):
mm_vals['email'] = vals['email']
if vals.get('title'):
mm_vals['title_id'] = vals['title']
if vals.get('company_id'):
company = self.env['res.company'].browse(vals.get('company_id'))
mm_vals['company_name'] = company.name
if vals.get('country_id'):
mm_vals['country_id'] = vals['country_id']
if vals.get('category_id'):
mm_vals['tag_ids'] = vals['category_id']
if vals.get("name"):
mm_vals["name"] = vals["name"]
if vals.get("email"):
mm_vals["email"] = vals["email"]
if vals.get("title"):
mm_vals["title_id"] = vals["title"]
if vals.get("company_id"):
company = self.env["res.company"].browse(vals.get("company_id"))
mm_vals["company_name"] = company.name
if vals.get("country_id"):
mm_vals["country_id"] = vals["country_id"]
if vals.get("category_id"):
mm_vals["tag_ids"] = vals["category_id"]
if mm_vals:
# Using sudo because ACLs shouldn't produce data inconsistency
self.env["mail.mass_mailing.contact"].sudo().search([
("partner_id", "in", self.ids),
]).write(mm_vals)
self.env["mail.mass_mailing.contact"].sudo().search(
[("partner_id", "in", self.ids)]
).write(mm_vals)
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_partner_mail_list_wizard
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):
super(BaseCase, self).setUp()
self.main_company = self.env.ref('base.main_company')
self.country_es = self.env.ref('base.es')
self.category_0 = self.env.ref('base.res_partner_category_0')
self.category_2 = self.env.ref('base.res_partner_category_2')
self.title_mister = self.env.ref('base.res_partner_title_mister')
self.main_company = self.env.ref("base.main_company")
self.country_es = self.env.ref("base.es")
self.category_0 = self.env.ref("base.res_partner_category_0")
self.category_2 = self.env.ref("base.res_partner_category_2")
self.title_mister = self.env.ref("base.res_partner_title_mister")
self.partner = self.create_partner(
{'name': 'Partner test', 'email': 'partner@test.com',
'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)]})
{
"name": "Partner test",
"email": "partner@test.com",
"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.mailing_list = self.create_mailing_list({'name': 'List test'})
self.category_3 = self.env.ref("base.res_partner_category_3")
self.mailing_list = self.create_mailing_list({"name": "List test"})
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):
m_partner = self.env['res.partner']
m_partner = self.env["res.partner"]
return m_partner.create(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)
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)
def check_mailing_contact_partner(self, mailing_contact):
if mailing_contact.partner_id:
self.assertEqual(mailing_contact.partner_id.email,
mailing_contact.email)
self.assertEqual(mailing_contact.partner_id.name,
mailing_contact.name)
self.assertEqual(mailing_contact.partner_id.title,
mailing_contact.title_id)
self.assertEqual(mailing_contact.partner_id.email, mailing_contact.email)
self.assertEqual(mailing_contact.partner_id.name, mailing_contact.name)
self.assertEqual(mailing_contact.partner_id.title, mailing_contact.title_id)
if mailing_contact.partner_id.company_id:
self.assertEqual(mailing_contact.partner_id.company_id.name,
mailing_contact.company_name)
self.assertEqual(mailing_contact.partner_id.country_id,
mailing_contact.country_id)
self.assertEqual(mailing_contact.partner_id.category_id,
mailing_contact.tag_ids)
self.assertEqual(
mailing_contact.partner_id.company_id.name,
mailing_contact.company_name,
)
self.assertEqual(
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):
def test_link_partner(self):
partner = self.create_partner(
{'name': 'Test partner'})
stat = self.env['mail.mail.statistics'].create({
'model': 'res.partner',
'res_id': partner.id,
})
partner = self.create_partner({"name": "Test partner"})
stat = self.env["mail.mail.statistics"].create(
{"model": "res.partner", "res_id": partner.id}
)
self.assertEqual(partner.id, stat.partner_id.id)
def test_link_mail_contact(self):
partner = self.create_partner(
{'name': 'Test partner', 'email': 'test@domain.com'})
{"name": "Test partner", "email": "test@domain.com"}
)
contact_vals = {
'partner_id': partner.id,
'list_ids': [[6, 0, [self.mailing_list.id]]]
"partner_id": partner.id,
"list_ids": [[6, 0, [self.mailing_list.id]]],
}
contact = self.create_mailing_contact(contact_vals)
stat = self.env['mail.mail.statistics'].create({
'model': 'mail.mass_mailing.contact',
'res_id': contact.id,
})
stat = self.env["mail.mail.statistics"].create(
{"model": "mail.mass_mailing.contact", "res_id": contact.id}
)
self.assertEqual(partner.id, stat.partner_id.id)

View File

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

View File

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

View File

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

View File

@ -3,37 +3,41 @@
# Copyright 2015 Javier Iniesta <javieria@antiun.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import base
from odoo.exceptions import ValidationError
from . import base
class ResPartnerCase(base.BaseCase):
def test_count_mass_mailing_contacts(self):
self.create_mailing_contact(
{'email': 'partner@test.com',
'list_ids': [[6, 0, [self.mailing_list.id]]]})
{"email": "partner@test.com", "list_ids": [[6, 0, [self.mailing_list.id]]]}
)
self.create_mailing_contact(
{'email': 'partner@test.com',
'list_ids': [[6, 0, [self.mailing_list2.id]]]})
{"email": "partner@test.com", "list_ids": [[6, 0, [self.mailing_list2.id]]]}
)
self.assertEqual(self.partner.mass_mailing_contacts_count, 2)
def test_write_res_partner(self):
contact = self.create_mailing_contact(
{'email': 'partner@test.com',
'list_ids': [[6, 0, [self.mailing_list.id]]]})
{"email": "partner@test.com", "list_ids": [[6, 0, [self.mailing_list.id]]]}
)
self.assertEqual(self.partner, contact.partner_id)
title_doctor = self.env.ref('base.res_partner_title_doctor')
country_cu = self.env.ref('base.cu')
category_8 = self.env.ref('base.res_partner_category_8')
category_11 = self.env.ref('base.res_partner_category_11')
self.partner.write({
'name': 'Changed', 'email': 'partner@changed.com',
'title': title_doctor.id, 'company_id': self.main_company.id,
'country_id': country_cu.id,
'category_id': [(6, 0, (category_8 | category_11).ids)],
})
title_doctor = self.env.ref("base.res_partner_title_doctor")
country_cu = self.env.ref("base.cu")
category_8 = self.env.ref("base.res_partner_category_8")
category_11 = self.env.ref("base.res_partner_category_11")
self.partner.write(
{
"name": "Changed",
"email": "partner@changed.com",
"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)
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"
_description = "Create contact mailing list"
mail_list_id = fields.Many2one(comodel_name="mail.mass_mailing.list",
string="Mailing List")
mail_list_id = fields.Many2one(
comodel_name="mail.mass_mailing.list", string="Mailing List"
)
partner_ids = fields.Many2many(
comodel_name="res.partner", relation="mail_list_wizard_partner",
default=lambda self: self.env.context.get("active_ids"))
comodel_name="res.partner",
relation="mail_list_wizard_partner",
default=lambda self: self.env.context.get("active_ids"),
)
@api.multi
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
add_list = partners.filtered('mass_mailing_contact_ids')
add_list = partners.filtered("mass_mailing_contact_ids")
for partner in add_list:
partner.mass_mailing_contact_ids[0].list_ids |= self.mail_list_id
@ -31,11 +34,11 @@ class PartnerMailListWizard(models.TransientModel):
if not partner.email:
raise UserError(_("Partner '%s' has no email.") % partner.name)
contact_vals = {
'partner_id': partner.id,
'list_ids': [[6, 0, [self.mail_list_id.id]]],
'title_id': partner.title or False,
'company_name': partner.company_id.name or False,
'country_id': partner.country_id or False,
'tag_ids': partner.category_id or False,
"partner_id": partner.id,
"list_ids": [[6, 0, [self.mail_list_id.id]]],
"title_id": partner.title or False,
"company_name": partner.company_id.name or False,
"country_id": partner.country_id or False,
"tag_ids": partner.category_id or False,
}
contact_obj.create(contact_vals)