mail_template_preview.py 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models
  4. from odoo.exceptions import UserError
  5. class MailTemplatePreview(models.TransientModel):
  6. _name = 'mail.template.preview'
  7. _description = 'Email Template Preview'
  8. _MAIL_TEMPLATE_FIELDS = ['subject', 'body_html', 'email_from', 'email_to',
  9. 'email_cc', 'reply_to', 'scheduled_date', 'attachment_ids']
  10. @api.model
  11. def _selection_target_model(self):
  12. return [(model.model, model.name) for model in self.env['ir.model'].sudo().search([])]
  13. @api.model
  14. def _selection_languages(self):
  15. return self.env['res.lang'].get_installed()
  16. @api.model
  17. def default_get(self, fields):
  18. result = super(MailTemplatePreview, self).default_get(fields)
  19. if not result.get('mail_template_id') or 'resource_ref' not in fields:
  20. return result
  21. mail_template = self.env['mail.template'].browse(result['mail_template_id']).sudo()
  22. model = mail_template.model
  23. res = self.env[model].search([], limit=1)
  24. if res:
  25. result['resource_ref'] = '%s,%s' % (model, res.id)
  26. return result
  27. mail_template_id = fields.Many2one('mail.template', string='Related Mail Template', required=True)
  28. model_id = fields.Many2one('ir.model', string='Targeted model', related="mail_template_id.model_id")
  29. resource_ref = fields.Reference(string='Record', selection='_selection_target_model')
  30. lang = fields.Selection(_selection_languages, string='Template Preview Language')
  31. no_record = fields.Boolean('No Record', compute='_compute_no_record')
  32. error_msg = fields.Char('Error Message', readonly=True)
  33. # Fields same than the mail.template model, computed with resource_ref and lang
  34. subject = fields.Char('Subject', compute='_compute_mail_template_fields')
  35. email_from = fields.Char('From', compute='_compute_mail_template_fields', help="Sender address")
  36. email_to = fields.Char('To', compute='_compute_mail_template_fields',
  37. help="Comma-separated recipient addresses")
  38. email_cc = fields.Char('Cc', compute='_compute_mail_template_fields', help="Carbon copy recipients")
  39. reply_to = fields.Char('Reply-To', compute='_compute_mail_template_fields', help="Preferred response address")
  40. scheduled_date = fields.Char('Scheduled Date', compute='_compute_mail_template_fields',
  41. help="The queue manager will send the email after the date")
  42. body_html = fields.Html('Body', compute='_compute_mail_template_fields', sanitize=False)
  43. attachment_ids = fields.Many2many('ir.attachment', 'Attachments', compute='_compute_mail_template_fields')
  44. # Extra fields info generated by generate_email
  45. partner_ids = fields.Many2many('res.partner', string='Recipients', compute='_compute_mail_template_fields')
  46. @api.depends('model_id')
  47. def _compute_no_record(self):
  48. for preview, preview_sudo in zip(self, self.sudo()):
  49. model_id = preview_sudo.model_id
  50. preview.no_record = not model_id or not self.env[model_id.model].search_count([])
  51. @api.depends('lang', 'resource_ref')
  52. def _compute_mail_template_fields(self):
  53. """ Preview the mail template (body, subject, ...) depending of the language and
  54. the record reference, more precisely the record id for the defined model of the mail template.
  55. If no record id is selectable/set, the inline_template placeholders won't be replace in the display information. """
  56. copy_depends_values = {'lang': self.lang}
  57. mail_template = self.mail_template_id.with_context(lang=self.lang)
  58. try:
  59. if not self.resource_ref:
  60. self._set_mail_attributes()
  61. else:
  62. copy_depends_values['resource_ref'] = '%s,%s' % (self.resource_ref._name, self.resource_ref.id)
  63. mail_values = mail_template.with_context(template_preview_lang=self.lang).generate_email(
  64. self.resource_ref.id, self._MAIL_TEMPLATE_FIELDS + ['partner_to'])
  65. self._set_mail_attributes(values=mail_values)
  66. self.error_msg = False
  67. except UserError as user_error:
  68. self._set_mail_attributes()
  69. self.error_msg = user_error.args[0]
  70. finally:
  71. # Avoid to be change by a cache invalidation (in generate_mail), e.g. Quotation / Order report
  72. for key, value in copy_depends_values.items():
  73. self[key] = value
  74. def _set_mail_attributes(self, values=None):
  75. for field in self._MAIL_TEMPLATE_FIELDS:
  76. field_value = values.get(field, False) if values else self.mail_template_id[field]
  77. self[field] = field_value
  78. self.partner_ids = values.get('partner_ids', False) if values else False