Skip to content

Commit

Permalink
[ADD] base_export_async: send mail using mail template
Browse files Browse the repository at this point in the history
module **base_export_async**:
send mail using mail template for Asynchronous Export data

Co-authored-by: vincent-cowboy <[email protected]>
  • Loading branch information
paras-tsd and vincent-cowboy committed Aug 9, 2023
1 parent 99387de commit 58378c0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
3 changes: 2 additions & 1 deletion base_export_async/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'summary': """
Asynchronous export with job queue
""",
'version': '12.0.1.1.0',
'version': '12.0.1.1.1',
'license': 'AGPL-3',
'author': 'ACSONE SA/NV, Odoo Community Association (OCA)',
'website': 'https://github.com/OCA/queue',
Expand All @@ -20,6 +20,7 @@
'security/ir_rule.xml',
'data/config_parameter.xml',
'data/cron.xml',
'data/mail_template_data.xml',
],
'demo': [
],
Expand Down
19 changes: 19 additions & 0 deletions base_export_async/data/mail_template_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">
<record id="delay_export_mail_template" model="mail.template">
<field name="name">Delay Export</field>
<field
name="subject"
>Export ${object.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">
<p>Your export is available <a href="${object.url}">here</a>.</p>
<p>It will be automatically deleted the ${object.expiration_date}.</p>
<br />
<p><span
style="color: #808080;"
>This is an automated message please do not reply.</span></p>
</field>
</record>
</odoo>
41 changes: 24 additions & 17 deletions base_export_async/models/delay_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class DelayExport(models.Model):
_description = 'Asynchronous Export'

user_ids = fields.Many2many('res.users', string='Users', index=True)
model_description = fields.Char()
url = fields.Char()
expiration_date = fields.Date()

@api.model
def delay_export(self, data):
Expand Down Expand Up @@ -116,26 +119,30 @@ def export(self, params):
expiration_date = fields.Date.to_string(
date_today + relativedelta(days=+int(time_to_live)))

# TODO : move to email template
odoo_bot = self.sudo().env.ref("base.partner_root")
email_from = odoo_bot.email
model_description = self.env[model_name]._description
self.env['mail.mail'].create({
'email_from': email_from,
'reply_to': email_from,
'recipient_ids': [(6, 0, users.mapped('partner_id').ids)],
'subject': _("Export {} {}").format(
model_description, fields.Date.to_string(fields.Date.today())),
'body_html': _("""
<p>Your export is available <a href="{}">here</a>.</p>
<p>It will be automatically deleted the {}.</p>
<p>&nbsp;</p>
<p><span style="color: #808080;">
This is an automated message please do not reply.
</span></p>
""").format(url, expiration_date),
'auto_delete': True,
})
mail_template = self.env.ref(
'base_export_async.delay_export_mail_template',
raise_if_not_found=False
)
if mail_template:
export_record.write(
{
"url": url,
"expiration_date": expiration_date,
"model_description": model_description,
}
)
mail_template.send_mail(
export_record.id,
force_send=False,
email_values={
"email_from": email_from,
"reply_to": email_from,
"recipient_ids": [(6, 0, users.mapped("partner_id").ids)],
},
)

@api.model
def cron_delete(self):
Expand Down

0 comments on commit 58378c0

Please sign in to comment.