1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2013-Today OpenERP SA (<http://www.openerp.com>)
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
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
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/>
20 ##############################################################################
25 from openerp import tools
26 from openerp import SUPERUSER_ID
27 from openerp.osv import osv, fields
30 class MailMail(osv.Model):
31 """Add the mass mailing campaign data to mail"""
33 _inherit = ['mail.mail']
36 'statistics_ids': fields.one2many(
37 'mail.mail.statistics', 'mail_mail_id',
42 def create(self, cr, uid, values, context=None):
43 """ Override mail_mail creation to create an entry in mail.mail.statistics """
44 # TDE note: should be after 'all values computed', to have values (FIXME after merging other branch holding create refactoring)
45 mail_id = super(MailMail, self).create(cr, uid, values, context=context)
46 if values.get('statistics_ids'):
47 mail = self.browse(cr, SUPERUSER_ID, mail_id)
48 for stat in mail.statistics_ids:
49 self.pool['mail.mail.statistics'].write(cr, uid, [stat.id], {'message_id': mail.message_id}, context=context)
52 def _get_tracking_url(self, cr, uid, mail, partner=None, context=None):
53 base_url = self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url')
54 track_url = urlparse.urljoin(
55 base_url, 'mail/track/%(mail_id)s/blank.gif?%(params)s' % {
57 'params': urllib.urlencode({'db': cr.dbname})
60 return '<img src="%s" alt=""/>' % track_url
62 def send_get_mail_body(self, cr, uid, mail, partner=None, context=None):
63 """ Override to add the tracking URL to the body. """
64 body = super(MailMail, self).send_get_mail_body(cr, uid, mail, partner=partner, context=context)
66 # generate tracking URL
67 if mail.statistics_ids:
68 tracking_url = self._get_tracking_url(cr, uid, mail, partner, context=context)
70 body = tools.append_content_to_html(body, tracking_url, plaintext=False, container_tag='div')
73 def _postprocess_sent_message(self, cr, uid, mail, context=None, mail_sent=True):
74 if mail_sent is True and mail.statistics_ids:
75 self.pool['mail.mail.statistics'].write(cr, uid, [s.id for s in mail.statistics_ids], {'sent': fields.datetime.now()}, context=context)
76 return super(MailMail, self)._postprocess_sent_message(cr, uid, mail, context=context, mail_sent=mail_sent)