Skip to content

Commit

Permalink
[14.0][IMP] export_async_schedule : Send attachments directly
Browse files Browse the repository at this point in the history
  • Loading branch information
quoc-pn committed Dec 28, 2023
1 parent 306093d commit ee429fb
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 15 deletions.
2 changes: 1 addition & 1 deletion base_export_async/data/mail_template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<field name="name">Delay Export</field>
<field
name="subject"
>Export ${object.model_description} ${datetime.date.today()}</field>
>Export ${object.sudo().model_description} ${datetime.date.today()}</field>
<field name="model_id" ref="base_export_async.model_delay_export" />
<field name="auto_delete" eval="True" />
<field name="body_html" type="html">
Expand Down
29 changes: 22 additions & 7 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,14 +126,27 @@ def export(self, params):
"model_description": model_description,
}
)
email_values = {
"email_from": email_from,
"reply_to": email_from,
"recipient_ids": [(6, 0, users.mapped("partner_id").ids)],
}

email_template = (
mail_template
if mail_template
else self.env.ref("base_export_async.delay_export_mail_template")
)

self.env.ref("base_export_async.delay_export_mail_template").send_mail(
if is_export_file_attached_to_email:
attachment_ids = attachment.ids
if email_template and email_template.attachment_ids:
attachment_ids += email_template.attachment_ids.ids

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

View check run for this annotation

Codecov / codecov/patch

base_export_async/models/delay_export.py#L144

Added line #L144 was not covered by tests
attachment_ids = [(4, aid) for aid in attachment_ids]
email_values.update({"attachment_ids": attachment_ids})
email_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_values,
)

@api.model
Expand Down
14 changes: 12 additions & 2 deletions base_export_async/security/ir_rule.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">
<record id="delay_export_user_only" model="ir.rule">
<field name="name">Only user can read delay.export</field>
<record id="delay_export_user" model="ir.rule">
<field name="name">Recipient can read delay.export</field>
<field name="model_id" ref="model_delay_export" />
<field name="groups" eval="[(4, ref('base.group_user'))]" />
<field name="perm_read" eval="True" />
Expand All @@ -10,4 +10,14 @@
<field name="perm_unlink" eval="False" />
<field name="domain_force">[('user_ids', 'in', user.id)]</field>
</record>
<record id="delay_export_admin" model="ir.rule">
<field name="name">Admin / Settings can read all delay.export</field>
<field name="model_id" ref="model_delay_export" />
<field name="groups" eval="[(4, ref('base.group_system'))]" />
<field name="perm_read" eval="True" />
<field name="perm_create" eval="False" />
<field name="perm_write" eval="False" />
<field name="perm_unlink" eval="False" />
<field name="domain_force">[(1, '=', 1)]</field>
</record>
</odoo>
11 changes: 11 additions & 0 deletions base_export_async/tests/test_base_export_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ def test_export_xls(self):
self.assertEqual(len(new_mail), 1)
self.assertEqual(new_attachment.name, "res.partner.xls")

def test_export_mail_attachments(self):
"""Check that the export generate an email and attached the generated attachment"""
params = json.loads(data_csv.get("data"))
mails = self.env["mail.mail"].search([])
self.delay_export_obj.export(
params, mail_template=None, is_export_file_attached_to_email=True
)
new_mail = self.env["mail.mail"].search([]) - mails
self.assertEqual(len(new_mail.attachment_ids), 1)
self.assertEqual(new_mail.attachment_ids.name, "res.partner.csv")

def test_cron_delete(self):
"""Check that cron delete attachment after TTL"""
params = json.loads(data_csv.get("data"))
Expand Down
39 changes: 34 additions & 5 deletions export_async_schedule/models/export_async_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ class ExportAsyncSchedule(models.Model):
comodel_name="ir.model", required=True, ondelete="cascade"
)
model_name = fields.Char(related="model_id.model", string="Model Name")
user_ids = fields.Many2many(
string="Recipients", comodel_name="res.users", required=True
)
user_ids = fields.Many2many(string="Recipients", comodel_name="res.users")
domain = fields.Char(string="Export Domain", default=[])
ir_export_id = fields.Many2one(
comodel_name="ir.exports",
Expand All @@ -45,7 +43,25 @@ 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 Template",
default=lambda self: self.env.ref(
"base_export_async.delay_export_mail_template"
),
context=lambda env: {
"default_model_id": env.ref("base_export_async.model_delay_export").id
},
)
is_mail_template_has_recipients = fields.Boolean(
compute="_compute_is_mail_template_has_recipients"
)
is_mail_template_has_recipients = fields.Boolean(
compute="_compute_is_mail_template_has_recipients"
)
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 All @@ -62,6 +78,15 @@ class ExportAsyncSchedule(models.Model):
)
end_of_month = fields.Boolean()

@api.depends(
"mail_template_id", "mail_template_id.email_to", "mail_template_id.partner_to"
)
def _compute_is_mail_template_has_recipients(self):
for record in self:
record.is_mail_template_has_recipients = record.mail_template_id and (

Check warning on line 86 in export_async_schedule/models/export_async_schedule.py

View check run for this annotation

Codecov / codecov/patch

export_async_schedule/models/export_async_schedule.py#L86

Added line #L86 was not covered by tests
record.mail_template_id.email_to or record.mail_template_id.partner_to
)

def name_get(self):
result = []
for record in self:
Expand Down Expand Up @@ -149,4 +174,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,
)
7 changes: 7 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,11 +43,17 @@
name="model_id"
options="{'no_open': True, 'no_create_edit': True}"
/>
<field
name="mail_template_id"
domain="[('model_id.model','=', 'delay.export')]"
/>
<field name="model_name" invisible="1" />
<field name="is_mail_template_has_recipients" invisible="1" />
<field
name="user_ids"
widget="many2many_tags"
options="{'no_open': True, 'no_create_edit': True}"
attrs="{'required': [('is_mail_template_has_recipients', '=', False)]}"
/>
<field name="lang" />
<field
Expand All @@ -61,6 +67,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 ee429fb

Please sign in to comment.