[IMP] typo manifest mass mailing'
[odoo/odoo.git] / addons / mass_mailing / mail_mail.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 urlparse import urljoin
23
24 from openerp import tools
25 from openerp import SUPERUSER_ID
26 from openerp.osv import osv, fields
27
28
29 class MailMail(osv.Model):
30     """Add the mass mailing campaign data to mail"""
31     _name = 'mail.mail'
32     _inherit = ['mail.mail']
33
34     _columns = {
35         'statistics_ids': fields.one2many(
36             'mail.mail.statistics', 'mail_mail_id',
37             string='Statistics',
38         ),
39     }
40
41     def create(self, cr, uid, values, context=None):
42         """ Override mail_mail creation to create an entry in mail.mail.statistics """
43         # TDE note: should be after 'all values computed', to have values (FIXME after merging other branch holding create refactoring)
44         mail_id = super(MailMail, self).create(cr, uid, values, context=context)
45         if values.get('statistics_ids'):
46             mail = self.browse(cr, SUPERUSER_ID, mail_id)
47             for stat in mail.statistics_ids:
48                 self.pool['mail.mail.statistics'].write(cr, uid, [stat.id], {'message_id': mail.message_id}, context=context)
49         return mail_id
50
51     def _get_tracking_url(self, cr, uid, mail, partner=None, context=None):
52         base_url = self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url')
53         track_url = urljoin(base_url, 'mail/track/%d/blank.gif' % mail.id)
54         return '<img src="%s" alt=""/>' % track_url
55
56     def send_get_mail_body(self, cr, uid, mail, partner=None, context=None):
57         """ Override to add the tracking URL to the body. """
58         body = super(MailMail, self).send_get_mail_body(cr, uid, mail, partner=partner, context=context)
59
60         # generate tracking URL
61         if mail.statistics_ids:
62             tracking_url = self._get_tracking_url(cr, uid, mail, partner, context=context)
63             if tracking_url:
64                 body = tools.append_content_to_html(body, tracking_url, plaintext=False, container_tag='div')
65         return body