From 775e8a9f2de3370e9947a612097ead65afd3b7ed Mon Sep 17 00:00:00 2001 From: XavierDo Date: Mon, 1 Oct 2018 14:59:41 +0200 Subject: [PATCH] [FIX] mail: make guideline template a qweb view The mail template mail_template_channel_send_guidelines should only be used from send_guidelines action. Anyway, with mail_template on activity and all demo done to show this mechanic on contacts, trying to render this one will generate a traceback. This fix transforms the mail_template to qweb view so that it won't be available in mail template. --- addons/mail/data/mail_data.xml | 18 +++++------------- addons/mail/models/mail_channel.py | 20 ++++++++++++-------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/addons/mail/data/mail_data.xml b/addons/mail/data/mail_data.xml index 2f2d36f62f2..a664838b5a9 100644 --- a/addons/mail/data/mail_data.xml +++ b/addons/mail/data/mail_data.xml @@ -364,23 +364,15 @@ - - Partner: Send channel guidelines - - ${object.company_id.catchall or object.company_id.email|safe} - ${object.id} - Guidelines of channel ${ctx['channel'].name} - + diff --git a/addons/mail/models/mail_channel.py b/addons/mail/models/mail_channel.py index d2a375f4fc9..3dcfb6f4436 100644 --- a/addons/mail/models/mail_channel.py +++ b/addons/mail/models/mail_channel.py @@ -425,7 +425,7 @@ class Channel(models.Model): if self.env.user in self.moderator_ids or self.env.user.has_group('base.group_system'): success = self._send_guidelines(self.channel_partner_ids) if not success: - raise UserError('Template "mail.mail_template_channel_send_guidelines" was not found. No email has been sent. Please contact an administrator to fix this issue.') + raise UserError('View "mail.mail_channel_send_guidelines" was not found. No email has been sent. Please contact an administrator to fix this issue.') else: raise UserError("Only an administrator or a moderator can send guidelines to channel members!") @@ -434,19 +434,23 @@ class Channel(models.Model): """ Send guidelines of a given channel. Returns False if template used for guidelines not found. Caller may have to handle this return value. """ self.ensure_one() - template = self.env.ref('mail.mail_template_channel_send_guidelines', raise_if_not_found=False) - if not template: - _logger.warning('Template "mail.mail_template_channel_send_guidelines" was not found.') + view = self.env.ref('mail.mail_channel_send_guidelines', raise_if_not_found=False) + if not view: + _logger.warning('View "mail.mail_channel_send_guidelines" was not found.') return False banned_emails = self.env['mail.moderation'].sudo().search([ ('status', '=', 'ban'), ('channel_id', 'in', self.ids) ]).mapped('email') for partner in partners.filtered(lambda p: p.email and not (p.email in banned_emails)): - # the sudo is needed because because send_mail will create a message (a mail). As the template is - # linked to res.partner model the current user could not have the rights to modify this model. It - # means it could prevent users to create the mail.message necessary for the mail.mail. - template.with_context(lang=partner.lang, channel=self).sudo().send_mail(partner.id) + create_values = { + 'body_html': view.render({'channel': self, 'partner': partner}, engine='ir.qweb', minimal_qcontext=True), + 'subject': "Guidelines of channel %s" % self.name, + 'email_from': partner.company_id.catchall or partner.company_id.email, + 'recipient_ids': [(4, partner.id)] + } + mail = self.env['mail.mail'].create(create_values) + mail.send() return True @api.multi