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.

69 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. }
  33. if message.res_id and hasattr(
  34. self.env[message.model], 'message_get_email_values'
  35. ):
  36. message_values = self.env[message.model].browse(
  37. message.res_id
  38. ).message_get_email_values(message)
  39. # message_get_email_values is guessed to @api.one
  40. if message_values and isinstance(message_values, list):
  41. message_values = message_values[0]
  42. custom_values.update(message_values)
  43. message_id = message.subtype_id.template_id.send_mail(this.id)
  44. if 'mail_message_id' in custom_values:
  45. custom_values.pop('mail_message_id')
  46. self.env['mail.mail'].browse(message_id).write(custom_values)
  47. message_ids.append(message_id)
  48. return message_ids or True
  49. @api.multi
  50. def _compute_record(self):
  51. for this in self:
  52. if not this.message_id.model or not this.message_id.res_id:
  53. continue
  54. this.record = self.env[this.message_id.model].browse(
  55. this.message_id.res_id)
  56. link_html = self.env['mail.mail']._get_partner_access_link(
  57. self.env['mail.mail'].new({
  58. 'notification': True,
  59. 'mail_message_id': this.message_id.id,
  60. }),
  61. this.partner_id
  62. )
  63. for a in etree.HTML(link_html or '<html/>').xpath('//a[@href]'):
  64. this.record_access_link = a.get('href')