From bc3901671e913fab02c34db3fb2ac611e2e7ded6 Mon Sep 17 00:00:00 2001 From: Quoc - Pham Ngoc Date: Fri, 3 Nov 2023 15:24:51 +0700 Subject: [PATCH] [IMP] export_async_schedule : Send attachments directly --- base_export_async/models/delay_export.py | 31 +++++++++++++------ .../tests/test_base_export_async.py | 16 ++++++---- .../models/export_async_schedule.py | 17 ++++++++-- .../views/export_async_schedule_views.xml | 2 ++ 4 files changed, 48 insertions(+), 18 deletions(-) diff --git a/base_export_async/models/delay_export.py b/base_export_async/models/delay_export.py index a4590df883..8e805ef6b7 100644 --- a/base_export_async/models/delay_export.py +++ b/base_export_async/models/delay_export.py @@ -66,7 +66,9 @@ def _get_file_content(self, params): return xls.from_data(columns_headers, import_data) @api.model - def export(self, params): + def export( + self, params, mail_template=None, is_export_file_attached_to_email=False + ): """Delayed export of a file sent by email The ``params`` is a dict of parameters, contains: @@ -124,15 +126,24 @@ def export(self, params): "model_description": model_description, } ) - - self.env.ref("base_export_async.delay_export_mail_template").send_mail( - export_record.id, - email_values={ - "email_from": email_from, - "reply_to": email_from, - "recipient_ids": [(6, 0, users.mapped("partner_id").ids)], - }, - ) + email_values = { + "email_from": email_from, + "reply_to": email_from, + "recipient_ids": [(6, 0, users.mapped("partner_id").ids)], + } + if is_export_file_attached_to_email: + email_values.update({"attachments": [(name, base64.b64encode(content))]}) + + if mail_template: + mail_template.send_mail( + export_record.id, + email_values=email_values, + ) + else: + self.env.ref("base_export_async.delay_export_mail_template").send_mail( + export_record.id, + email_values=email_values, + ) @api.model def cron_delete(self): diff --git a/base_export_async/tests/test_base_export_async.py b/base_export_async/tests/test_base_export_async.py index e1b5c3579a..bf23089647 100644 --- a/base_export_async/tests/test_base_export_async.py +++ b/base_export_async/tests/test_base_export_async.py @@ -64,22 +64,26 @@ def test_export_csv(self): params = json.loads(data_csv.get("data")) mails = self.env["mail.mail"].search([]) attachments = self.env["ir.attachment"].search([]) - self.delay_export_obj.export(params) + self.delay_export_obj.export( + params, self.env.ref("base_export_async.delay_export_mail_template"), True + ) new_mail = self.env["mail.mail"].search([]) - mails - new_attachment = self.env["ir.attachment"].search([]) - attachments + new_attachments = self.env["ir.attachment"].search([]) - attachments self.assertEqual(len(new_mail), 1) - self.assertEqual(new_attachment.name, "res.partner.csv") + self.assertEqual(len(new_mail.attachment_ids), 1) + self.assertEqual(new_attachments[0].name, "res.partner.csv") def test_export_xls(self): """Check that the export generate an attachment and email""" params = json.loads(data_xls.get("data")) mails = self.env["mail.mail"].search([]) attachments = self.env["ir.attachment"].search([]) - self.delay_export_obj.export(params) + self.delay_export_obj.export(params, None, True) new_mail = self.env["mail.mail"].search([]) - mails - new_attachment = self.env["ir.attachment"].search([]) - attachments + new_attachments = self.env["ir.attachment"].search([]) - attachments self.assertEqual(len(new_mail), 1) - self.assertEqual(new_attachment.name, "res.partner.xls") + self.assertEqual(len(new_mail.attachment_ids), 1) + self.assertEqual(new_attachments[0].name, "res.partner.xls") def test_cron_delete(self): """Check that cron delete attachment after TTL""" diff --git a/export_async_schedule/models/export_async_schedule.py b/export_async_schedule/models/export_async_schedule.py index 04ac3fa706..cda0053928 100644 --- a/export_async_schedule/models/export_async_schedule.py +++ b/export_async_schedule/models/export_async_schedule.py @@ -45,7 +45,16 @@ class ExportAsyncSchedule(models.Model): default=lambda self: self.env.lang, help="Exports will be translated in this language.", ) - + mail_template_id = fields.Many2one( + "mail.template", + string="Email addresses", + default=lambda self: self.env.ref( + "base_export_async.delay_export_mail_template" + ), + ) + is_export_file_attached_to_email = fields.Boolean( + "Export file attached to email", default=False + ) # Scheduling next_execution = fields.Datetime(default=fields.Datetime.now, required=True) interval = fields.Integer(default=1, required=True) @@ -149,4 +158,8 @@ def action_export(self): for record in self: record = record.with_context(lang=record.lang) params = record._prepare_export_params() - record.env["delay.export"].with_delay().export(params) + record.env["delay.export"].with_delay().export( + params, + mail_template=record.mail_template_id, + is_export_file_attached_to_email=record.is_export_file_attached_to_email, + ) diff --git a/export_async_schedule/views/export_async_schedule_views.xml b/export_async_schedule/views/export_async_schedule_views.xml index a9a2a3dcaa..757ae14eda 100644 --- a/export_async_schedule/views/export_async_schedule_views.xml +++ b/export_async_schedule/views/export_async_schedule_views.xml @@ -43,6 +43,7 @@ name="model_id" options="{'no_open': True, 'no_create_edit': True}" /> + +