[IMP] mass_mailing: better management of contacts lists
This commit is contained in:
@@ -17,17 +17,17 @@
|
||||
<record id="mass_mail_contact_1" model="mail.mass_mailing.contact">
|
||||
<field name="name">Aristide Antario</field>
|
||||
<field name="email">aa@example.com</field>
|
||||
<field name="list_id" ref="mass_mailing.mass_mail_list_2"/>
|
||||
<field name="list_ids" eval="[(6,0,[ref('mass_mailing.mass_mail_list_2')])]"/>
|
||||
</record>
|
||||
<record id="mass_mail_contact_2" model="mail.mass_mailing.contact">
|
||||
<field name="name">Beverly Bridge</field>
|
||||
<field name="email">bb@example.com</field>
|
||||
<field name="list_id" ref="mass_mailing.mass_mail_list_2"/>
|
||||
<field name="list_ids" eval="[(6,0,[ref('mass_mailing.mass_mail_list_2'), ref('mass_mailing.mass_mail_list_2')])]"/>
|
||||
</record>
|
||||
<record id="mass_mail_contact_3" model="mail.mass_mailing.contact">
|
||||
<field name="name">Carol Cartridge</field>
|
||||
<field name="email">cc@example.com</field>
|
||||
<field name="list_id" ref="mass_mailing.mass_mail_list_2"/>
|
||||
<field name="list_ids" eval="[(6,0,[ref('mass_mailing.mass_mail_list_2')])]"/>
|
||||
<field name="opt_out" eval="True"/>
|
||||
</record>
|
||||
|
||||
|
||||
@@ -37,11 +37,22 @@ class MassMailingList(models.Model):
|
||||
create_date = fields.Datetime(string='Creation Date')
|
||||
contact_nbr = fields.Integer(compute="_compute_contact_nbr", string='Number of Contacts')
|
||||
|
||||
# Compute number of contacts non opt-out for a mailing list
|
||||
def _compute_contact_nbr(self):
|
||||
contacts_data = self.env['mail.mass_mailing.contact'].read_group([('list_id', 'in', self.ids), ('opt_out', '!=', True)], ['list_id'], ['list_id'])
|
||||
mapped_data = dict([(c['list_id'][0], c['list_id_count']) for c in contacts_data])
|
||||
self.env.cr.execute('''
|
||||
select
|
||||
list_id, count(*)
|
||||
from
|
||||
mail_mass_mailing_contact_list_rel r
|
||||
left join mail_mass_mailing_contact c on (r.contact_id=c.id)
|
||||
where
|
||||
c.opt_out <> true
|
||||
group by
|
||||
list_id
|
||||
''')
|
||||
data = dict(self.env.cr.fetchall())
|
||||
for mailing_list in self:
|
||||
mailing_list.contact_nbr = mapped_data.get(mailing_list.id, 0)
|
||||
mailing_list.contact_nbr = data.get(mailing_list.id, 0)
|
||||
|
||||
class MassMailingContact(models.Model):
|
||||
"""Model of a contact. This model is different from the partner model
|
||||
@@ -55,14 +66,18 @@ class MassMailingContact(models.Model):
|
||||
_rec_name = 'email'
|
||||
|
||||
name = fields.Char()
|
||||
company_name = fields.Char(string='Company Name')
|
||||
title_id = fields.Many2one('res.partner.title', string='Title')
|
||||
email = fields.Char(required=True)
|
||||
create_date = fields.Datetime(string='Create Date')
|
||||
list_id = fields.Many2one(
|
||||
'mail.mass_mailing.list', string='Mailing List',
|
||||
ondelete='cascade', required=True, default=lambda self: self.env['mail.mass_mailing.list'].search([], limit=1, order='id desc'))
|
||||
create_date = fields.Datetime(string='Creation Date')
|
||||
list_ids = fields.Many2many(
|
||||
'mail.mass_mailing.list', 'mail_mass_mailing_contact_list_rel',
|
||||
'contact_id', 'list_id', string='Mailing Lists')
|
||||
opt_out = fields.Boolean(string='Opt Out', help='The contact has chosen not to receive mails anymore from this list')
|
||||
unsubscription_date = fields.Datetime(string='Unsubscription Date')
|
||||
message_bounce = fields.Integer(string='Bounce', help='Counter of the number of bounced emails for this contact.')
|
||||
message_bounce = fields.Integer(string='Bounced', help='Counter of the number of bounced emails for this contact.')
|
||||
country_id = fields.Many2one('res.country', string='Country')
|
||||
tag_ids = fields.Many2many('res.partner.category', string='Tags')
|
||||
|
||||
@api.model
|
||||
def create(self, vals):
|
||||
@@ -93,7 +108,7 @@ class MassMailingContact(models.Model):
|
||||
@api.model
|
||||
def add_to_list(self, name, list_id):
|
||||
name, email = self.get_name_email(name)
|
||||
contact = self.create({'name': name, 'email': email, 'list_id': list_id})
|
||||
contact = self.create({'name': name, 'email': email, 'list_ids': [(4, list_id)]})
|
||||
return contact.name_get()[0]
|
||||
|
||||
@api.multi
|
||||
@@ -432,9 +447,9 @@ class MassMailing(models.Model):
|
||||
def _onchange_model_and_list(self):
|
||||
if self.mailing_model == 'mail.mass_mailing.contact':
|
||||
if self.contact_list_ids:
|
||||
self.mailing_domain = "[('list_id', 'in', %s), ('opt_out', '=', False)]" % self.contact_list_ids.ids
|
||||
self.mailing_domain = "[('list_ids', 'in', [%s]), ('opt_out', '=', False)]" % (','.join(map(str,self.contact_list_ids.ids)),)
|
||||
else:
|
||||
self.mailing_domain = "[('list_id', '=', False)]"
|
||||
self.mailing_domain = "[(0, '=', 1)]"
|
||||
elif 'opt_out' in self.env[self.mailing_model]._fields:
|
||||
self.mailing_domain = "[('opt_out', '=', False)]"
|
||||
else:
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
<menuitem name="Mass Mailing" id="mass_mailing_menu_root" sequence="60" web_icon="mass_mailing,static/description/icon.png"/>
|
||||
<menuitem name="Mailings" id="mass_mailing_menu"
|
||||
parent="mass_mailing_menu_root" sequence="1" groups="mass_mailing.group_mass_mailing_user"/>
|
||||
<menuitem name="Contacts" id="mass_mailing_contact_menu"
|
||||
parent="mass_mailing_menu_root" sequence="2" groups="mass_mailing.group_mass_mailing_user"/>
|
||||
<!-- Marketing / Configuration -->
|
||||
<menuitem name="Configuration" id="mass_mailing_configuration"
|
||||
parent="mass_mailing_menu_root" sequence="100"/>
|
||||
@@ -16,33 +18,32 @@
|
||||
<field name="name">List Subscription</field>
|
||||
<field name="model_id">mail.mass_mailing.contact</field>
|
||||
<field name="user_id" eval="False"/>
|
||||
<field name="context">{'group_by': ['create_date:month'], 'col_group_by': ['list_id']}</field>
|
||||
<field name="context">{'group_by': ['create_date:month']}</field>
|
||||
</record>
|
||||
<record id="filter_contact_unsubscription" model="ir.filters">
|
||||
<field name="name">List Unsubscription</field>
|
||||
<field name="model_id">mail.mass_mailing.contact</field>
|
||||
<field name="user_id" eval="False"/>
|
||||
<field name="domain">[('opt_out','=',True)]</field>
|
||||
<field name="context">{'group_by': ['unsubscription_date:month'], 'col_group_by': ['list_id']}</field>
|
||||
<field name="context">{'group_by': ['unsubscription_date:month']}</field>
|
||||
</record>
|
||||
|
||||
<record model="ir.ui.view" id="view_mail_mass_mailing_contact_search">
|
||||
<field name="name">mail.mass_mailing.contact.search</field>
|
||||
<field name="model">mail.mass_mailing.contact</field>
|
||||
<field name="arch" type="xml">
|
||||
<search string="Mailing Lists Subscribers">
|
||||
<field name="name"/>
|
||||
<field name="email"/>
|
||||
<field name="list_id"/>
|
||||
<search string="Contacts">
|
||||
<field name="name" filter_domain="['|','|',('name','ilike',self),('company_name','ilike',self), ('email','ilike',self)]"
|
||||
string="Name / Email"/>
|
||||
<field name="tag_ids"/>
|
||||
<field name="list_ids"/>
|
||||
<separator/>
|
||||
<filter string="Exclude Opt Out" name="not_opt_out" domain="[('opt_out', '=', False)]"/>
|
||||
<separator/>
|
||||
<filter string="Exclude Dead" name="not_bounced" domain="[('message_bounce', '<', 5)]"/>
|
||||
<filter string="Exclude Bounced" name="not_bounced" domain="[('message_bounce', '<', 3)]"/>
|
||||
<group expand="0" string="Group By">
|
||||
<filter string="Creation Date" name="group_create_date"
|
||||
context="{'group_by': 'create_date'}"/>
|
||||
<filter string="Mailing Lists" name="group_list_id"
|
||||
context="{'group_by': 'list_id'}"/>
|
||||
</group>
|
||||
</search>
|
||||
</field>
|
||||
@@ -53,35 +54,85 @@
|
||||
<field name="model">mail.mass_mailing.contact</field>
|
||||
<field name="priority">10</field>
|
||||
<field name="arch" type="xml">
|
||||
<tree string="Mailing Lists Subscribers" editable="top">
|
||||
<field name="email"/>
|
||||
<field name="name"/>
|
||||
<field name="list_id"/>
|
||||
<field name="message_bounce"/>
|
||||
<tree string="Contacts">
|
||||
<field name="create_date"/>
|
||||
<field name="unsubscription_date" readonly="1"/>
|
||||
<field name="name"/>
|
||||
<field name="company_name"/>
|
||||
<field name="email"/>
|
||||
<field name="message_bounce"/>
|
||||
<field name="opt_out"/>
|
||||
</tree>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record model="ir.ui.view" id="view_mail_mass_mailing_contact_form">
|
||||
<field name="name">mail.mass_mailing.contact.form</field>
|
||||
<field name="model">mail.mass_mailing.contact</field>
|
||||
<field name="priority">10</field>
|
||||
<field name="arch" type="xml">
|
||||
<form string="Contacts">
|
||||
<sheet>
|
||||
<div class="oe_title">
|
||||
<label for="name" class="oe_edit_only"/>
|
||||
<h1>
|
||||
<field name="name" placeholder="e.g. John Smith"/>
|
||||
</h1>
|
||||
<label for="tag_ids" class="oe_edit_only"/>
|
||||
<div>
|
||||
<field name="tag_ids" widget="many2many_tags" style="width: 100%%"/>
|
||||
</div>
|
||||
</div>
|
||||
<group>
|
||||
<group>
|
||||
<field name="email"/>
|
||||
<field name="title_id"/>
|
||||
<field name="company_name"/>
|
||||
<field name="country_id"/>
|
||||
</group>
|
||||
<group>
|
||||
<field name="create_date" readonly="1"/>
|
||||
<field name="unsubscription_date"/>
|
||||
<label for="message_bounce"/>
|
||||
<div class="o_row">
|
||||
<field name="message_bounce"/>
|
||||
<span> times</span>
|
||||
</div>
|
||||
<field name="opt_out"/>
|
||||
</group>
|
||||
</group>
|
||||
<field name="list_ids"/>
|
||||
</sheet>
|
||||
</form>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record model="ir.ui.view" id="view_mail_mass_mailing_contact_pivot">
|
||||
<field name="name">mail.mass_mailing.contact.pivot</field>
|
||||
<field name="model">mail.mass_mailing.contact</field>
|
||||
<field name="priority">10</field>
|
||||
<field name="arch" type="xml">
|
||||
<pivot string="Contacts" type="pivot" stacked="True">
|
||||
<field name="create_date" type="row"/>
|
||||
</pivot>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record model="ir.ui.view" id="view_mail_mass_mailing_contact_graph">
|
||||
<field name="name">mail.mass_mailing.contact.graph</field>
|
||||
<field name="model">mail.mass_mailing.contact</field>
|
||||
<field name="priority">10</field>
|
||||
<field name="arch" type="xml">
|
||||
<graph string="Mailing Lists Subscriber" type="pivot" stacked="True">
|
||||
<graph string="Contacts" type="pivot" stacked="True">
|
||||
<field name="create_date" type="row"/>
|
||||
<field name="list_id" type="col"/>
|
||||
</graph>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record model="ir.actions.act_window" id="action_view_mass_mailing_contacts">
|
||||
<field name="name">Mailing List Subscribers</field>
|
||||
<field name="name">Contacts</field>
|
||||
<field name="res_model">mail.mass_mailing.contact</field>
|
||||
<field name="view_type">form</field>
|
||||
<field name="view_mode">tree,graph</field>
|
||||
<field name="view_mode">tree,form,graph,pivot</field>
|
||||
<field name="context">{'search_default_not_opt_out': 1}</field>
|
||||
</record>
|
||||
|
||||
@@ -90,7 +141,8 @@
|
||||
<field name="res_model">mail.mass_mailing.contact</field>
|
||||
<field name="view_type">form</field>
|
||||
<field name="view_mode">tree,form,graph</field>
|
||||
<field name="context">{'search_default_list_id': active_id, 'search_default_not_opt_out': 1}</field>
|
||||
<field name="domain">[('list_ids', 'in', [active_id])]</field>
|
||||
<field name="context">{'search_default_not_opt_out': 1}</field>
|
||||
<field name="help" type="html">
|
||||
<p class="oe_view_nocontent_create">
|
||||
Click to create a recipient.
|
||||
@@ -99,7 +151,7 @@
|
||||
</record>
|
||||
|
||||
<menuitem name="Contacts" id="menu_email_mass_mailing_contacts"
|
||||
parent="mass_mailing_menu" sequence="4" groups="base.group_no_one"
|
||||
parent="mass_mailing_contact_menu" sequence="4"
|
||||
action="action_view_mass_mailing_contacts"/>
|
||||
|
||||
<!-- MASS MAILING LIST -->
|
||||
@@ -175,7 +227,7 @@
|
||||
</record>
|
||||
|
||||
<menuitem name="Mailing Lists" id="menu_email_mass_mailing_lists"
|
||||
parent="mass_mailing_menu" sequence="3"
|
||||
parent="mass_mailing_contact_menu" sequence="3"
|
||||
action="action_view_mass_mailing_lists"/>
|
||||
|
||||
<!-- MASS MAILING !-->
|
||||
|
||||
@@ -38,7 +38,7 @@ class MassMailController(MassMailController):
|
||||
|
||||
is_subscriber = False
|
||||
if email:
|
||||
contacts_count = request.env['mail.mass_mailing.contact'].sudo().search_count([('list_id', '=', int(list_id)), ('email', '=', email), ('opt_out', '=', False)])
|
||||
contacts_count = request.env['mail.mass_mailing.contact'].sudo().search_count([('list_ids', 'in', [int(list_id)]), ('email', '=', email), ('opt_out', '=', False)])
|
||||
is_subscriber = contacts_count > 0
|
||||
|
||||
return {'is_subscriber': is_subscriber, 'email': email}
|
||||
@@ -49,12 +49,12 @@ class MassMailController(MassMailController):
|
||||
name, email = Contacts.get_name_email(email)
|
||||
|
||||
contact_ids = Contacts.search([
|
||||
('list_id', '=', int(list_id)),
|
||||
('list_ids', 'in', [int(list_id)]),
|
||||
('email', '=', email),
|
||||
], limit=1)
|
||||
if not contact_ids:
|
||||
# inline add_to_list as we've already called half of it
|
||||
Contacts.create({'name': name, 'email': email, 'list_id': int(list_id)})
|
||||
Contacts.create({'name': name, 'email': email, 'list_ids': [(6,0,[int(list_id)])]})
|
||||
elif contact_ids.opt_out:
|
||||
contact_ids.opt_out = False
|
||||
# add email to session
|
||||
|
||||
@@ -22,7 +22,9 @@
|
||||
<li class="list-group-item">
|
||||
<input type="checkbox" class="mail_list_checkbox" name="contact_ids"
|
||||
t-att-value="contact['id']" t-att-checked="'checked' if contact['opt_out'] == False else None"/>
|
||||
<t t-esc="contact.list_id.name"/>
|
||||
<t t-foreach="contact.list_ids" t-as="contact_list">
|
||||
<t t-esc="contact_list.name"/>
|
||||
</t>
|
||||
</li>
|
||||
</t>
|
||||
</ul>
|
||||
|
||||
Reference in New Issue
Block a user