From b4d757e2117b58306696e3ae3fda9be9a9f05347 Mon Sep 17 00:00:00 2001 From: clementthomas Date: Tue, 19 Dec 2023 13:38:57 +0100 Subject: [PATCH] [NEW] event_mail_attachment --- event_mail_attachment/__init__.py | 1 + event_mail_attachment/__manifest__.py | 24 ++++++++++ event_mail_attachment/models/__init__.py | 1 + event_mail_attachment/models/mail_template.py | 48 +++++++++++++++++++ .../views/mail_template_views.xml | 27 +++++++++++ 5 files changed, 101 insertions(+) create mode 100644 event_mail_attachment/__init__.py create mode 100644 event_mail_attachment/__manifest__.py create mode 100644 event_mail_attachment/models/__init__.py create mode 100644 event_mail_attachment/models/mail_template.py create mode 100644 event_mail_attachment/views/mail_template_views.xml diff --git a/event_mail_attachment/__init__.py b/event_mail_attachment/__init__.py new file mode 100644 index 0000000..9a7e03e --- /dev/null +++ b/event_mail_attachment/__init__.py @@ -0,0 +1 @@ +from . import models \ No newline at end of file diff --git a/event_mail_attachment/__manifest__.py b/event_mail_attachment/__manifest__.py new file mode 100644 index 0000000..2d9d872 --- /dev/null +++ b/event_mail_attachment/__manifest__.py @@ -0,0 +1,24 @@ +# Copyright 2016-2020 Akretion France () +# @author: Alexis de Lattre +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +{ + "name": "Event mail attachments", + "version": "16.0.0.0.0", + "license": "AGPL-3", + "author": "Elabore", + "website": "https://www.elabore.coop", + 'summary': 'Event mail attachments', + 'description': """ +Event mail attachments +---------------------------------------------------- + +""", + "category": "", + "depends": ["ctl_training_customization"], + "data": [ + 'views/mail_template_views.xml' + ], + "installable": True, + +} diff --git a/event_mail_attachment/models/__init__.py b/event_mail_attachment/models/__init__.py new file mode 100644 index 0000000..c403d41 --- /dev/null +++ b/event_mail_attachment/models/__init__.py @@ -0,0 +1 @@ +from . import mail_template \ No newline at end of file diff --git a/event_mail_attachment/models/mail_template.py b/event_mail_attachment/models/mail_template.py new file mode 100644 index 0000000..4ee3c50 --- /dev/null +++ b/event_mail_attachment/models/mail_template.py @@ -0,0 +1,48 @@ +from odoo import _, api, Command, fields, models +from lxml import etree, html + +class MailTemplate(models.Model): + _inherit = "mail.template" + + event_attachment_name_prefix = fields.Char('Attachment name prefix', help="If there is an attachment in event registration, or in event, with a name that starts with this name, it will be attached to the mail.") + + + def generate_email(self, res_ids, fields): + res = super(MailTemplate, self).generate_email(res_ids, fields) + + self.ensure_one() + multi_mode = True + if isinstance(res_ids, int): + res_ids = [res_ids] + multi_mode = False + + for lang, (template, template_res_ids) in self._classify_per_lang(res_ids).items(): + #add reports attached to event.registration or event.event from attachment name + if template.event_attachment_name_prefix: + for res_id in template_res_ids: + event_registration = self.env['event.registration'].browse(res_id) + attachments = self.env['ir.attachment'].search([ + ('res_model','=','event.registration'), + ('res_id','=',res_id), + ('name','like',template.event_attachment_name_prefix)]) + attachments |= self.env['ir.attachment'].search([ + ('res_model','=','event.event'), + ('res_id','=',event_registration.event_id.id), + ('name','like',template.event_attachment_name_prefix)]) + + attachments_res = [(attachment.name, attachment.datas) for attachment in attachments] + + if multi_mode: + if res_id in res: + if not 'attachments' in res[res_id]: + res[res_id]['attachments'] = attachments_res + else: + res[res_id]['attachments'].extend(attachments_res) + else: + if not 'attachments' in res: + res['attachments'] = attachments_res + else: + res['attachments'].extend(attachments_res) + + + return res diff --git a/event_mail_attachment/views/mail_template_views.xml b/event_mail_attachment/views/mail_template_views.xml new file mode 100644 index 0000000..b2646da --- /dev/null +++ b/event_mail_attachment/views/mail_template_views.xml @@ -0,0 +1,27 @@ + + + + + email.template.form.event.mail.attachment + + mail.template + + + + + + + + + email.template.tree.event.mail.attachment + + mail.template + + + + + + + + +