2013-08-07 13:03:34 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2013-today OpenERP SA (<http://www.openerp.com>)
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
2013-09-10 15:29:33 +00:00
|
|
|
from datetime import datetime
|
2013-09-03 14:59:36 +00:00
|
|
|
from dateutil import relativedelta
|
2014-04-01 13:24:54 +00:00
|
|
|
import random
|
2014-03-21 17:21:39 +00:00
|
|
|
import urllib
|
|
|
|
import urlparse
|
2013-09-03 14:59:36 +00:00
|
|
|
|
|
|
|
from openerp import tools
|
2014-03-24 17:10:17 +00:00
|
|
|
from openerp.exceptions import Warning
|
2014-03-14 16:51:13 +00:00
|
|
|
from openerp.tools.safe_eval import safe_eval as eval
|
2013-09-12 10:09:09 +00:00
|
|
|
from openerp.tools.translate import _
|
2013-08-07 13:03:34 +00:00
|
|
|
from openerp.osv import osv, fields
|
|
|
|
|
|
|
|
|
2014-03-14 16:51:13 +00:00
|
|
|
class MassMailingCategory(osv.Model):
|
|
|
|
"""Model of categories of mass mailing, i.e. marketing, newsletter, ... """
|
|
|
|
_name = 'mail.mass_mailing.category'
|
|
|
|
_description = 'Mass Mailing Category'
|
|
|
|
|
|
|
|
_columns = {
|
|
|
|
'name': fields.char('Name', required=True),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class MassMailingContact(osv.Model):
|
|
|
|
"""Model of a contact. This model is different from the partner model
|
|
|
|
because it holds only some basic information: name, email. The purpose is to
|
|
|
|
be able to deal with large contact list to email without bloating the partner
|
|
|
|
database. """
|
|
|
|
_name = 'mail.mass_mailing.contact'
|
|
|
|
_description = 'Mass Mailing Contact'
|
|
|
|
|
|
|
|
_columns = {
|
|
|
|
'name': fields.char('Name', required=True),
|
|
|
|
'email': fields.char('Email', required=True),
|
|
|
|
'list_id': fields.many2one(
|
|
|
|
'mail.mass_mailing.list', string='Mailing List',
|
2014-03-31 11:07:56 +00:00
|
|
|
domain=[('model', '=', 'mail.mass_mailing.contact')],
|
2014-03-18 16:41:17 +00:00
|
|
|
ondelete='cascade',
|
2014-03-14 16:51:13 +00:00
|
|
|
),
|
|
|
|
'opt_out': fields.boolean('Opt Out', help='The contact has chosen not to receive news anymore from this mailing list'),
|
|
|
|
}
|
|
|
|
|
2014-03-18 16:41:17 +00:00
|
|
|
def name_create(self, cr, uid, name, context=None):
|
|
|
|
name, email = self.pool['res.partner']._parse_partner_name(name, context=context)
|
|
|
|
if name and not email:
|
|
|
|
email = name
|
|
|
|
if email and not name:
|
|
|
|
name = email
|
|
|
|
rec_id = self.create(cr, uid, {'name': name, 'email': email}, context=context)
|
|
|
|
return self.name_get(cr, uid, [rec_id], context)[0]
|
|
|
|
|
2014-03-14 16:51:13 +00:00
|
|
|
|
|
|
|
class MassMailingList(osv.Model):
|
|
|
|
"""Model of a contact list. """
|
|
|
|
_name = 'mail.mass_mailing.list'
|
|
|
|
_description = 'Contact List'
|
|
|
|
|
|
|
|
def default_get(self, cr, uid, fields, context=None):
|
|
|
|
"""Override default_get to handle active_domain coming from the list view. """
|
|
|
|
res = super(MassMailingList, self).default_get(cr, uid, fields, context=context)
|
2014-03-18 13:04:28 +00:00
|
|
|
if 'domain' in fields:
|
2014-03-27 15:46:06 +00:00
|
|
|
if not 'model' in res and context.get('active_model'):
|
|
|
|
res['model'] = context['active_model']
|
2014-03-18 13:04:28 +00:00
|
|
|
if 'active_domain' in context:
|
|
|
|
res['domain'] = '%s' % context['active_domain']
|
|
|
|
elif 'active_ids' in context:
|
|
|
|
res['domain'] = '%s' % [('id', 'in', context['active_ids'])]
|
|
|
|
else:
|
2014-03-18 16:41:17 +00:00
|
|
|
res['domain'] = False
|
2014-03-14 16:51:13 +00:00
|
|
|
return res
|
|
|
|
|
|
|
|
def _get_contact_nbr(self, cr, uid, ids, name, arg, context=None):
|
|
|
|
"""Compute the number of contacts linked to the mailing list. """
|
|
|
|
results = dict.fromkeys(ids, 0)
|
|
|
|
for contact_list in self.browse(cr, uid, ids, context=context):
|
2014-03-18 13:04:28 +00:00
|
|
|
results[contact_list.id] = self.pool[contact_list.model].search(
|
|
|
|
cr, uid,
|
2014-03-18 16:41:17 +00:00
|
|
|
self._get_domain(cr, uid, [contact_list.id], context=context)[contact_list.id],
|
2014-03-18 13:04:28 +00:00
|
|
|
count=True, context=context
|
|
|
|
)
|
2014-03-14 16:51:13 +00:00
|
|
|
return results
|
|
|
|
|
|
|
|
def _get_model_list(self, cr, uid, context=None):
|
2014-03-17 17:16:12 +00:00
|
|
|
return self.pool['mail.mass_mailing']._get_mailing_model(cr, uid, context=context)
|
2014-03-14 16:51:13 +00:00
|
|
|
|
|
|
|
# indirections for inheritance
|
|
|
|
_model_list = lambda self, *args, **kwargs: self._get_model_list(*args, **kwargs)
|
|
|
|
|
|
|
|
_columns = {
|
|
|
|
'name': fields.char('Name', required=True),
|
|
|
|
'contact_nbr': fields.function(
|
|
|
|
_get_contact_nbr, type='integer',
|
|
|
|
string='Contact Number',
|
|
|
|
),
|
|
|
|
'model': fields.selection(
|
|
|
|
_model_list, type='char', required=True,
|
|
|
|
string='Applies To'
|
|
|
|
),
|
|
|
|
'filter_id': fields.many2one(
|
|
|
|
'ir.filters', string='Custom Filter',
|
2014-03-18 13:04:28 +00:00
|
|
|
domain="[('model_id.model', '=', model)]",
|
2014-03-14 16:51:13 +00:00
|
|
|
),
|
|
|
|
'domain': fields.text('Domain'),
|
|
|
|
}
|
|
|
|
|
|
|
|
def on_change_model(self, cr, uid, ids, model, context=None):
|
2014-03-31 11:07:56 +00:00
|
|
|
return {'value': {'filter_id': False}}
|
2014-03-14 16:51:13 +00:00
|
|
|
|
|
|
|
def on_change_filter_id(self, cr, uid, ids, filter_id, context=None):
|
|
|
|
values = {}
|
|
|
|
if filter_id:
|
|
|
|
ir_filter = self.pool['ir.filters'].browse(cr, uid, filter_id, context=context)
|
|
|
|
values['domain'] = ir_filter.domain
|
|
|
|
return {'value': values}
|
|
|
|
|
2014-03-20 17:04:55 +00:00
|
|
|
def on_change_domain(self, cr, uid, ids, domain, model, context=None):
|
|
|
|
if domain is False:
|
|
|
|
return {'value': {'contact_nbr': 0}}
|
|
|
|
else:
|
|
|
|
domain = eval(domain)
|
|
|
|
return {'value': {'contact_nbr': self.pool[model].search(cr, uid, domain, context=context, count=True)}}
|
|
|
|
|
2014-03-31 11:07:56 +00:00
|
|
|
def create(self, cr, uid, values, context=None):
|
|
|
|
new_id = super(MassMailingList, self).create(cr, uid, values, context=context)
|
|
|
|
if values.get('model') == 'mail.mass_mailing.contact':
|
|
|
|
domain = values.get('domain')
|
|
|
|
if domain is None or domain is False:
|
|
|
|
return new_id
|
|
|
|
contact_ids = self.pool['mail.mass_mailing.contact'].search(cr, uid, eval(domain), context=context)
|
|
|
|
self.pool['mail.mass_mailing.contact'].write(cr, uid, contact_ids, {'list_id': new_id}, context=context)
|
|
|
|
self.pool['mail.mass_mailing.list'].write(cr, uid, [new_id], {'domain': [('list_id', '=', new_id)]}, context=context)
|
|
|
|
return new_id
|
|
|
|
|
2014-03-14 16:51:13 +00:00
|
|
|
def action_see_records(self, cr, uid, ids, context=None):
|
|
|
|
contact_list = self.browse(cr, uid, ids[0], context=context)
|
|
|
|
ctx = dict(context)
|
|
|
|
ctx['search_default_not_opt_out'] = True
|
|
|
|
return {
|
|
|
|
'name': _('See Contact List'),
|
|
|
|
'type': 'ir.actions.act_window',
|
|
|
|
'view_type': 'form',
|
|
|
|
'view_mode': 'tree,form',
|
|
|
|
'res_model': contact_list.model,
|
|
|
|
'views': [(False, 'tree'), (False, 'form')],
|
|
|
|
'view_id': False,
|
|
|
|
'target': 'current',
|
|
|
|
'context': ctx,
|
|
|
|
'domain': contact_list.domain,
|
|
|
|
}
|
|
|
|
|
2014-03-18 13:04:28 +00:00
|
|
|
def action_add_to_mailing(self, cr, uid, ids, context=None):
|
|
|
|
mass_mailing_id = context.get('default_mass_mailing_id')
|
|
|
|
if not mass_mailing_id:
|
|
|
|
return False
|
|
|
|
self.pool['mail.mass_mailing'].write(cr, uid, [mass_mailing_id], {'contact_list_ids': [(4, list_id) for list_id in ids]}, context=context)
|
|
|
|
return {
|
|
|
|
'name': _('New Mass Mailing'),
|
|
|
|
'type': 'ir.actions.act_window',
|
|
|
|
'view_type': 'form',
|
|
|
|
'view_mode': 'form',
|
|
|
|
'res_model': 'mail.mass_mailing',
|
|
|
|
'res_id': mass_mailing_id,
|
|
|
|
'context': context,
|
|
|
|
}
|
|
|
|
|
|
|
|
def _get_domain(self, cr, uid, ids, context=None):
|
2014-03-31 11:07:56 +00:00
|
|
|
# todo: model-based method + check opt_out field exists
|
2014-03-18 13:04:28 +00:00
|
|
|
domains = {}
|
2014-03-14 16:51:13 +00:00
|
|
|
for contact_list in self.browse(cr, uid, ids, context=context):
|
2014-03-18 13:04:28 +00:00
|
|
|
if contact_list.model == 'mail.mass_mailing.contact':
|
|
|
|
domain = [('list_id', '=', contact_list.id)]
|
2014-03-31 11:07:56 +00:00
|
|
|
elif contact_list.domain is False or contact_list.domain is None: # domain is a string like False or None -> void list
|
2014-03-18 13:04:28 +00:00
|
|
|
domain = [('id', '=', '0')]
|
|
|
|
else:
|
|
|
|
domain = eval(contact_list.domain)
|
|
|
|
domains[contact_list.id] = domain
|
|
|
|
return domains
|
|
|
|
|
|
|
|
def get_global_domain(self, cr, uid, ids, context=None):
|
|
|
|
model_to_domains = dict((mailing_model[0], list())
|
|
|
|
for mailing_model in self.pool['mail.mass_mailing']._get_mailing_model(cr, uid, context=context))
|
|
|
|
for contact_list in self.browse(cr, uid, ids, context=context):
|
|
|
|
domain = self._get_domain(cr, uid, [contact_list.id], context=context)[contact_list.id]
|
2014-03-14 16:51:13 +00:00
|
|
|
if domain is not False:
|
2014-03-18 13:04:28 +00:00
|
|
|
model_to_domains[contact_list.model].append(domain)
|
|
|
|
for model, domains in model_to_domains.iteritems():
|
|
|
|
if domains:
|
|
|
|
final_domain = ['|'] * (len(domains) - 1) + [leaf for dom in domains for leaf in dom]
|
|
|
|
else:
|
|
|
|
final_domain = [('id', '=', '0')]
|
|
|
|
model_to_domains[model] = final_domain
|
|
|
|
return model_to_domains
|
2014-03-14 16:51:13 +00:00
|
|
|
|
|
|
|
|
2013-08-07 13:03:34 +00:00
|
|
|
class MassMailingCampaign(osv.Model):
|
2014-03-18 13:04:28 +00:00
|
|
|
"""Model of mass mailing campaigns. """
|
2013-08-07 13:03:34 +00:00
|
|
|
_name = "mail.mass_mailing.campaign"
|
|
|
|
_description = 'Mass Mailing Campaign'
|
2013-09-17 10:35:55 +00:00
|
|
|
# number of embedded mailings in kanban view
|
|
|
|
_kanban_mailing_nbr = 4
|
2013-08-07 13:03:34 +00:00
|
|
|
|
|
|
|
def _get_statistics(self, cr, uid, ids, name, arg, context=None):
|
|
|
|
""" Compute statistics of the mass mailing campaign """
|
2014-03-17 16:09:19 +00:00
|
|
|
Statistics = self.pool['mail.mail.statistics']
|
2013-08-07 13:03:34 +00:00
|
|
|
results = dict.fromkeys(ids, False)
|
2014-03-17 16:09:19 +00:00
|
|
|
for cid in ids:
|
|
|
|
results[cid] = {
|
|
|
|
'total': Statistics.search(cr, uid, [('mass_mailing_campaign_id', '=', cid)], count=True, context=context),
|
2014-04-01 13:53:52 +00:00
|
|
|
'scheduled': Statistics.search(cr, uid, [('mass_mailing_campaign_id', '=', cid), ('scheduled', '!=', False), ('sent', '=', False)], count=True, context=context),
|
2014-03-17 16:09:19 +00:00
|
|
|
'sent': Statistics.search(cr, uid, [('mass_mailing_campaign_id', '=', cid), ('sent', '!=', False)], count=True, context=context),
|
|
|
|
'opened': Statistics.search(cr, uid, [('mass_mailing_campaign_id', '=', cid), ('opened', '!=', False)], count=True, context=context),
|
|
|
|
'replied': Statistics.search(cr, uid, [('mass_mailing_campaign_id', '=', cid), ('replied', '!=', False)], count=True, context=context),
|
|
|
|
'bounced': Statistics.search(cr, uid, [('mass_mailing_campaign_id', '=', cid), ('bounced', '!=', False)], count=True, context=context),
|
2013-08-07 13:03:34 +00:00
|
|
|
}
|
2014-03-17 16:09:19 +00:00
|
|
|
results[cid]['delivered'] = results[cid]['sent'] - results[cid]['bounced']
|
2013-09-03 14:59:36 +00:00
|
|
|
return results
|
|
|
|
|
2014-03-14 16:51:13 +00:00
|
|
|
def _get_state_list(self, cr, uid, context=None):
|
|
|
|
return [('draft', 'Schedule'), ('design', 'Design'), ('done', 'Sent')]
|
|
|
|
|
|
|
|
# indirections for inheritance
|
|
|
|
_state = lambda self, *args, **kwargs: self._get_state_list(*args, **kwargs)
|
|
|
|
|
2013-08-07 13:03:34 +00:00
|
|
|
_columns = {
|
2014-03-17 16:09:19 +00:00
|
|
|
'name': fields.char('Name', required=True),
|
|
|
|
'state': fields.selection(_state, string='Status', required=True),
|
2013-09-13 11:54:08 +00:00
|
|
|
'user_id': fields.many2one(
|
|
|
|
'res.users', 'Responsible',
|
|
|
|
required=True,
|
2013-09-03 14:59:36 +00:00
|
|
|
),
|
2014-03-14 16:51:13 +00:00
|
|
|
'category_id': fields.many2one(
|
|
|
|
'mail.mass_mailing.category', 'Category',
|
|
|
|
help='Category'),
|
2013-09-13 11:54:08 +00:00
|
|
|
'mass_mailing_ids': fields.one2many(
|
|
|
|
'mail.mass_mailing', 'mass_mailing_campaign_id',
|
|
|
|
'Mass Mailings',
|
2013-09-03 14:59:36 +00:00
|
|
|
),
|
2014-04-01 13:24:54 +00:00
|
|
|
'ab_testing': fields.boolean(
|
|
|
|
'AB Testing',
|
|
|
|
help='If checked, recipients will be mailed only once, allowing to send'
|
|
|
|
'various mailings in a single campaign to test the effectiveness'
|
|
|
|
'of the mailings.'),
|
2013-09-10 10:46:23 +00:00
|
|
|
'color': fields.integer('Color Index'),
|
2013-09-03 14:59:36 +00:00
|
|
|
# stat fields
|
2014-03-17 16:09:19 +00:00
|
|
|
'total': fields.function(
|
2014-04-01 13:53:52 +00:00
|
|
|
_get_statistics, string='Total',
|
|
|
|
type='integer', multi='_get_statistics'
|
|
|
|
),
|
|
|
|
'scheduled': fields.function(
|
|
|
|
_get_statistics, string='Total',
|
2014-03-17 16:09:19 +00:00
|
|
|
type='integer', multi='_get_statistics'
|
|
|
|
),
|
2013-09-03 14:59:36 +00:00
|
|
|
'sent': fields.function(
|
2014-03-17 16:09:19 +00:00
|
|
|
_get_statistics, string='Sent Emails',
|
2013-09-03 14:59:36 +00:00
|
|
|
type='integer', multi='_get_statistics'
|
|
|
|
),
|
2013-09-10 10:46:23 +00:00
|
|
|
'delivered': fields.function(
|
2014-03-17 16:09:19 +00:00
|
|
|
_get_statistics, string='Delivered',
|
2013-09-10 10:46:23 +00:00
|
|
|
type='integer', multi='_get_statistics',
|
|
|
|
),
|
2013-09-03 14:59:36 +00:00
|
|
|
'opened': fields.function(
|
2014-03-17 16:09:19 +00:00
|
|
|
_get_statistics, string='Opened',
|
2013-09-03 14:59:36 +00:00
|
|
|
type='integer', multi='_get_statistics',
|
|
|
|
),
|
|
|
|
'replied': fields.function(
|
2014-03-17 16:09:19 +00:00
|
|
|
_get_statistics, string='Replied',
|
2013-09-03 14:59:36 +00:00
|
|
|
type='integer', multi='_get_statistics'
|
|
|
|
),
|
|
|
|
'bounced': fields.function(
|
2014-03-17 16:09:19 +00:00
|
|
|
_get_statistics, string='Bounced',
|
2013-09-03 14:59:36 +00:00
|
|
|
type='integer', multi='_get_statistics'
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
2013-09-13 14:00:43 +00:00
|
|
|
_defaults = {
|
|
|
|
'user_id': lambda self, cr, uid, ctx=None: uid,
|
2014-03-14 16:51:13 +00:00
|
|
|
'state': 'draft',
|
2013-09-13 14:00:43 +00:00
|
|
|
}
|
2013-09-13 11:54:08 +00:00
|
|
|
|
2014-03-14 16:51:13 +00:00
|
|
|
#------------------------------------------------------
|
|
|
|
# Technical stuff
|
|
|
|
#------------------------------------------------------
|
|
|
|
|
|
|
|
def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False):
|
|
|
|
""" Override read_group to always display all states. """
|
|
|
|
if groupby and groupby[0] == "state":
|
|
|
|
# Default result structure
|
|
|
|
states = self._get_state_list(cr, uid, context=context)
|
|
|
|
read_group_all_states = [{
|
|
|
|
'__context': {'group_by': groupby[1:]},
|
|
|
|
'__domain': domain + [('state', '=', state_value)],
|
|
|
|
'state': state_value,
|
|
|
|
'state_count': 0,
|
|
|
|
} for state_value, state_name in states]
|
|
|
|
# Get standard results
|
|
|
|
read_group_res = super(MassMailingCampaign, self).read_group(cr, uid, domain, fields, groupby, offset=offset, limit=limit, context=context, orderby=orderby)
|
|
|
|
# Update standard results with default results
|
|
|
|
result = []
|
|
|
|
for state_value, state_name in states:
|
|
|
|
res = filter(lambda x: x['state'] == state_value, read_group_res)
|
|
|
|
if not res:
|
|
|
|
res = filter(lambda x: x['state'] == state_value, read_group_all_states)
|
|
|
|
res[0]['state'] = [state_value, state_name]
|
|
|
|
result.append(res[0])
|
|
|
|
return result
|
|
|
|
else:
|
|
|
|
return super(MassMailingCampaign, self).read_group(cr, uid, domain, fields, groupby, offset=offset, limit=limit, context=context, orderby=orderby)
|
|
|
|
|
|
|
|
#------------------------------------------------------
|
|
|
|
# Actions
|
|
|
|
#------------------------------------------------------
|
|
|
|
|
2014-03-17 16:09:19 +00:00
|
|
|
def action_new_mailing(self, cr, uid, ids, context=None):
|
2013-09-12 10:09:09 +00:00
|
|
|
return {
|
2013-09-13 11:54:08 +00:00
|
|
|
'name': _('Create a Mass Mailing for the Campaign'),
|
2013-09-12 10:09:09 +00:00
|
|
|
'type': 'ir.actions.act_window',
|
|
|
|
'view_type': 'form',
|
|
|
|
'view_mode': 'form',
|
2014-03-17 16:09:19 +00:00
|
|
|
'res_model': 'mail.mass_mailing',
|
2013-09-12 10:09:09 +00:00
|
|
|
'views': [(False, 'form')],
|
2014-03-17 16:09:19 +00:00
|
|
|
'context': dict(context, default_mass_mailing_campaign_id=ids[0]),
|
2013-09-12 10:09:09 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 13:24:54 +00:00
|
|
|
#------------------------------------------------------
|
|
|
|
# API
|
|
|
|
#------------------------------------------------------
|
|
|
|
|
|
|
|
def get_recipients(self, cr, uid, ids, model=None, context=None):
|
|
|
|
"""Return the recipints of a mailing campaign. This is based on the statistics
|
|
|
|
build for each mailing. """
|
|
|
|
Statistics = self.pool['mail.mail.statistics']
|
|
|
|
res = dict.fromkeys(ids, False)
|
|
|
|
for cid in ids:
|
|
|
|
domain = [('mass_mailing_campaign_id', '=', cid)]
|
|
|
|
if model:
|
|
|
|
domain += [('model', '=', model)]
|
|
|
|
stat_ids = Statistics.search(cr, uid, domain, context=context)
|
|
|
|
res[cid] = set(stat.res_id for stat in Statistics.browse(cr, uid, stat_ids, context=context))
|
|
|
|
return res
|
|
|
|
|
2013-09-03 14:59:36 +00:00
|
|
|
|
2013-09-13 11:54:08 +00:00
|
|
|
class MassMailing(osv.Model):
|
|
|
|
""" MassMailing models a wave of emails for a mass mailign campaign.
|
|
|
|
A mass mailing is an occurence of sending emails. """
|
2013-09-10 12:11:23 +00:00
|
|
|
|
2013-09-13 11:54:08 +00:00
|
|
|
_name = 'mail.mass_mailing'
|
2014-03-14 16:51:13 +00:00
|
|
|
_description = 'Mass Mailing'
|
2013-09-03 14:59:36 +00:00
|
|
|
# number of periods for tracking mail_mail statistics
|
|
|
|
_period_number = 6
|
2013-09-17 10:35:55 +00:00
|
|
|
_order = 'date DESC'
|
2013-09-03 14:59:36 +00:00
|
|
|
|
2013-09-10 12:11:23 +00:00
|
|
|
def __get_bar_values(self, cr, uid, id, obj, domain, read_fields, value_field, groupby_field, context=None):
|
2013-09-03 14:59:36 +00:00
|
|
|
""" Generic method to generate data for bar chart values using SparklineBarWidget.
|
|
|
|
This method performs obj.read_group(cr, uid, domain, read_fields, groupby_field).
|
|
|
|
|
|
|
|
:param obj: the target model (i.e. crm_lead)
|
|
|
|
:param domain: the domain applied to the read_group
|
|
|
|
:param list read_fields: the list of fields to read in the read_group
|
|
|
|
:param str value_field: the field used to compute the value of the bar slice
|
|
|
|
:param str groupby_field: the fields used to group
|
|
|
|
|
|
|
|
:return list section_result: a list of dicts: [
|
|
|
|
{ 'value': (int) bar_column_value,
|
|
|
|
'tootip': (str) bar_column_tooltip,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
"""
|
2013-09-10 12:11:23 +00:00
|
|
|
date_begin = datetime.strptime(self.browse(cr, uid, id, context=context).date, tools.DEFAULT_SERVER_DATETIME_FORMAT).date()
|
2013-09-03 14:59:36 +00:00
|
|
|
section_result = [{'value': 0,
|
2013-09-10 12:11:23 +00:00
|
|
|
'tooltip': (date_begin + relativedelta.relativedelta(days=i)).strftime('%d %B %Y'),
|
|
|
|
} for i in range(0, self._period_number)]
|
2013-09-03 14:59:36 +00:00
|
|
|
group_obj = obj.read_group(cr, uid, domain, read_fields, groupby_field, context=context)
|
2014-02-19 12:35:05 +00:00
|
|
|
field_col_info = obj._all_columns.get(groupby_field.split(':')[0])
|
|
|
|
pattern = tools.DEFAULT_SERVER_DATE_FORMAT if field_col_info.column._type == 'date' else tools.DEFAULT_SERVER_DATETIME_FORMAT
|
2013-09-03 14:59:36 +00:00
|
|
|
for group in group_obj:
|
2014-02-19 12:35:05 +00:00
|
|
|
group_begin_date = datetime.strptime(group['__domain'][0][2], pattern).date()
|
2013-09-10 12:11:23 +00:00
|
|
|
timedelta = relativedelta.relativedelta(group_begin_date, date_begin)
|
|
|
|
section_result[timedelta.days] = {'value': group.get(value_field, 0), 'tooltip': group.get(groupby_field)}
|
2013-09-03 14:59:36 +00:00
|
|
|
return section_result
|
|
|
|
|
2013-09-17 10:35:55 +00:00
|
|
|
def _get_daily_statistics(self, cr, uid, ids, field_name, arg, context=None):
|
|
|
|
""" Get the daily statistics of the mass mailing. This is done by a grouping
|
|
|
|
on opened and replied fields. Using custom format in context, we obtain
|
|
|
|
results for the next 6 days following the mass mailing date. """
|
2013-09-13 11:54:08 +00:00
|
|
|
obj = self.pool['mail.mail.statistics']
|
2013-09-10 12:11:23 +00:00
|
|
|
res = {}
|
2013-09-03 14:59:36 +00:00
|
|
|
for id in ids:
|
2013-09-10 12:11:23 +00:00
|
|
|
res[id] = {}
|
2013-10-03 08:27:36 +00:00
|
|
|
date_begin = datetime.strptime(self.browse(cr, uid, id, context=context).date, tools.DEFAULT_SERVER_DATETIME_FORMAT)
|
|
|
|
date_end = date_begin + relativedelta.relativedelta(days=self._period_number - 1)
|
|
|
|
date_begin_str = date_begin.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT)
|
|
|
|
date_end_str = date_end.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT)
|
|
|
|
domain = [('mass_mailing_id', '=', id), ('opened', '>=', date_begin_str), ('opened', '<=', date_end_str)]
|
2014-03-14 16:51:13 +00:00
|
|
|
res[id]['opened_dayly'] = self.__get_bar_values(cr, uid, id, obj, domain, ['opened'], 'opened_count', 'opened:day', context=context)
|
2013-10-03 08:27:36 +00:00
|
|
|
domain = [('mass_mailing_id', '=', id), ('replied', '>=', date_begin_str), ('replied', '<=', date_end_str)]
|
2014-03-14 16:51:13 +00:00
|
|
|
res[id]['replied_dayly'] = self.__get_bar_values(cr, uid, id, obj, domain, ['replied'], 'replied_count', 'replied:day', context=context)
|
2013-09-03 14:59:36 +00:00
|
|
|
return res
|
|
|
|
|
|
|
|
def _get_statistics(self, cr, uid, ids, name, arg, context=None):
|
|
|
|
""" Compute statistics of the mass mailing campaign """
|
2014-03-17 16:09:19 +00:00
|
|
|
Statistics = self.pool['mail.mail.statistics']
|
2013-09-03 14:59:36 +00:00
|
|
|
results = dict.fromkeys(ids, False)
|
2014-03-17 16:09:19 +00:00
|
|
|
for mid in ids:
|
|
|
|
results[mid] = {
|
|
|
|
'total': Statistics.search(cr, uid, [('mass_mailing_id', '=', mid)], count=True, context=context),
|
2014-03-28 13:59:07 +00:00
|
|
|
'scheduled': Statistics.search(cr, uid, [('mass_mailing_id', '=', mid), ('scheduled', '!=', False), ('sent', '=', False)], count=True, context=context),
|
2014-03-17 16:09:19 +00:00
|
|
|
'sent': Statistics.search(cr, uid, [('mass_mailing_id', '=', mid), ('sent', '!=', False)], count=True, context=context),
|
|
|
|
'opened': Statistics.search(cr, uid, [('mass_mailing_id', '=', mid), ('opened', '!=', False)], count=True, context=context),
|
|
|
|
'replied': Statistics.search(cr, uid, [('mass_mailing_id', '=', mid), ('replied', '!=', False)], count=True, context=context),
|
|
|
|
'bounced': Statistics.search(cr, uid, [('mass_mailing_id', '=', mid), ('bounced', '!=', False)], count=True, context=context),
|
2013-09-03 14:59:36 +00:00
|
|
|
}
|
2014-03-17 16:09:19 +00:00
|
|
|
results[mid]['delivered'] = results[mid]['sent'] - results[mid]['bounced']
|
2014-03-28 13:59:07 +00:00
|
|
|
results[mid]['received_ratio'] = 100.0 * results[mid]['delivered'] / (results[mid]['sent'] or 1)
|
|
|
|
results[mid]['opened_ratio'] = 100.0 * results[mid]['opened'] / (results[mid]['sent'] or 1)
|
|
|
|
results[mid]['replied_ratio'] = 100.0 * results[mid]['replied'] / (results[mid]['sent'] or 1)
|
2013-09-03 14:59:36 +00:00
|
|
|
return results
|
|
|
|
|
2014-03-18 13:04:28 +00:00
|
|
|
def _get_contact_nbr(self, cr, uid, ids, name, arg, context=None):
|
2014-04-01 13:24:54 +00:00
|
|
|
res = dict.fromkeys(ids, False)
|
2014-03-18 13:04:28 +00:00
|
|
|
for mailing in self.browse(cr, uid, ids, context=context):
|
2014-04-01 13:24:54 +00:00
|
|
|
val = {'contact_nbr': 0, 'contact_ab_nbr': 0, 'contact_ab_done': 0}
|
|
|
|
val['contact_nbr'] = self.pool[mailing.mailing_model].search(
|
2014-03-18 13:04:28 +00:00
|
|
|
cr, uid,
|
|
|
|
self.pool['mail.mass_mailing.list'].get_global_domain(cr, uid, [c.id for c in mailing.contact_list_ids], context=context)[mailing.mailing_model],
|
|
|
|
count=True, context=context
|
|
|
|
)
|
2014-04-01 13:24:54 +00:00
|
|
|
val['contact_ab_nbr'] = int(val['contact_nbr'] * mailing.contact_ab_pc / 100.0)
|
|
|
|
if mailing.mass_mailing_campaign_id and mailing.ab_testing:
|
|
|
|
val['contact_ab_done'] = len(self.pool['mail.mass_mailing.campaign'].get_recipients(cr, uid, [mailing.mass_mailing_campaign_id.id], context=context)[mailing.mass_mailing_campaign_id.id])
|
|
|
|
res[mailing.id] = val
|
2014-03-18 13:04:28 +00:00
|
|
|
return res
|
|
|
|
|
2014-03-17 17:16:12 +00:00
|
|
|
def _get_mailing_model(self, cr, uid, context=None):
|
2014-03-14 16:51:13 +00:00
|
|
|
return [
|
|
|
|
('res.partner', 'Customers'),
|
|
|
|
('mail.mass_mailing.contact', 'Contacts')
|
|
|
|
]
|
|
|
|
|
|
|
|
def _get_state_list(self, cr, uid, context=None):
|
|
|
|
return [('draft', 'Schedule'), ('test', 'Tested'), ('done', 'Sent')]
|
|
|
|
|
|
|
|
# indirections for inheritance
|
2014-03-17 17:16:12 +00:00
|
|
|
_mailing_model = lambda self, *args, **kwargs: self._get_mailing_model(*args, **kwargs)
|
2014-03-14 16:51:13 +00:00
|
|
|
_state = lambda self, *args, **kwargs: self._get_state_list(*args, **kwargs)
|
|
|
|
|
2013-09-03 14:59:36 +00:00
|
|
|
_columns = {
|
2014-03-14 16:51:13 +00:00
|
|
|
'name': fields.char('Subject', required=True),
|
|
|
|
'date': fields.datetime('Date'),
|
|
|
|
'state': fields.selection(
|
|
|
|
_state, string='Status', required=True,
|
2013-09-03 14:59:36 +00:00
|
|
|
),
|
2013-08-07 13:03:34 +00:00
|
|
|
'template_id': fields.many2one(
|
|
|
|
'email.template', 'Email Template',
|
2014-03-24 17:10:17 +00:00
|
|
|
domain="[('use_in_mass_mailing', '=', True), ('model', '=', mailing_model)]",
|
2014-03-14 16:51:13 +00:00
|
|
|
),
|
|
|
|
'body_html': fields.related(
|
|
|
|
'template_id', 'body_html', type='html',
|
|
|
|
string='Body', readonly='True',
|
|
|
|
help='Technical field: used only to display a view of the template in the form view',
|
|
|
|
),
|
|
|
|
'mass_mailing_campaign_id': fields.many2one(
|
|
|
|
'mail.mass_mailing.campaign', 'Mass Mailing Campaign',
|
2013-08-07 13:03:34 +00:00
|
|
|
ondelete='set null',
|
|
|
|
),
|
2014-04-01 13:24:54 +00:00
|
|
|
'ab_testing': fields.related(
|
|
|
|
'mass_mailing_campaign_id', 'ab_testing',
|
|
|
|
type='boolean', string='AB Testing'
|
|
|
|
),
|
2013-09-10 15:19:01 +00:00
|
|
|
'color': fields.related(
|
|
|
|
'mass_mailing_campaign_id', 'color',
|
|
|
|
type='integer', string='Color Index',
|
|
|
|
),
|
2014-03-14 16:51:13 +00:00
|
|
|
# mailing options
|
|
|
|
'email_from': fields.char('From'),
|
2014-03-27 13:08:27 +00:00
|
|
|
'test_email_to': fields.char('Test Emails'),
|
2014-03-14 16:51:13 +00:00
|
|
|
'reply_to': fields.char('Reply To'),
|
2014-03-17 17:16:12 +00:00
|
|
|
'mailing_model': fields.selection(_mailing_model, string='Type', required=True),
|
2014-03-14 16:51:13 +00:00
|
|
|
'contact_list_ids': fields.many2many(
|
|
|
|
'mail.mass_mailing.list', 'mail_mass_mailing_list_rel',
|
|
|
|
string='Mailing Lists',
|
2014-03-17 17:16:12 +00:00
|
|
|
domain="[('model', '=', mailing_model)]",
|
2014-03-14 16:51:13 +00:00
|
|
|
),
|
2014-04-01 13:24:54 +00:00
|
|
|
'contact_nbr': fields.function(
|
|
|
|
_get_contact_nbr, type='integer', multi='_get_contact_nbr',
|
|
|
|
string='Contact Number'
|
|
|
|
),
|
|
|
|
'contact_ab_pc': fields.integer(
|
|
|
|
'AB Testing percentage',
|
|
|
|
help='Percentage of the contacts that will be mailed. Recipients will be taken randomly.'
|
|
|
|
),
|
|
|
|
'contact_ab_nbr': fields.function(
|
|
|
|
_get_contact_nbr, type='integer', multi='_get_contact_nbr',
|
|
|
|
string='Contact Number in AB Testing'
|
|
|
|
),
|
|
|
|
'contact_ab_done': fields.function(
|
|
|
|
_get_contact_nbr, type='integer', multi='_get_contact_nbr',
|
|
|
|
string='Number of already mailed contacts'
|
|
|
|
),
|
2013-09-13 11:54:08 +00:00
|
|
|
# statistics data
|
|
|
|
'statistics_ids': fields.one2many(
|
|
|
|
'mail.mail.statistics', 'mass_mailing_id',
|
2013-09-13 14:00:43 +00:00
|
|
|
'Emails Statistics',
|
2013-08-07 13:03:34 +00:00
|
|
|
),
|
2014-03-17 16:09:19 +00:00
|
|
|
'total': fields.function(
|
2014-03-28 13:59:07 +00:00
|
|
|
_get_statistics, string='Total',
|
|
|
|
type='integer', multi='_get_statistics',
|
|
|
|
),
|
|
|
|
'scheduled': fields.function(
|
2014-03-17 16:09:19 +00:00
|
|
|
_get_statistics, string='Scheduled',
|
2014-03-28 13:59:07 +00:00
|
|
|
type='integer', multi='_get_statistics',
|
2014-03-17 16:09:19 +00:00
|
|
|
),
|
2013-08-07 13:03:34 +00:00
|
|
|
'sent': fields.function(
|
2014-03-17 16:09:19 +00:00
|
|
|
_get_statistics, string='Sent',
|
2014-03-28 13:59:07 +00:00
|
|
|
type='integer', multi='_get_statistics',
|
2013-08-07 13:03:34 +00:00
|
|
|
),
|
2013-09-10 10:46:23 +00:00
|
|
|
'delivered': fields.function(
|
2014-03-17 16:09:19 +00:00
|
|
|
_get_statistics, string='Delivered',
|
2013-09-10 10:46:23 +00:00
|
|
|
type='integer', multi='_get_statistics',
|
|
|
|
),
|
2013-09-03 14:59:36 +00:00
|
|
|
'opened': fields.function(
|
2014-03-17 16:09:19 +00:00
|
|
|
_get_statistics, string='Opened',
|
2013-09-03 14:59:36 +00:00
|
|
|
type='integer', multi='_get_statistics',
|
2013-08-07 13:03:34 +00:00
|
|
|
),
|
2013-09-03 14:59:36 +00:00
|
|
|
'replied': fields.function(
|
2014-03-17 16:09:19 +00:00
|
|
|
_get_statistics, string='Replied',
|
2014-03-28 13:59:07 +00:00
|
|
|
type='integer', multi='_get_statistics',
|
2013-08-07 13:03:34 +00:00
|
|
|
),
|
2013-09-03 14:59:36 +00:00
|
|
|
'bounced': fields.function(
|
2014-03-17 16:09:19 +00:00
|
|
|
_get_statistics, string='Bounced',
|
2014-03-28 13:59:07 +00:00
|
|
|
type='integer', multi='_get_statistics',
|
|
|
|
),
|
|
|
|
'received_ratio': fields.function(
|
|
|
|
_get_statistics, string='Received Ratio',
|
|
|
|
type='integer', multi='_get_statistics',
|
|
|
|
),
|
|
|
|
'opened_ratio': fields.function(
|
|
|
|
_get_statistics, string='Opened Ratio',
|
|
|
|
type='integer', multi='_get_statistics',
|
|
|
|
),
|
|
|
|
'replied_ratio': fields.function(
|
|
|
|
_get_statistics, string='Replied Ratio',
|
|
|
|
type='integer', multi='_get_statistics',
|
2013-09-03 14:59:36 +00:00
|
|
|
),
|
2014-03-28 13:59:07 +00:00
|
|
|
# dayly ratio
|
2014-03-14 16:51:13 +00:00
|
|
|
'opened_dayly': fields.function(
|
2014-03-17 16:09:19 +00:00
|
|
|
_get_daily_statistics, string='Opened',
|
2013-09-17 10:35:55 +00:00
|
|
|
type='char', multi='_get_daily_statistics',
|
2014-03-14 16:51:13 +00:00
|
|
|
oldname='opened_monthly',
|
2013-09-03 14:59:36 +00:00
|
|
|
),
|
2014-03-14 16:51:13 +00:00
|
|
|
'replied_dayly': fields.function(
|
2014-03-17 16:09:19 +00:00
|
|
|
_get_daily_statistics, string='Replied',
|
2013-09-17 10:35:55 +00:00
|
|
|
type='char', multi='_get_daily_statistics',
|
2014-03-14 16:51:13 +00:00
|
|
|
oldname='replied_monthly',
|
2013-08-07 13:03:34 +00:00
|
|
|
),
|
|
|
|
}
|
2013-09-10 15:19:01 +00:00
|
|
|
|
|
|
|
_defaults = {
|
2014-03-14 16:51:13 +00:00
|
|
|
'state': 'draft',
|
2013-10-03 08:27:36 +00:00
|
|
|
'date': fields.datetime.now,
|
2014-03-14 16:51:13 +00:00
|
|
|
'email_from': lambda self, cr, uid, ctx=None: self.pool['mail.message']._get_default_from(cr, uid, context=ctx),
|
2014-03-17 17:16:12 +00:00
|
|
|
'mailing_model': 'res.partner',
|
2014-04-01 13:24:54 +00:00
|
|
|
'contact_ab_pc': 100,
|
2013-09-10 15:19:01 +00:00
|
|
|
}
|
2013-09-13 11:54:08 +00:00
|
|
|
|
2014-03-14 16:51:13 +00:00
|
|
|
#------------------------------------------------------
|
|
|
|
# Technical stuff
|
|
|
|
#------------------------------------------------------
|
|
|
|
|
|
|
|
def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False):
|
|
|
|
""" Override read_group to always display all states. """
|
|
|
|
if groupby and groupby[0] == "state":
|
|
|
|
# Default result structure
|
|
|
|
states = self._get_state_list(cr, uid, context=context)
|
|
|
|
read_group_all_states = [{
|
|
|
|
'__context': {'group_by': groupby[1:]},
|
|
|
|
'__domain': domain + [('state', '=', state_value)],
|
|
|
|
'state': state_value,
|
|
|
|
'state_count': 0,
|
|
|
|
} for state_value, state_name in states]
|
|
|
|
# Get standard results
|
|
|
|
read_group_res = super(MassMailing, self).read_group(cr, uid, domain, fields, groupby, offset=offset, limit=limit, context=context, orderby=orderby)
|
|
|
|
# Update standard results with default results
|
|
|
|
result = []
|
|
|
|
for state_value, state_name in states:
|
|
|
|
res = filter(lambda x: x['state'] == state_value, read_group_res)
|
|
|
|
if not res:
|
|
|
|
res = filter(lambda x: x['state'] == state_value, read_group_all_states)
|
|
|
|
res[0]['state'] = [state_value, state_name]
|
|
|
|
result.append(res[0])
|
|
|
|
return result
|
|
|
|
else:
|
|
|
|
return super(MassMailing, self).read_group(cr, uid, domain, fields, groupby, offset=offset, limit=limit, context=context, orderby=orderby)
|
|
|
|
|
|
|
|
#------------------------------------------------------
|
|
|
|
# Views & Actions
|
|
|
|
#------------------------------------------------------
|
|
|
|
|
2014-03-17 17:16:12 +00:00
|
|
|
def on_change_mailing_model(self, cr, uid, ids, mailing_model, context=None):
|
2014-03-20 17:04:55 +00:00
|
|
|
return {'value': {'contact_list_ids': [], 'template_id': False, 'contact_nbr': 0}}
|
2014-03-14 16:51:13 +00:00
|
|
|
|
|
|
|
def on_change_template_id(self, cr, uid, ids, template_id, context=None):
|
|
|
|
values = {}
|
|
|
|
if template_id:
|
|
|
|
template = self.pool['email.template'].browse(cr, uid, template_id, context=context)
|
|
|
|
if template.email_from:
|
|
|
|
values['email_from'] = template.email_from
|
|
|
|
if template.reply_to:
|
|
|
|
values['reply_to'] = template.reply_to
|
|
|
|
values['body_html'] = template.body_html
|
|
|
|
else:
|
|
|
|
values['email_from'] = self.pool['mail.message']._get_default_from(cr, uid, context=context)
|
|
|
|
values['reply_to'] = False
|
|
|
|
values['body_html'] = False
|
|
|
|
return {'value': values}
|
|
|
|
|
2014-03-20 17:50:04 +00:00
|
|
|
def action_duplicate(self, cr, uid, ids, context=None):
|
|
|
|
copy_id = None
|
|
|
|
for mailing in self.browse(cr, uid, ids, context=context):
|
|
|
|
copy_id = self.copy(
|
|
|
|
cr, uid, mailing.id, default={
|
|
|
|
'statistics_ids': [],
|
|
|
|
'state': 'draft',
|
|
|
|
'name': _('%s (duplicate)') % mailing.name,
|
|
|
|
}, context=context)
|
|
|
|
if copy_id:
|
|
|
|
return {
|
|
|
|
'type': 'ir.actions.act_window',
|
|
|
|
'view_type': 'form',
|
|
|
|
'view_mode': 'form',
|
|
|
|
'res_model': 'mail.mass_mailing',
|
|
|
|
'res_id': copy_id,
|
|
|
|
'context': context,
|
|
|
|
}
|
|
|
|
return False
|
|
|
|
|
2014-03-27 15:46:06 +00:00
|
|
|
def _get_model_to_list_action_id(self, cr, uid, model, context=None):
|
|
|
|
if model == 'res.partner':
|
|
|
|
return self.pool['ir.model.data'].xmlid_to_res_id(cr, uid, 'mass_mailing.action_partner_to_mailing_list')
|
|
|
|
else:
|
|
|
|
return self.pool['ir.model.data'].xmlid_to_res_id(cr, uid, 'mass_mailing.action_contact_to_mailing_list')
|
|
|
|
|
2014-03-18 13:04:28 +00:00
|
|
|
def action_new_list(self, cr, uid, ids, context=None):
|
2014-03-17 17:16:12 +00:00
|
|
|
wizard = self.browse(cr, uid, ids[0], context=context)
|
2014-03-27 15:46:06 +00:00
|
|
|
action_id = self._get_model_to_list_action_id(cr, uid, wizard.mailing_model, context=context)
|
2014-03-28 13:59:07 +00:00
|
|
|
ctx = dict(context, search_default_not_opt_out=True, view_manager_highlight=[action_id], default_mass_mailing_id=ids[0], default_model=wizard.mailing_model)
|
2014-03-17 17:16:12 +00:00
|
|
|
return {
|
|
|
|
'name': _('Choose Recipients'),
|
|
|
|
'type': 'ir.actions.act_window',
|
|
|
|
'view_type': 'form',
|
|
|
|
'view_mode': 'tree,form',
|
|
|
|
'res_model': wizard.mailing_model,
|
|
|
|
'context': ctx,
|
|
|
|
}
|
|
|
|
|
2014-03-18 13:04:28 +00:00
|
|
|
def action_see_recipients(self, cr, uid, ids, context=None):
|
|
|
|
mailing = self.browse(cr, uid, ids[0], context=context)
|
|
|
|
domain = self.pool['mail.mass_mailing.list'].get_global_domain(cr, uid, [c.id for c in mailing.contact_list_ids], context=context)[mailing.mailing_model]
|
|
|
|
return {
|
|
|
|
'name': _('Mailing Recipients'),
|
|
|
|
'type': 'ir.actions.act_window',
|
|
|
|
'view_type': 'form',
|
|
|
|
'view_mode': 'tree,form',
|
|
|
|
'res_model': mailing.mailing_model,
|
2014-03-20 17:04:55 +00:00
|
|
|
'target': 'new',
|
2014-03-18 13:04:28 +00:00
|
|
|
'domain': domain,
|
|
|
|
'context': context,
|
|
|
|
}
|
|
|
|
|
2014-03-17 17:16:12 +00:00
|
|
|
#------------------------------------------------------
|
|
|
|
# Email Sending
|
|
|
|
#------------------------------------------------------
|
2014-03-14 16:51:13 +00:00
|
|
|
|
2014-03-25 15:41:14 +00:00
|
|
|
def get_recipients_data(self, cr, uid, mailing, res_ids, context=None):
|
|
|
|
# tde todo: notification link ?
|
2014-03-18 16:41:17 +00:00
|
|
|
if mailing.mailing_model == 'mail.mass_mailing.contact':
|
|
|
|
contacts = self.pool['mail.mass_mailing.contact'].browse(cr, uid, res_ids, context=context)
|
2014-03-25 15:41:14 +00:00
|
|
|
return dict((contact.id, {'partner_id': False, 'name': contact.name, 'email': contact.email}) for contact in contacts)
|
2014-03-18 16:41:17 +00:00
|
|
|
else:
|
2014-03-21 17:21:39 +00:00
|
|
|
partners = self.pool['res.partner'].browse(cr, uid, res_ids, context=context)
|
2014-03-25 15:41:14 +00:00
|
|
|
return dict((partner.id, {'partner_id': partner.id, 'name': partner.name, 'email': partner.email}) for partner in partners)
|
|
|
|
|
2014-04-01 13:24:54 +00:00
|
|
|
def get_recipients(self, cr, uid, mailing, context=None):
|
|
|
|
domain = self.pool['mail.mass_mailing.list'].get_global_domain(
|
|
|
|
cr, uid, [l.id for l in mailing.contact_list_ids], context=context
|
|
|
|
)[mailing.mailing_model]
|
|
|
|
res_ids = self.pool[mailing.mailing_model].search(cr, uid, domain, context=context)
|
|
|
|
|
|
|
|
# randomly choose a fragment
|
|
|
|
if mailing.contact_ab_pc != 100:
|
|
|
|
topick = mailing.contact_ab_nbr
|
|
|
|
if mailing.mass_mailing_campaign_id and mailing.ab_testing:
|
|
|
|
already_mailed = self.pool['mail.mass_mailing.campaign'].get_recipients(cr, uid, [mailing.mass_mailing_campaign_id.id], context=context)[mailing.mass_mailing_campaign_id.id]
|
|
|
|
else:
|
|
|
|
already_mailed = set([])
|
|
|
|
remaining = set(res_ids).difference(already_mailed)
|
|
|
|
if topick > len(remaining):
|
|
|
|
topick = len(remaining)
|
|
|
|
res_ids = random.sample(remaining, topick)
|
|
|
|
return res_ids
|
|
|
|
|
2014-03-25 16:18:13 +00:00
|
|
|
def get_unsubscribe_url(self, cr, uid, mailing_id, res_id, email, msg=None, context=None):
|
2014-03-25 15:41:14 +00:00
|
|
|
base_url = self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url')
|
|
|
|
url = urlparse.urljoin(
|
|
|
|
base_url, 'mail/mailing/%(mailing_id)s/unsubscribe?%(params)s' % {
|
2014-03-25 16:18:13 +00:00
|
|
|
'mailing_id': mailing_id,
|
2014-03-28 13:59:07 +00:00
|
|
|
'params': urllib.urlencode({'db': cr.dbname, 'res_id': res_id, 'email': email})
|
2014-03-25 15:41:14 +00:00
|
|
|
}
|
|
|
|
)
|
2014-03-28 13:59:07 +00:00
|
|
|
return '<small><a href="%s">%s</a></small>' % (url, msg or 'Click to unsubscribe')
|
2014-03-18 16:41:17 +00:00
|
|
|
|
2014-03-17 17:16:12 +00:00
|
|
|
def send_mail(self, cr, uid, ids, context=None):
|
2014-03-21 17:21:39 +00:00
|
|
|
author_id = self.pool['res.users'].browse(cr, uid, uid, context=context).partner_id.id
|
2014-03-17 17:16:12 +00:00
|
|
|
Mail = self.pool['mail.mail']
|
|
|
|
for mailing in self.browse(cr, uid, ids, context=context):
|
2014-03-25 15:41:14 +00:00
|
|
|
if not mailing.template_id:
|
2014-03-31 11:07:56 +00:00
|
|
|
raise Warning('Please specify a template to use.')
|
2014-03-25 15:51:27 +00:00
|
|
|
if not mailing.contact_nbr:
|
|
|
|
raise Warning('Please select recipients.')
|
2014-03-25 15:41:14 +00:00
|
|
|
|
|
|
|
# get mail and recipints data
|
2014-04-01 13:24:54 +00:00
|
|
|
res_ids = self.get_recipients(cr, uid, mailing, context=context)
|
2014-03-25 15:41:14 +00:00
|
|
|
template_values = self.pool['mail.compose.message'].generate_email_for_composer_batch(
|
2014-03-14 16:51:13 +00:00
|
|
|
cr, uid, mailing.template_id.id, res_ids,
|
2014-03-18 16:41:17 +00:00
|
|
|
context=context, fields=['body_html', 'attachment_ids', 'mail_server_id'])
|
2014-03-25 15:41:14 +00:00
|
|
|
recipient_values = self.get_recipients_data(cr, uid, mailing, res_ids, context=context)
|
|
|
|
|
|
|
|
for res_id, mail_values in template_values.iteritems():
|
2014-03-21 17:21:39 +00:00
|
|
|
body = mail_values.get('body')
|
2014-03-25 15:41:14 +00:00
|
|
|
recipient = recipient_values[res_id]
|
2014-03-25 16:18:13 +00:00
|
|
|
unsubscribe_url = self.get_unsubscribe_url(cr, uid, mailing.id, res_id, recipient['email'], context=context)
|
2014-03-25 15:41:14 +00:00
|
|
|
if unsubscribe_url:
|
|
|
|
body = tools.append_content_to_html(body, unsubscribe_url, plaintext=False, container_tag='p')
|
2014-03-21 17:21:39 +00:00
|
|
|
|
2014-03-14 16:51:13 +00:00
|
|
|
mail_values.update({
|
|
|
|
'email_from': mailing.email_from,
|
|
|
|
'reply_to': mailing.reply_to,
|
|
|
|
'subject': mailing.name,
|
2014-03-21 17:21:39 +00:00
|
|
|
'record_name': False,
|
2014-03-28 13:59:07 +00:00
|
|
|
'model': mailing.mailing_model,
|
|
|
|
'res_id': res_id,
|
2014-03-21 17:21:39 +00:00
|
|
|
'author_id': author_id,
|
|
|
|
'body_html': body,
|
2014-03-14 16:51:13 +00:00
|
|
|
'auto_delete': True,
|
2014-03-18 16:41:17 +00:00
|
|
|
'notification': True,
|
2014-03-25 15:41:14 +00:00
|
|
|
'email_to': '"%s" <%s>' % (recipient['name'], recipient['email'])
|
2014-03-18 16:41:17 +00:00
|
|
|
})
|
|
|
|
mail_values['statistics_ids'] = [
|
|
|
|
(0, 0, {
|
2014-03-17 17:16:12 +00:00
|
|
|
'model': mailing.mailing_model,
|
2014-03-14 16:51:13 +00:00
|
|
|
'res_id': res_id,
|
|
|
|
'mass_mailing_id': mailing.id,
|
|
|
|
})]
|
|
|
|
m2m_attachment_ids = self.pool['mail.thread']._message_preprocess_attachments(
|
|
|
|
cr, uid, mail_values.pop('attachments', []),
|
|
|
|
mail_values.pop('attachment_ids', []),
|
|
|
|
'mail.message', 0,
|
|
|
|
context=context)
|
|
|
|
mail_values['attachment_ids'] = m2m_attachment_ids
|
2014-03-18 16:41:17 +00:00
|
|
|
|
2014-03-14 16:51:13 +00:00
|
|
|
Mail.create(cr, uid, mail_values, context=context)
|
|
|
|
return True
|
|
|
|
|
2014-03-18 16:41:17 +00:00
|
|
|
def send_mail_test(self, cr, uid, ids, context=None):
|
2014-03-14 16:51:13 +00:00
|
|
|
Mail = self.pool['mail.mail']
|
|
|
|
for mailing in self.browse(cr, uid, ids, context=context):
|
2014-03-25 15:41:14 +00:00
|
|
|
if not mailing.template_id:
|
|
|
|
raise Warning('Please specifiy a template to use.')
|
2014-03-27 13:08:27 +00:00
|
|
|
test_emails = tools.email_split(mailing.test_email_to)
|
2014-03-25 16:18:13 +00:00
|
|
|
if not test_emails:
|
2014-03-24 17:10:17 +00:00
|
|
|
raise Warning('Please specifiy test email adresses.')
|
2014-03-18 16:41:17 +00:00
|
|
|
mail_ids = []
|
2014-03-25 16:18:13 +00:00
|
|
|
for test_mail in test_emails:
|
|
|
|
body = mailing.template_id.body_html
|
|
|
|
unsubscribe_url = self.get_unsubscribe_url(cr, uid, mailing.id, 0, email=test_mail, context=context)
|
|
|
|
body = tools.append_content_to_html(body, unsubscribe_url, plaintext=False, container_tag='p')
|
2014-03-18 16:41:17 +00:00
|
|
|
mail_values = {
|
|
|
|
'email_from': mailing.email_from,
|
|
|
|
'reply_to': mailing.reply_to,
|
2014-03-25 16:18:13 +00:00
|
|
|
'email_to': test_mail,
|
2014-03-18 16:41:17 +00:00
|
|
|
'subject': mailing.name,
|
2014-03-25 16:18:13 +00:00
|
|
|
'body_html': body,
|
2014-03-18 16:41:17 +00:00
|
|
|
'auto_delete': True,
|
|
|
|
}
|
|
|
|
mail_ids.append(Mail.create(cr, uid, mail_values, context=context))
|
|
|
|
Mail.send(cr, uid, mail_ids, context=context)
|
2014-03-14 16:51:13 +00:00
|
|
|
return True
|
|
|
|
|
2013-09-13 11:54:08 +00:00
|
|
|
|
|
|
|
class MailMailStats(osv.Model):
|
|
|
|
""" MailMailStats models the statistics collected about emails. Those statistics
|
|
|
|
are stored in a separated model and table to avoid bloating the mail_mail table
|
|
|
|
with statistics values. This also allows to delete emails send with mass mailing
|
|
|
|
without loosing the statistics about them. """
|
|
|
|
|
|
|
|
_name = 'mail.mail.statistics'
|
|
|
|
_description = 'Email Statistics'
|
|
|
|
_rec_name = 'message_id'
|
|
|
|
_order = 'message_id'
|
|
|
|
|
|
|
|
_columns = {
|
|
|
|
'mail_mail_id': fields.integer(
|
|
|
|
'Mail ID',
|
|
|
|
help='ID of the related mail_mail. This field is an integer field because'
|
|
|
|
'the related mail_mail can be deleted separately from its statistics.'
|
|
|
|
),
|
2014-03-28 13:59:07 +00:00
|
|
|
'message_id': fields.char('Message-ID'),
|
|
|
|
'model': fields.char('Document model'),
|
|
|
|
'res_id': fields.integer('Document ID'),
|
2013-09-13 11:54:08 +00:00
|
|
|
# campaign / wave data
|
|
|
|
'mass_mailing_id': fields.many2one(
|
|
|
|
'mail.mass_mailing', 'Mass Mailing',
|
|
|
|
ondelete='set null',
|
|
|
|
),
|
|
|
|
'mass_mailing_campaign_id': fields.related(
|
|
|
|
'mass_mailing_id', 'mass_mailing_campaign_id',
|
|
|
|
type='many2one', ondelete='set null',
|
|
|
|
relation='mail.mass_mailing.campaign',
|
|
|
|
string='Mass Mailing Campaign',
|
|
|
|
store=True, readonly=True,
|
|
|
|
),
|
|
|
|
'template_id': fields.related(
|
|
|
|
'mass_mailing_id', 'template_id',
|
|
|
|
type='many2one', ondelete='set null',
|
|
|
|
relation='email.template',
|
|
|
|
string='Email Template',
|
|
|
|
store=True, readonly=True,
|
|
|
|
),
|
|
|
|
# Bounce and tracking
|
2014-03-28 13:59:07 +00:00
|
|
|
'scheduled': fields.datetime('Scheduled', help='Date when the email has been created'),
|
|
|
|
'sent': fields.datetime('Sent', help='Date when the email has been sent'),
|
|
|
|
'opened': fields.datetime('Opened', help='Date when the email has been opened the first time'),
|
|
|
|
'replied': fields.datetime('Replied', help='Date when this email has been replied for the first time.'),
|
|
|
|
'bounced': fields.datetime('Bounced', help='Date when this email has bounced.'),
|
2013-09-13 11:54:08 +00:00
|
|
|
}
|
|
|
|
|
2014-03-28 13:59:07 +00:00
|
|
|
_defaults = {
|
|
|
|
'scheduled': fields.datetime.now,
|
|
|
|
}
|
|
|
|
|
|
|
|
def _get_ids(self, cr, uid, ids=None, mail_mail_ids=None, mail_message_ids=None, domain=None, context=None):
|
2013-09-13 11:54:08 +00:00
|
|
|
if not ids and mail_mail_ids:
|
2014-03-28 13:59:07 +00:00
|
|
|
base_domain = [('mail_mail_id', 'in', mail_mail_ids)]
|
2013-09-13 11:54:08 +00:00
|
|
|
elif not ids and mail_message_ids:
|
2014-03-28 13:59:07 +00:00
|
|
|
base_domain = [('message_id', 'in', mail_message_ids)]
|
2013-09-13 11:54:08 +00:00
|
|
|
else:
|
2014-03-28 13:59:07 +00:00
|
|
|
base_domain = [('id', 'in', ids or [])]
|
|
|
|
if domain:
|
|
|
|
base_domain = ['&'] + domain + base_domain
|
|
|
|
return self.search(cr, uid, base_domain, context=context)
|
|
|
|
|
|
|
|
def set_opened(self, cr, uid, ids=None, mail_mail_ids=None, mail_message_ids=None, context=None):
|
|
|
|
stat_ids = self._get_ids(cr, uid, ids, mail_mail_ids, mail_message_ids, [('opened', '=', False)], context)
|
|
|
|
self.write(cr, uid, stat_ids, {'opened': fields.datetime.now()}, context=context)
|
|
|
|
return stat_ids
|
2013-09-13 11:54:08 +00:00
|
|
|
|
|
|
|
def set_replied(self, cr, uid, ids=None, mail_mail_ids=None, mail_message_ids=None, context=None):
|
2014-03-28 13:59:07 +00:00
|
|
|
stat_ids = self._get_ids(cr, uid, ids, mail_mail_ids, mail_message_ids, [('replied', '=', False)], context)
|
|
|
|
self.write(cr, uid, stat_ids, {'replied': fields.datetime.now()}, context=context)
|
|
|
|
return stat_ids
|
2013-09-13 11:54:08 +00:00
|
|
|
|
|
|
|
def set_bounced(self, cr, uid, ids=None, mail_mail_ids=None, mail_message_ids=None, context=None):
|
2014-03-28 13:59:07 +00:00
|
|
|
stat_ids = self._get_ids(cr, uid, ids, mail_mail_ids, mail_message_ids, [('bounced', '=', False)], context)
|
|
|
|
self.write(cr, uid, stat_ids, {'bounced': fields.datetime.now()}, context=context)
|
|
|
|
return stat_ids
|