Skip to content

Commit

Permalink
[FIX] website_slides: translate course invite into recipients' language
Browse files Browse the repository at this point in the history
Problem:
when adding attendees to a course, the sent email is not translated into
the recipient's language.

Fix:
Upon sending, translate the email on a per recipient basis,
but **only when** the final message to sent is exactly the same
as the template. (i.e. the body was not edited from the dialog box)

This is a compromise to permit emails being translated while not
breaking the following 2 current features:
* there can be many recipients (with different languages)
* the body is pre-filled with a template but can be edited further
  by the user directly from the dialog box.

opw-3862369
  • Loading branch information
naja628 committed May 8, 2024
1 parent b8feadf commit 150582c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
40 changes: 40 additions & 0 deletions addons/website_slides/tests/test_attendee.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,3 +514,43 @@ def test_members_invitation_expiration(self):
self.assertFalse(channel_partner_portal.exists(), 'Expired invitations should be removed, even if archived')
self.assertTrue(self.channel_partner_emp.exists(), 'Memberships with progress should never be removed, even archived.')
self.assertFalse(self.channel_partner_no_user.exists(), 'No Last Invitation Date is considered as expired for invited members')

def test_invite_email_translation(self):
"Make sure that invitation emails are translated if unchanged when adding attendees to a course"
self.env['res.lang']._activate_lang('fr_FR')
jean = self.env['res.partner'].create({'name': 'Jean', 'lang': 'fr_FR'})

template = self.env['mail.template'].create({
'subject': 'Hello',
'body_html': 'en',
})
template.lang = '{{ object.partner_id.lang }}'
template.render_model = 'slide.channel.partner'

template.with_context(lang='fr_FR').subject = 'Bonjour'
template.with_context(lang='fr_FR').body_html = 'fr'

channel = self.env['slide.channel'].create({'name': 'Test Course'})
slide_channel_jean = self.env['slide.channel.partner'].create({
'channel_id': channel.id,
'partner_id': jean.id,
})

wizard = self.env['slide.channel.invite'].create({
'send_email': True,
'partner_ids': [jean.id],
'channel_id': channel.id,
'template_id': template.id,
})

mail_vals = wizard._prepare_mail_values(slide_channel_jean)
self.assertEqual(
mail_vals['body_html'],
'fr',
"Mail body should have been translated into recipient's language"
)
self.assertEqual(
mail_vals['subject'],
'Bonjour',
"Mail subject should have been translated into recipient's language"
)
11 changes: 8 additions & 3 deletions addons/website_slides/wizard/slide_channel_invite.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,13 @@ def action_invite(self):

def _prepare_mail_values(self, slide_channel_partner):
""" Create mail specific for recipient """
subject = self._render_field('subject', slide_channel_partner.ids)[slide_channel_partner.id]
body = self._render_field('body', slide_channel_partner.ids)[slide_channel_partner.id]
set_lang, lang_opt = {}, {}
subject_same_as_template = (self.template_id and self.subject == self.template_id.subject)
if self.body_has_template_value and subject_same_as_template:
lang = self._render_lang(slide_channel_partner.ids)[slide_channel_partner.id]
set_lang, lang_opt = {'set_lang': lang}, {'lang': lang}
subject = self._render_field('subject', slide_channel_partner.ids, **set_lang)[slide_channel_partner.id]
body = self._render_field('body', slide_channel_partner.ids, **set_lang)[slide_channel_partner.id]
# post the message
mail_values = {
'attachment_ids': [(4, att.id) for att in self.attachment_ids],
Expand All @@ -112,7 +117,7 @@ def _prepare_mail_values(self, slide_channel_partner):
'company': self.env.company,
'signature': self.channel_id.user_id.signature,
}
body = self.env['ir.qweb']._render(email_layout_xmlid, template_ctx, engine='ir.qweb', minimal_qcontext=True, raise_if_not_found=False)
body = self.env['ir.qweb']._render(email_layout_xmlid, template_ctx, engine='ir.qweb', minimal_qcontext=True, raise_if_not_found=False, **lang_opt)
if body:
mail_values['body_html'] = self.env['mail.render.mixin']._replace_local_links(body)
else:
Expand Down

0 comments on commit 150582c

Please sign in to comment.