mass_mailing_stats.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. from openerp.osv import osv, fields
  22. class MailMailStats(osv.Model):
  23. """ MailMailStats models the statistics collected about emails. Those statistics
  24. are stored in a separated model and table to avoid bloating the mail_mail table
  25. with statistics values. This also allows to delete emails send with mass mailing
  26. without loosing the statistics about them. """
  27. _name = 'mail.mail.statistics'
  28. _description = 'Email Statistics'
  29. _rec_name = 'message_id'
  30. _order = 'message_id'
  31. _columns = {
  32. 'mail_mail_id': fields.many2one('mail.mail', 'Mail', ondelete='set null', select=True),
  33. 'mail_mail_id_int': fields.integer(
  34. 'Mail ID (tech)',
  35. help='ID of the related mail_mail. This field is an integer field because'
  36. 'the related mail_mail can be deleted separately from its statistics.'
  37. 'However the ID is needed for several action and controllers.'
  38. ),
  39. 'message_id': fields.char('Message-ID'),
  40. 'model': fields.char('Document model'),
  41. 'res_id': fields.integer('Document ID'),
  42. # campaign / wave data
  43. 'mass_mailing_id': fields.many2one(
  44. 'mail.mass_mailing', 'Mass Mailing',
  45. ondelete='set null',
  46. ),
  47. 'mass_mailing_campaign_id': fields.related(
  48. 'mass_mailing_id', 'mass_mailing_campaign_id',
  49. type='many2one', ondelete='set null',
  50. relation='mail.mass_mailing.campaign',
  51. string='Mass Mailing Campaign',
  52. store=True, readonly=True,
  53. ),
  54. # Bounce and tracking
  55. 'scheduled': fields.datetime('Scheduled', help='Date when the email has been created'),
  56. 'sent': fields.datetime('Sent', help='Date when the email has been sent'),
  57. 'exception': fields.datetime('Exception', help='Date of technical error leading to the email not being sent'),
  58. 'opened': fields.datetime('Opened', help='Date when the email has been opened the first time'),
  59. 'replied': fields.datetime('Replied', help='Date when this email has been replied for the first time.'),
  60. 'bounced': fields.datetime('Bounced', help='Date when this email has bounced.'),
  61. }
  62. _defaults = {
  63. 'scheduled': fields.datetime.now,
  64. }
  65. def create(self, cr, uid, values, context=None):
  66. if 'mail_mail_id' in values:
  67. values['mail_mail_id_int'] = values['mail_mail_id']
  68. res = super(MailMailStats, self).create(cr, uid, values, context=context)
  69. return res
  70. def _get_ids(self, cr, uid, ids=None, mail_mail_ids=None, mail_message_ids=None, domain=None, context=None):
  71. if not ids and mail_mail_ids:
  72. base_domain = [('mail_mail_id_int', 'in', mail_mail_ids)]
  73. elif not ids and mail_message_ids:
  74. base_domain = [('message_id', 'in', mail_message_ids)]
  75. else:
  76. base_domain = [('id', 'in', ids or [])]
  77. if domain:
  78. base_domain = ['&'] + domain + base_domain
  79. return self.search(cr, uid, base_domain, context=context)
  80. def set_opened(self, cr, uid, ids=None, mail_mail_ids=None, mail_message_ids=None, context=None):
  81. stat_ids = self._get_ids(cr, uid, ids, mail_mail_ids, mail_message_ids, [('opened', '=', False)], context)
  82. self.write(cr, uid, stat_ids, {'opened': fields.datetime.now()}, context=context)
  83. return stat_ids
  84. def set_replied(self, cr, uid, ids=None, mail_mail_ids=None, mail_message_ids=None, context=None):
  85. stat_ids = self._get_ids(cr, uid, ids, mail_mail_ids, mail_message_ids, [('replied', '=', False)], context)
  86. self.write(cr, uid, stat_ids, {'replied': fields.datetime.now()}, context=context)
  87. return stat_ids
  88. def set_bounced(self, cr, uid, ids=None, mail_mail_ids=None, mail_message_ids=None, context=None):
  89. stat_ids = self._get_ids(cr, uid, ids, mail_mail_ids, mail_message_ids, [('bounced', '=', False)], context)
  90. self.write(cr, uid, stat_ids, {'bounced': fields.datetime.now()}, context=context)
  91. return stat_ids