d2d9ee1156db3daf5e5abf1185da94f1d87b64d7
[odoo/odoo.git] / addons / mass_mailing / wizard / mail_compose_message.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2013-today OpenERP SA (<http://www.openerp.com>)
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>
19 #
20 ##############################################################################
21
22 from openerp.osv import osv, fields
23
24
25 class MailComposeMessage(osv.TransientModel):
26     """Add concept of mass mailing campaign to the mail.compose.message wizard
27     """
28     _inherit = 'mail.compose.message'
29
30     _columns = {
31         'mass_mailing_campaign_id': fields.many2one(
32             'mail.mass_mailing.campaign', 'Mass mailing campaign',
33         ),
34         'mass_mailing_name': fields.char('Mass Mailing'),
35     }
36
37     def get_mail_values(self, cr, uid, wizard, res_ids, context=None):
38         """ Override method that generated the mail content by creating the
39         mail.mail.statistics values in the o2m of mail_mail, when doing pure
40         email mass mailing. """
41         res = super(MailComposeMessage, self).get_mail_values(cr, uid, wizard, res_ids, context=context)
42         # use only for allowed models in mass mailing
43         if wizard.model not in [t[0] for t in self.pool['mail.mass_mailing']._get_mailing_type()]:
44             return res
45         if wizard.composition_mode == 'mass_mail' and wizard.mass_mailing_name:
46             list_id = self.pool['mail.mass_mailing.list'].create(
47                 cr, uid, {
48                     'name': wizard.mass_mailing_name,
49                     'model': wizard.model,
50                     'domain': wizard.active_domain,
51                 }, context=context)
52             mass_mailing_id = self.pool['mail.mass_mailing'].create(
53                 cr, uid, {
54                     'mass_mailing_campaign_id': wizard.mass_mailing_campaign_id and wizard.mass_mailing_campaign_id.id or False,
55                     'name': wizard.mass_mailing_name,
56                     'template_id': wizard.template_id and wizard.template_id.id or False,
57                     'state': 'done',
58                     'mailing_type': wizard.model,
59                     'contact_list_ids': [(4, list_id)],
60                 }, context=context)
61             for res_id in res_ids:
62                 res[res_id]['statistics_ids'] = [(0, 0, {
63                     'model': wizard.model,
64                     'res_id': res_id,
65                     'mass_mailing_id': mass_mailing_id,
66                 })]
67         return res