diff --git a/addons/survey/tests/test_survey_invite.py b/addons/survey/tests/test_survey_invite.py index f00f73f13ef..a781e14c4cf 100644 --- a/addons/survey/tests/test_survey_invite.py +++ b/addons/survey/tests/test_survey_invite.py @@ -318,3 +318,52 @@ class TestSurveyInvite(common.TestSurveyCommon, MailCommon): answers = self.env['survey.user_input'].search([('survey_id', '=', self.survey.id)]) self.assertEqual(len(answers), 1) self.assertEqual(answers.partner_id.display_name, first_partner.display_name) + + @users('survey_user') + def test_survey_invite_with_template_attachment(self): + """ + Test that a group_survey_user can send a survey that includes an attachment from the survey invite's + email template + """ + mail_template = self.env['mail.template'].create({ + 'name': 'test mail template', + 'attachment_ids': [Command.create({ + 'name': 'some_attachment.pdf', + 'res_model': 'mail.template', + 'datas': 'test', + 'type': 'binary', + })], + }) + + user_survey = self.env['survey.survey'].create({ + 'title': 'User Created Survey', + 'access_mode': 'public', + 'users_login_required': False, + 'users_can_go_back': False, + 'question_and_page_ids': [ + Command.create({ + 'title': 'First page', + 'sequence': 1, + 'is_page': True, + 'question_type': False, + }), + Command.create({ + 'title': 'Test Free Text', + 'sequence': 2, + 'question_type': 'text_box', + }), + ] + }) + + action = user_survey.action_send_survey() + invite_form = Form(self.env[action['res_model']].with_context(action['context'])) + invite_form.send_email = True + invite_form.template_id = mail_template + invite_form.emails = 'test_survey_invite_with_template_attachment@odoo.gov' + invite = invite_form.save() + with self.mock_mail_gateway(): + invite.action_invite() + + self.assertEqual(self.env['mail.mail'].sudo().search([ + ('email_to', '=', 'test_survey_invite_with_template_attachment@odoo.gov') + ]).attachment_ids, mail_template.attachment_ids) diff --git a/addons/survey/wizard/survey_invite.py b/addons/survey/wizard/survey_invite.py index f98c894dae7..ec127c84fba 100644 --- a/addons/survey/wizard/survey_invite.py +++ b/addons/survey/wizard/survey_invite.py @@ -25,7 +25,7 @@ class SurveyInvite(models.TransientModel): # composer content attachment_ids = fields.Many2many( 'ir.attachment', 'survey_mail_compose_message_ir_attachments_rel', 'wizard_id', 'attachment_id', - string='Attachments') + string='Attachments', compute='_compute_attachment_ids', store=True, readonly=False) # origin author_id = fields.Many2one( 'res.partner', 'Author', index=True, @@ -168,6 +168,18 @@ class SurveyInvite(models.TransientModel): invite = invite.with_context(lang=langs.pop()) super(SurveyInvite, invite)._compute_body() + @api.depends('template_id') + def _compute_attachment_ids(self): + """ + 'OnChange-like' behavior used for template selection: not intended to update records when + individual attachments get added + """ + for invite in self: + if invite.template_id: + invite.attachment_ids = invite.template_id.attachment_ids + else: + invite.attachment_ids = False + # ------------------------------------------------------ # Wizard validation and send # ------------------------------------------------------