oca-ocb-core/odoo-bringout-oca-ocb-event/event/models/mail_template.py
Ernad Husremovic 2d3ee4855a 19.0 vanilla
2026-03-09 09:30:27 +01:00

28 lines
1.2 KiB
Python

# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, models
from odoo.fields import Domain
class MailTemplate(models.Model):
_inherit = 'mail.template'
@api.model
def _search(self, domain, *args, **kwargs):
"""Context-based hack to filter reference field in a m2o search box to emulate a domain the ORM currently does not support.
As we can not specify a domain on a reference field, we added a context
key `filter_template_on_event` on the template reference field. If this
key is set, we add our domain in the `domain` in the `_search`
method to filtrate the mail templates.
"""
if self.env.context.get('filter_template_on_event'):
domain = Domain('model', '=', 'event.registration') & Domain(domain)
return super()._search(domain, *args, **kwargs)
def unlink(self):
res = super().unlink()
domain = ('template_ref', 'in', [f"{template._name},{template.id}" for template in self])
self.env['event.mail'].sudo().search([domain]).unlink()
self.env['event.type.mail'].sudo().search([domain]).unlink()
return res