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.

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