From 7bff12e87446f6bd5e71ba42b96529c1967253e4 Mon Sep 17 00:00:00 2001 From: Florian da Costa Date: Mon, 10 Feb 2020 14:06:22 +0100 Subject: [PATCH] Add possibility to notify by mail in case of attachment processing failure --- base_attachment_queue/__manifest__.py | 1 + base_attachment_queue/data/mail_template.xml | 16 ++++++++++++++++ base_attachment_queue/models/attachment.py | 19 +++++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 base_attachment_queue/data/mail_template.xml diff --git a/base_attachment_queue/__manifest__.py b/base_attachment_queue/__manifest__.py index c2837f573..9038f9a06 100644 --- a/base_attachment_queue/__manifest__.py +++ b/base_attachment_queue/__manifest__.py @@ -19,6 +19,7 @@ 'security/ir.model.access.csv', 'data/cron.xml', 'data/ir_config_parameter.xml', + 'data/mail_template.xml', ], 'demo': [ 'demo/attachment_metadata_demo.xml' diff --git a/base_attachment_queue/data/mail_template.xml b/base_attachment_queue/data/mail_template.xml new file mode 100644 index 000000000..a09a66b4b --- /dev/null +++ b/base_attachment_queue/data/mail_template.xml @@ -0,0 +1,16 @@ + + + + + ${object.failure_emails} + Attachment Failure notification + The attachment ${object.name} has failed + + Hello,

+

The attachment ${object.name} has failed with the following error message :
${object.state_message}

+

Regards,

+ ]]>
+
+ +
diff --git a/base_attachment_queue/models/attachment.py b/base_attachment_queue/models/attachment.py index 992ec3e87..18f34d47b 100644 --- a/base_attachment_queue/models/attachment.py +++ b/base_attachment_queue/models/attachment.py @@ -26,6 +26,20 @@ class IrAttachmentMetadata(models.Model): ('done', 'Done'), ], readonly=False, required=True, default='pending') state_message = fields.Text() + failure_emails = fields.Char( + compute='_compute_failure_emails', + string="Failure Emails", + help="list of email (separated by comma) which should be notified in " + "case of failure") + + def _compute_failure_emails(self): + for attach in self: + attach.failure_emails = attach._get_failure_emails() + + def _get_failure_emails(self): + # to be overriden in submodules implementing the file_type + self.ensure_one() + return "" @api.model def run_attachment_metadata_scheduler(self, domain=None): @@ -47,6 +61,8 @@ class IrAttachmentMetadata(models.Model): """ Run the process for each attachment metadata """ + failure_tmpl = self.env.ref( + 'base_attachment_queue.attachment_failure_notification') for attachment in self: with api.Environment.manage(): with registry(self.env.cr.dbname).cursor() as new_cr: @@ -63,6 +79,9 @@ class IrAttachmentMetadata(models.Model): 'state': 'failed', 'state_message': str(e), }) + emails = attach.failure_emails + if emails: + failure_tmpl.send_mail(attach.id) attach.env.cr.commit() else: vals = {