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.2 KiB

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. _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, 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. print 'recipients', email_pids, im_uids
  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