Skip to content

Commit

Permalink
Improvement of export_async_schedule : Send attachments directly
Browse files Browse the repository at this point in the history
  • Loading branch information
quoc-pn committed Nov 2, 2023
1 parent 306093d commit 9944719
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
31 changes: 21 additions & 10 deletions base_export_async/models/delay_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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))]})

Check warning on line 135 in base_export_async/models/delay_export.py

View check run for this annotation

Codecov / codecov/patch

base_export_async/models/delay_export.py#L135

Added line #L135 was not covered by tests

if mail_template:
mail_template.send_mail(

Check warning on line 138 in base_export_async/models/delay_export.py

View check run for this annotation

Codecov / codecov/patch

base_export_async/models/delay_export.py#L138

Added line #L138 was not covered by tests
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):
Expand Down
17 changes: 15 additions & 2 deletions export_async_schedule/models/export_async_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
)
2 changes: 2 additions & 0 deletions export_async_schedule/views/export_async_schedule_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
name="model_id"
options="{'no_open': True, 'no_create_edit': True}"
/>
<field name="mail_template_id" />
<field name="model_name" invisible="1" />
<field
name="user_ids"
Expand All @@ -61,6 +62,7 @@
/>
<field name="export_format" />
<field name="import_compat" />
<field name="is_export_file_attached_to_email" />
</group>
<group name="scheduling" string="Scheduling">
<field name="next_execution" />
Expand Down

0 comments on commit 9944719

Please sign in to comment.