diff --git a/partner_communication/i18n/de_DE.po b/partner_communication/i18n/de_DE.po index a9e6d93c4..3b0394b6f 100644 --- a/partner_communication/i18n/de_DE.po +++ b/partner_communication/i18n/de_DE.po @@ -1750,3 +1750,10 @@ msgstr "Vorschau" #: model:mail.template,subject:partner_communication.default_communication_template msgid "{{object.subject or ' '}}" msgstr "" + +#. module: partner_communication +#. odoo-python +#: code:addons/partner_communication/wizards/email_template_preview_view.py:0 +msgid "" +"No communication uses this template yet. Choose one in Record to preview it." +msgstr "Noch keine Kommunikation verwendet diese Vorlage. Wählen Sie eine unter Datensatz, um sie in der Vorschau anzuzeigen." diff --git a/partner_communication/i18n/fr_CH.po b/partner_communication/i18n/fr_CH.po index 7cd629ac7..896b53bb1 100644 --- a/partner_communication/i18n/fr_CH.po +++ b/partner_communication/i18n/fr_CH.po @@ -1729,3 +1729,10 @@ msgstr "aperçu" #: model:mail.template,subject:partner_communication.default_communication_template msgid "{{object.subject or ' '}}" msgstr "\t{{object.subject or ' '}}" + +#. module: partner_communication +#. odoo-python +#: code:addons/partner_communication/wizards/email_template_preview_view.py:0 +msgid "" +"No communication uses this template yet. Choose one in Record to preview it." +msgstr "Aucune communication n'utilise encore ce modèle. Choisissez-en une dans Enregistrement pour le prévisualiser." diff --git a/partner_communication/i18n/it_IT.po b/partner_communication/i18n/it_IT.po index a717ab3c9..c67966e53 100644 --- a/partner_communication/i18n/it_IT.po +++ b/partner_communication/i18n/it_IT.po @@ -1725,3 +1725,10 @@ msgstr "anteprima" #: model:mail.template,subject:partner_communication.default_communication_template msgid "{{object.subject or ' '}}" msgstr "{{oggetto.soggetto o ' '}}" + +#. module: partner_communication +#. odoo-python +#: code:addons/partner_communication/wizards/email_template_preview_view.py:0 +msgid "" +"No communication uses this template yet. Choose one in Record to preview it." +msgstr "Nessuna comunicazione utilizza ancora questo modello. Sceglierne una in Record per visualizzarne l'anteprima." diff --git a/partner_communication/tests/__init__.py b/partner_communication/tests/__init__.py index 843ef3cc8..d066126bd 100644 --- a/partner_communication/tests/__init__.py +++ b/partner_communication/tests/__init__.py @@ -6,3 +6,5 @@ # The licence is in the file __manifest__.py # ############################################################################## + +from . import test_template_preview diff --git a/partner_communication/tests/test_template_preview.py b/partner_communication/tests/test_template_preview.py new file mode 100644 index 000000000..e8c1ef9e5 --- /dev/null +++ b/partner_communication/tests/test_template_preview.py @@ -0,0 +1,49 @@ +from odoo.tests.common import TransactionCase + + +class TestTemplatePreview(TransactionCase): + """The preview of a communication template renders a communication of a + rule using it (T3523).""" + + @classmethod + def setUpClass(cls): + super().setUpClass() + job_model = cls.env["ir.model"]._get("partner.communication.job") + cls.template = cls.env["mail.template"].create( + { + "name": "T3523 preview", + "model_id": job_model.id, + "body_html": "

Hello

", + } + ) + cls.config = cls.env.ref("partner_communication.default_communication").copy( + {"name": "T3523 preview rule", "email_template_id": cls.template.id} + ) + cls.partner = cls.env["res.partner"].create({"name": "T3523 preview"}) + + def _preview(self): + return self.env["mail.template.preview"].create( + {"mail_template_id": self.template.id} + ) + + def test_preview_uses_a_communication_of_the_template(self): + job = self.env["partner.communication.job"].create( + {"partner_id": self.partner.id, "config_id": self.config.id} + ) + # A newer communication of another rule + self.env["partner.communication.job"].create( + { + "partner_id": self.partner.id, + "config_id": self.env.ref( + "partner_communication.default_communication" + ).id, + } + ) + preview = self._preview() + self.assertEqual(preview.resource_ref, job) + self.assertFalse(preview.error_msg) + + def test_preview_without_communication_says_so(self): + preview = self._preview() + self.assertFalse(preview.resource_ref) + self.assertTrue(preview.error_msg) diff --git a/partner_communication/wizards/email_template_preview_view.py b/partner_communication/wizards/email_template_preview_view.py index e5c173954..998252e1a 100644 --- a/partner_communication/wizards/email_template_preview_view.py +++ b/partner_communication/wizards/email_template_preview_view.py @@ -8,37 +8,37 @@ # ############################################################################## -from odoo import api, fields, models +from odoo import _, api, models + +JOB_MODEL = "partner.communication.job" class TemplatePreview(models.TransientModel): _inherit = "mail.template.preview" - res_id = fields.Reference( - selection=lambda s: s._reference_models(), default=lambda s: s._default_model() - ) - - @api.model - def _reference_models(self): - template = self.env["mail.template"].browse(self._context.get("active_id")) - domain = [("model", "=", template.model)] - models = self.env["ir.model"].search(domain) - result = [(model.model, model.name) for model in models] - return result + def _is_communication_template(self): + return self.mail_template_id.sudo().model == JOB_MODEL - @api.model - def _default_model(self): - recs = self._reference_models() - model = recs[0][0] - model_obj = self.env[model] - has_partner_field = hasattr(model_obj, "partner_id") - if has_partner_field: - domain = [("partner_id", "!=", False)] - else: - domain = [] - result = model + "," + str(model_obj.search(domain, limit=1).id) - return result + @api.depends("mail_template_id") + def _compute_resource_ref(self): + """A communication template reads the objects of its own rule (e.g. a + registration): preview it on a communication of a rule using it, not + on the latest communication of any rule.""" + res = super()._compute_resource_ref() + for preview in self.filtered(lambda p: p._is_communication_template()): + job = self.env[JOB_MODEL].search( + [("config_id.email_template_id", "=", preview.mail_template_id.id)], + limit=1, + ) + preview.resource_ref = f"{JOB_MODEL},{job.id}" if job else False + return res - @api.onchange("res_id") - def on_change_res_id(self): - return super().on_change_res_id() + @api.depends("lang", "resource_ref") + def _compute_mail_template_fields(self): + res = super()._compute_mail_template_fields() + if self._is_communication_template() and not self.resource_ref: + self.error_msg = _( + "No communication uses this template yet. Choose one in Record " + "to preview it." + ) + return res