Browse Source

mass_mailing_partner: Improve wizard behavior

For each partner, if already has a contact it's added to the selected
list, otherwise a new one is created
pull/323/head
ernesto 6 years ago
committed by Pedro M. Baeza
parent
commit
3699250182
  1. 1
      mass_mailing_partner/README.rst
  2. 13
      mass_mailing_partner/models/res_partner.py
  3. 1
      mass_mailing_partner/readme/CONTRIBUTORS.rst
  4. 1
      mass_mailing_partner/static/description/index.html
  5. 11
      mass_mailing_partner/tests/test_partner_mail_list_wizard.py
  6. 33
      mass_mailing_partner/wizard/partner_mail_list_wizard.py

1
mass_mailing_partner/README.rst

@ -85,6 +85,7 @@ Contributors
* Jairo Llopis
* David Vidal
* Ernesto Tejeda
* Victor M.M. Torres
Maintainers
~~~~~~~~~~~

13
mass_mailing_partner/models/res_partner.py

@ -13,18 +13,21 @@ class ResPartner(models.Model):
mass_mailing_contact_ids = fields.One2many(
string="Mailing contacts",
oldname="mass_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', store=True,
compute='_compute_mass_mailing_contacts_count',
store=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)
compute='_compute_mass_mailing_stats_count',
store=True)
@api.constrains('email')
def _check_email_mass_mailing_contacts(self):

1
mass_mailing_partner/readme/CONTRIBUTORS.rst

@ -7,3 +7,4 @@
* Jairo Llopis
* David Vidal
* Ernesto Tejeda
* Victor M.M. Torres

1
mass_mailing_partner/static/description/index.html

@ -430,6 +430,7 @@ If you spotted it first, help us smashing it by providing a detailed and welcome
<li>Jairo Llopis</li>
<li>David Vidal</li>
<li>Ernesto Tejeda</li>
<li>Victor M.M. Torres</li>
</ul>
</blockquote>
</li>

11
mass_mailing_partner/tests/test_partner_mail_list_wizard.py

@ -21,6 +21,17 @@ class PartnerMailListWizardCase(base.BaseCase):
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)
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)
partner = self.env['res.partner'].create({'name': 'No email partner'})
wizard.partner_ids = [partner.id]
with self.assertRaises(UserError):

33
mass_mailing_partner/wizard/partner_mail_list_wizard.py

@ -20,21 +20,22 @@ class PartnerMailListWizard(models.TransientModel):
@api.multi
def add_to_mail_list(self):
contact_obj = self.env['mail.mass_mailing.contact']
for partner in self.partner_ids:
partners = self.partner_ids
add_list = partners.filtered(lambda r: r.mass_mailing_contact_ids)
if add_list:
add_list.mass_mailing_contact_ids[0].list_ids |= self.mail_list_id
to_create = partners - add_list
for partner in to_create:
if not partner.email:
raise UserError(_("Partner '%s' has no email.") % partner.name)
contact = contact_obj.search([('partner_id', '=', partner.id)])
if self.mail_list_id not in contact.mapped('list_ids'):
contact_vals = {
'partner_id': partner.id,
'list_ids': [[6, 0, [self.mail_list_id.id]]]
}
if partner.title:
contact_vals['title_id'] = partner.title.id
if partner.company_id:
contact_vals['company_name'] = partner.company_id.name
if partner.country_id:
contact_vals['country_id'] = partner.country_id.id
if partner.category_id:
contact_vals['tag_ids'] = partner.category_id.ids
contact_obj.create(contact_vals)
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,
}
contact_obj.create(contact_vals)
Loading…
Cancel
Save