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.

160 lines
7.0 KiB

8 years ago
10 years ago
8 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
8 years ago
8 years ago
10 years ago
8 years ago
10 years ago
10 years ago
10 years ago
8 years ago
8 years ago
10 years ago
10 years ago
10 years ago
  1. # -*- coding: utf-8 -*-
  2. from openerp import SUPERUSER_ID
  3. from openerp import models
  4. from openerp import tools
  5. from openerp.osv import fields as old_fields
  6. class ResPartner(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 MailNotification(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. about = message.subject or message.record_name or 'UNDEFINED'
  75. about = '[ABOUT] %s' % about
  76. if url:
  77. about = '<a href="%s">%s</a>' % (url, about)
  78. im_text = [
  79. '_____________________',
  80. '<a href="%s">_____[open_inbox]_____</a>' % inbox_url,
  81. '%s [FROM] %s' % (message.type, author),
  82. about,
  83. ]
  84. # im_text = im_text + body.split('\n')
  85. return im_text
  86. def _notify_email(self, cr, uid, ids, message_id, force_send=False, user_signature=True, context=None):
  87. # based on addons/mail/mail_followers.py::_notify_email
  88. message = self.pool['mail.message'].browse(cr, SUPERUSER_ID, message_id, context=context)
  89. # compute partners
  90. email_pids, im_uids = self.get_recipients(cr, uid, ids, message, context=None)
  91. if email_pids:
  92. self._do_notify_email(cr, uid, email_pids, message, force_send, user_signature, context)
  93. if im_uids:
  94. self._do_notify_im(cr, uid, im_uids, message, context)
  95. return True
  96. def _do_notify_im(self, cr, uid, im_uids, message, context=None):
  97. im_text = self._message2im(cr, uid, message)
  98. user_from = self.pool['ir.model.data'].xmlid_to_res_id(cr, SUPERUSER_ID, 'im_notif.notif_user')
  99. session_obj = self.pool['im_chat.session']
  100. message_type = 'message'
  101. for user_to in im_uids:
  102. session = session_obj.session_get(cr, user_from, user_to, context=context)
  103. uuid = session.get('uuid')
  104. message_content = '\n'.join(im_text)
  105. self.pool["im_chat.message"].post(cr, SUPERUSER_ID, user_from, uuid, message_type, message_content, context=context)
  106. return True
  107. def _do_notify_email(self, cr, uid, email_pids, message, force_send=False, user_signature=True, context=None):
  108. # compute email body (signature, company data)
  109. body_html = message.body
  110. # add user signature except for mail groups, where users are usually adding their own signatures already
  111. 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
  112. 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'))
  113. if signature_company:
  114. body_html = tools.append_content_to_html(body_html, signature_company, plaintext=False, container_tag='div')
  115. # compute email references
  116. references = message.parent_id.message_id if message.parent_id else False
  117. # custom values
  118. custom_values = dict()
  119. if message.model and message.res_id and self.pool.get(message.model) and hasattr(self.pool[message.model], 'message_get_email_values'):
  120. custom_values = self.pool[message.model].message_get_email_values(cr, uid, message.res_id, message, context=context)
  121. # create email values
  122. max_recipients = 50
  123. chunks = [email_pids[x:x + max_recipients] for x in xrange(0, len(email_pids), max_recipients)]
  124. email_ids = []
  125. for chunk in chunks:
  126. mail_values = {
  127. 'mail_message_id': message.id,
  128. 'auto_delete': True,
  129. 'body_html': body_html,
  130. 'recipient_ids': [(4, id) for id in chunk],
  131. 'references': references,
  132. }
  133. mail_values.update(custom_values)
  134. email_ids.append(self.pool.get('mail.mail').create(cr, uid, mail_values, context=context))
  135. if force_send and len(chunks) < 2: # for more than 50 followers, use the queue system
  136. self.pool.get('mail.mail').send(cr, uid, email_ids, context=context)
  137. return True