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.

165 lines
7.3 KiB

10 years ago
10 years ago
10 years ago
10 years ago
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. class mail_notification(models.Model):
  21. _inherit = 'mail.notification'
  22. def get_recipients(self, cr, uid, ids, message, context=None):
  23. # based on addons/mail/mail_followers.py::get_partners_to_email
  24. """ Return the list of partners to notify, based on their preferences.
  25. :param browse_record message: mail.message to notify
  26. :param list partners_to_notify: optional list of partner ids restricting
  27. the notifications to process
  28. """
  29. email_pids = []
  30. im_uids = []
  31. for notification in self.browse(cr, uid, ids, context=context):
  32. if notification.is_read:
  33. continue
  34. partner = notification.partner_id
  35. # Do not send to partners without email address defined
  36. if not partner.email:
  37. continue
  38. # Do not send to partners having same email address than the author (can cause loops or bounce effect due to messy database)
  39. if message.author_id and message.author_id.email == partner.email:
  40. continue
  41. # Partner does not want to receive any emails or is opt-out
  42. n = partner.notify_email
  43. if n == 'none':
  44. continue
  45. if n == 'always':
  46. email_pids.append(partner.id)
  47. continue
  48. send_email = False
  49. for user in partner.user_ids:
  50. if user.im_status=='offline':
  51. if n != 'im':
  52. send_email = True
  53. else:
  54. im_uids.append(user.id)
  55. if n == 'im_and_email':
  56. send_email = True
  57. if not len(partner.user_ids):
  58. # send notification to partner, that doesn't have odoo account, but has "im*" value in notify_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, cr, uid, message):
  64. inbox_action = self.pool['ir.model.data'].xmlid_to_res_id(cr, SUPERUSER_ID, 'mail.mail_inboxfeeds')
  65. inbox_url = '#menu_id=%s' % inbox_action
  66. url = None
  67. if message.res_id:
  68. url = '#id=%s&model=%s&view_type=form' % (
  69. message.res_id,
  70. message.model
  71. )
  72. author = message.author_id and message.author_id.name_get()
  73. author = author and author[0][1] or message.email_from
  74. #body = html2plaintext(message.body)[:100] or ''
  75. mtype = {'email': _('Email'),
  76. 'comment': _('Comment'),
  77. 'notification': _('System notification'),
  78. }.get(message.type, '')
  79. about = message.subject or message.record_name or 'UNDEFINED'
  80. about = '[ABOUT] %s' % about
  81. if url:
  82. about = '<a href="%s">%s</a>' % (url, about)
  83. im_text = [
  84. '_____________________',
  85. '<a href="%s">_____[open_inbox]_____</a>' % inbox_url,
  86. '%s [FROM] %s' % (message.type, author),
  87. about,
  88. ]
  89. #im_text = im_text + body.split('\n')
  90. return im_text
  91. def _notify_email(self, cr, uid, ids, message_id, force_send=False, user_signature=True, context=None):
  92. # based on addons/mail/mail_followers.py::_notify_email
  93. message = self.pool['mail.message'].browse(cr, SUPERUSER_ID, message_id, context=context)
  94. # compute partners
  95. email_pids, im_uids = self.get_recipients(cr, uid, ids, message, context=None)
  96. if email_pids:
  97. self._do_notify_email(cr, uid, email_pids, message, force_send, user_signature, context)
  98. if im_uids:
  99. self._do_notify_im(cr, uid, im_uids, message, context)
  100. return True
  101. def _do_notify_im(self, cr, uid, im_uids, message, context=None):
  102. im_text = self._message2im(cr, uid, message)
  103. user_from = self.pool['ir.model.data'].xmlid_to_res_id(cr, SUPERUSER_ID, 'im_notif.notif_user')
  104. session_obj = self.pool['im_chat.session']
  105. message_type = 'message'
  106. for user_to in im_uids:
  107. session = session_obj.session_get(cr, user_from, user_to, context=context)
  108. uuid = session.get('uuid')
  109. message_content = '\n'.join(im_text)
  110. message_id = self.pool["im_chat.message"].post(cr, SUPERUSER_ID, user_from, uuid, message_type, message_content, context=context)
  111. return True
  112. def _do_notify_email(self, cr, uid, email_pids, message, force_send=False, user_signature=True, context=None):
  113. # compute email body (signature, company data)
  114. body_html = message.body
  115. # add user signature except for mail groups, where users are usually adding their own signatures already
  116. 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
  117. 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'))
  118. if signature_company:
  119. body_html = tools.append_content_to_html(body_html, signature_company, plaintext=False, container_tag='div')
  120. # compute email references
  121. references = message.parent_id.message_id if message.parent_id else False
  122. # custom values
  123. custom_values = dict()
  124. if message.model and message.res_id and self.pool.get(message.model) and hasattr(self.pool[message.model], 'message_get_email_values'):
  125. custom_values = self.pool[message.model].message_get_email_values(cr, uid, message.res_id, message, context=context)
  126. # create email values
  127. max_recipients = 50
  128. chunks = [email_pids[x:x + max_recipients] for x in xrange(0, len(email_pids), max_recipients)]
  129. email_ids = []
  130. for chunk in chunks:
  131. mail_values = {
  132. 'mail_message_id': message.id,
  133. 'auto_delete': True,
  134. 'body_html': body_html,
  135. 'recipient_ids': [(4, id) for id in chunk],
  136. 'references': references,
  137. }
  138. mail_values.update(custom_values)
  139. email_ids.append(self.pool.get('mail.mail').create(cr, uid, mail_values, context=context))
  140. if force_send and len(chunks) < 2: # for more than 50 followers, use the queue system
  141. self.pool.get('mail.mail').send(cr, uid, email_ids, context=context)
  142. return True