You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
6.9 KiB

10 years ago
10 years ago
  1. import openerp
  2. from openerp import api, models, fields, SUPERUSER_ID, tools
  3. from openerp.osv import fields as old_fields
  4. from openerp.tools import html2plaintext
  5. from openerp.tools.translate import _
  6. class res_partner(models.Model):
  7. _inherit = 'res.partner'
  8. _columns = {
  9. 'notify_email': old_fields.selection([
  10. ('none', 'Never'),
  11. ('im', 'Only IM (if online)'),
  12. ('im_xor_email', 'IM (if online) + email (if offline)'),
  13. ('im_and_email', 'IM (if online) + email'),
  14. ('always', 'Only emails'),
  15. ], 'Receive Inbox Notifications by Email, IM', required=True,
  16. oldname='notification_email_send',
  17. help="Policy to receive emails, IM for new messages pushed to your personal Inbox. IM can be used only for partners with odoo user account"
  18. ),
  19. }
  20. _defaults = {
  21. 'notify_email': lambda *args: 'always'
  22. }
  23. class mail_notification(models.Model):
  24. _inherit = 'mail.notification'
  25. def get_recipients(self, cr, uid, ids, message, context=None):
  26. # based on addons/mail/mail_followers.py::get_partners_to_email
  27. """ Return the list of partners to notify, based on their preferences.
  28. :param browse_record message: mail.message to notify
  29. :param list partners_to_notify: optional list of partner ids restricting
  30. the notifications to process
  31. """
  32. email_pids = []
  33. im_uids = []
  34. for notification in self.browse(cr, uid, ids, context=context):
  35. if notification.is_read:
  36. continue
  37. partner = notification.partner_id
  38. # Do not send to partners without email address defined
  39. if not partner.email:
  40. continue
  41. # Do not send to partners having same email address than the author (can cause loops or bounce effect due to messy database)
  42. if message.author_id and message.author_id.email == partner.email:
  43. continue
  44. # Partner does not want to receive any emails or is opt-out
  45. n = partner.notify_email
  46. if n == 'none':
  47. continue
  48. if n == 'always':
  49. email_pids.append(partner.id)
  50. continue
  51. send_email = False
  52. for user in partner.user_ids:
  53. if user.im_status=='offline':
  54. if n != 'im':
  55. send_email = True
  56. else:
  57. im_uids.append(user.id)
  58. if n == 'im_and_email':
  59. send_email = True
  60. if send_email:
  61. email_pids.append(partner.id)
  62. return email_pids, im_uids
  63. def _message2im(self, message):
  64. url = 'ODOO_REF#id=%s&model=%s&view_type=form' % (
  65. message.res_id,
  66. message.model
  67. )
  68. author = message.author_id and message.author_id.name_get()
  69. author = author and author[0][1] or message.email_from
  70. body = html2plaintext(message.body)[:100] or ''
  71. mtype = {'email': _('Email'),
  72. 'comment': _('Comment'),
  73. 'notification': _('System notification'),
  74. }.get(message.type, '')
  75. im_text = [
  76. '_____________________',
  77. '_____________________',
  78. '%s FROM %s' % (message.type, author),
  79. 'ABOUT %s: %s' % (message.record_name or '', url)
  80. ]
  81. im_text = im_text + body.split('\n')
  82. return im_text
  83. def _notify_email(self, cr, uid, ids, message_id, force_send=False, user_signature=True, context=None):
  84. # based on addons/mail/mail_followers.py::_notify_email
  85. message = self.pool['mail.message'].browse(cr, SUPERUSER_ID, message_id, context=context)
  86. # compute partners
  87. email_pids, im_uids = self.get_recipients(cr, uid, ids, message, context=None)
  88. print 'recipients', email_pids, im_uids
  89. if email_pids:
  90. self._do_notify_email(cr, uid, email_pids, message, force_send, user_signature, context)
  91. if im_uids:
  92. self._do_notify_im(cr, uid, im_uids, message, context)
  93. return True
  94. def _do_notify_im(self, cr, uid, im_uids, message, context=None):
  95. im_text = self._message2im(message)
  96. user_from = self.pool['ir.model.data'].xmlid_to_res_id(cr, SUPERUSER_ID, 'im_notif.notif_user')
  97. session_obj = self.pool['im_chat.session']
  98. message_type = 'message'
  99. for user_to in im_uids:
  100. session = session_obj.session_get(cr, user_from, user_to, context=context)
  101. uuid = session.get('uuid')
  102. message_content = '\n'.join(im_text)
  103. message_id = self.pool["im_chat.message"].post(cr, SUPERUSER_ID, user_from, uuid, message_type, message_content, context=context)
  104. return True
  105. def _do_notify_email(self, cr, uid, email_pids, message, force_send=False, user_signature=True, context=None):
  106. # compute email body (signature, company data)
  107. body_html = message.body
  108. # add user signature except for mail groups, where users are usually adding their own signatures already
  109. user_id = message.author_id and message.author_id.user_ids and message.author_id.user_ids[0] and message.author_id.user_ids[0].id or None
  110. signature_company = self.get_signature_footer(cr, uid, user_id, res_model=message.model, res_id=message.res_id, context=context, user_signature=(user_signature and message.model != 'mail.group'))
  111. if signature_company:
  112. body_html = tools.append_content_to_html(body_html, signature_company, plaintext=False, container_tag='div')
  113. # compute email references
  114. references = message.parent_id.message_id if message.parent_id else False
  115. # custom values
  116. custom_values = dict()
  117. if message.model and message.res_id and self.pool.get(message.model) and hasattr(self.pool[message.model], 'message_get_email_values'):
  118. custom_values = self.pool[message.model].message_get_email_values(cr, uid, message.res_id, message, context=context)
  119. # create email values
  120. max_recipients = 50
  121. chunks = [email_pids[x:x + max_recipients] for x in xrange(0, len(email_pids), max_recipients)]
  122. email_ids = []
  123. for chunk in chunks:
  124. mail_values = {
  125. 'mail_message_id': message.id,
  126. 'auto_delete': True,
  127. 'body_html': body_html,
  128. 'recipient_ids': [(4, id) for id in chunk],
  129. 'references': references,
  130. }
  131. mail_values.update(custom_values)
  132. email_ids.append(self.pool.get('mail.mail').create(cr, uid, mail_values, context=context))
  133. if force_send and len(chunks) < 2: # for more than 50 followers, use the queue system
  134. self.pool.get('mail.mail').send(cr, uid, email_ids, context=context)
  135. return True