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.

70 lines
2.9 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Therp BV <http://therp.nl>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from lxml import etree
  5. from openerp import api, fields, models
  6. class MailNotification(models.Model):
  7. _inherit = 'mail.notification'
  8. record = fields.Reference(
  9. selection=lambda self: [
  10. (m.model, m.name) for m in self.env['ir.model'].search([])
  11. ],
  12. compute='_compute_record')
  13. record_access_link = fields.Char(compute='_compute_record')
  14. @api.multi
  15. def _notify_email(self, message_id, force_send=False, user_signature=True):
  16. if not self.mapped('message_id.subtype_id.template_id'):
  17. return super(MailNotification, self)._notify_email(
  18. message_id, force_send=force_send,
  19. user_signature=user_signature)
  20. message_ids = []
  21. for this in self:
  22. if not this.mapped('message_id.subtype_id.template_id'):
  23. super(MailNotification, this)._notify_email(
  24. message_id, force_send=force_send,
  25. user_signature=user_signature)
  26. continue
  27. message = this.message_id
  28. if not this.get_partners_to_email(message):
  29. continue
  30. custom_values = {
  31. 'references': message.parent_id.message_id,
  32. 'author_id': message.author_id.id
  33. }
  34. if message.res_id and hasattr(
  35. self.env[message.model], 'message_get_email_values'
  36. ):
  37. message_values = self.env[message.model].browse(
  38. message.res_id
  39. ).message_get_email_values(message)
  40. # message_get_email_values is guessed to @api.one
  41. if message_values and isinstance(message_values, list):
  42. message_values = message_values[0]
  43. custom_values.update(message_values)
  44. message_id = message.subtype_id.template_id.send_mail(this.id)
  45. if 'mail_message_id' in custom_values:
  46. custom_values.pop('mail_message_id')
  47. self.env['mail.mail'].browse(message_id).write(custom_values)
  48. message_ids.append(message_id)
  49. return message_ids or True
  50. @api.multi
  51. def _compute_record(self):
  52. for this in self:
  53. if not this.message_id.model or not this.message_id.res_id:
  54. continue
  55. this.record = self.env[this.message_id.model].browse(
  56. this.message_id.res_id)
  57. link_html = self.env['mail.mail']._get_partner_access_link(
  58. self.env['mail.mail'].new({
  59. 'notification': True,
  60. 'mail_message_id': this.message_id.id,
  61. }),
  62. this.partner_id
  63. )
  64. for a in etree.HTML(link_html or '<html/>').xpath('//a[@href]'):
  65. this.record_access_link = a.get('href')