From 153dabf9cb1f90cb085a101144bc3b8e8645109b Mon Sep 17 00:00:00 2001 From: Mihai Fekete Date: Mon, 10 Jan 2022 16:49:29 +0200 Subject: [PATCH] [14.0] [FIX] Fix base comment template rendering. [UPD] Update base_comment_template.pot [UPD] README.rst base_comment_template 14.0.3.0.0 Update translation files Updated by "Update PO files to match POT (msgmerge)" hook in Weblate. Translation: reporting-engine-14.0/reporting-engine-14.0-base_comment_template Translate-URL: https://translation.odoo-community.org/projects/reporting-engine-14-0/reporting-engine-14-0-base_comment_template/ --- base_comment_template/README.rst | 27 +++- base_comment_template/__init__.py | 2 +- base_comment_template/__manifest__.py | 3 +- base_comment_template/i18n/am.po | 117 +++++++++++++++++ base_comment_template/i18n/ar.po | 117 +++++++++++++++++ .../i18n/base_comment_template.pot | 116 +++++++++++++++++ base_comment_template/i18n/bg.po | 117 +++++++++++++++++ base_comment_template/i18n/bs.po | 117 +++++++++++++++++ base_comment_template/i18n/ca.po | 117 +++++++++++++++++ base_comment_template/i18n/cs.po | 117 +++++++++++++++++ base_comment_template/i18n/da.po | 117 +++++++++++++++++ base_comment_template/i18n/de.po | 117 +++++++++++++++++ base_comment_template/i18n/el_GR.po | 117 +++++++++++++++++ base_comment_template/i18n/en_GB.po | 117 +++++++++++++++++ base_comment_template/i18n/es.po | 117 +++++++++++++++++ base_comment_template/i18n/es_AR.po | 117 +++++++++++++++++ base_comment_template/i18n/es_CL.po | 117 +++++++++++++++++ base_comment_template/i18n/es_CO.po | 117 +++++++++++++++++ base_comment_template/i18n/es_CR.po | 117 +++++++++++++++++ base_comment_template/i18n/es_DO.po | 117 +++++++++++++++++ base_comment_template/i18n/es_EC.po | 117 +++++++++++++++++ base_comment_template/i18n/es_ES.po | 117 +++++++++++++++++ base_comment_template/i18n/es_MX.po | 117 +++++++++++++++++ base_comment_template/i18n/es_PE.po | 117 +++++++++++++++++ base_comment_template/i18n/es_PY.po | 117 +++++++++++++++++ base_comment_template/i18n/es_VE.po | 117 +++++++++++++++++ base_comment_template/i18n/et.po | 117 +++++++++++++++++ base_comment_template/i18n/eu.po | 117 +++++++++++++++++ base_comment_template/i18n/fa.po | 117 +++++++++++++++++ base_comment_template/i18n/fi.po | 117 +++++++++++++++++ base_comment_template/i18n/fr.po | 117 +++++++++++++++++ base_comment_template/i18n/fr_CA.po | 117 +++++++++++++++++ base_comment_template/i18n/fr_CH.po | 117 +++++++++++++++++ base_comment_template/i18n/gl.po | 117 +++++++++++++++++ base_comment_template/i18n/gl_ES.po | 117 +++++++++++++++++ base_comment_template/i18n/he.po | 117 +++++++++++++++++ base_comment_template/i18n/hr.po | 117 +++++++++++++++++ base_comment_template/i18n/hr_HR.po | 117 +++++++++++++++++ base_comment_template/i18n/hu.po | 117 +++++++++++++++++ base_comment_template/i18n/id.po | 117 +++++++++++++++++ base_comment_template/i18n/it.po | 120 +++++++++++++++++- base_comment_template/i18n/ja.po | 117 +++++++++++++++++ base_comment_template/i18n/ko.po | 117 +++++++++++++++++ base_comment_template/i18n/lt.po | 117 +++++++++++++++++ base_comment_template/i18n/lt_LT.po | 117 +++++++++++++++++ base_comment_template/i18n/lv.po | 117 +++++++++++++++++ base_comment_template/i18n/mk.po | 117 +++++++++++++++++ base_comment_template/i18n/mn.po | 117 +++++++++++++++++ base_comment_template/i18n/nb.po | 117 +++++++++++++++++ base_comment_template/i18n/nb_NO.po | 117 +++++++++++++++++ base_comment_template/i18n/nl.po | 117 +++++++++++++++++ base_comment_template/i18n/nl_BE.po | 117 +++++++++++++++++ base_comment_template/i18n/nl_NL.po | 117 +++++++++++++++++ base_comment_template/i18n/pl.po | 117 +++++++++++++++++ base_comment_template/i18n/pt.po | 117 +++++++++++++++++ base_comment_template/i18n/pt_BR.po | 117 +++++++++++++++++ base_comment_template/i18n/pt_PT.po | 117 +++++++++++++++++ base_comment_template/i18n/ro.po | 117 +++++++++++++++++ base_comment_template/i18n/ru.po | 117 +++++++++++++++++ base_comment_template/i18n/sk.po | 117 +++++++++++++++++ base_comment_template/i18n/sl.po | 117 +++++++++++++++++ base_comment_template/i18n/sr.po | 117 +++++++++++++++++ base_comment_template/i18n/sr@latin.po | 117 +++++++++++++++++ base_comment_template/i18n/sv.po | 117 +++++++++++++++++ base_comment_template/i18n/th.po | 117 +++++++++++++++++ base_comment_template/i18n/tr.po | 117 +++++++++++++++++ base_comment_template/i18n/tr_TR.po | 117 +++++++++++++++++ base_comment_template/i18n/uk.po | 117 +++++++++++++++++ base_comment_template/i18n/vi.po | 117 +++++++++++++++++ base_comment_template/i18n/vi_VN.po | 117 +++++++++++++++++ base_comment_template/i18n/zh_CN.po | 117 +++++++++++++++++ base_comment_template/i18n/zh_TW.po | 117 +++++++++++++++++ base_comment_template/models/__init__.py | 5 +- .../models/comment_template.py | 14 ++ base_comment_template/readme/USAGE.rst | 27 +++- .../security/ir.model.access.csv | 1 + .../static/description/index.html | 31 +++-- .../tests/test_base_comment_template.py | 60 +++++++++ .../views/base_comment_template_view.xml | 8 ++ base_comment_template/wizard/__init__.py | 1 + .../wizard/base_comment_template_preview.py | 83 ++++++++++++ .../base_comment_template_preview_views.xml | 80 ++++++++++++ 82 files changed, 8388 insertions(+), 29 deletions(-) create mode 100644 base_comment_template/wizard/__init__.py create mode 100644 base_comment_template/wizard/base_comment_template_preview.py create mode 100644 base_comment_template/wizard/base_comment_template_preview_views.xml diff --git a/base_comment_template/README.rst b/base_comment_template/README.rst index 50a03023a..dfc2eedc1 100644 --- a/base_comment_template/README.rst +++ b/base_comment_template/README.rst @@ -87,20 +87,33 @@ The template is a html field which will be rendered just like a mail template, s Change the report related to the model from configuration and add a statement like: -

+ +

- + -

-

+ +

- - -

+ You should always use t-if since the method returns False if no template is found. +If you want to use Qweb templates, or different context, you can specify it just like in +mail.render.mixin with parameters: + +- engine: "jinja" or "qweb", +- add_context: dict with your own context, +- post_process: perform a post processing on rendered result + +so you could use it : + + +
+ + + Bug Tracker =========== diff --git a/base_comment_template/__init__.py b/base_comment_template/__init__.py index 83e553ac4..ba0a966ff 100644 --- a/base_comment_template/__init__.py +++ b/base_comment_template/__init__.py @@ -1,3 +1,3 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from . import models +from . import models, wizard diff --git a/base_comment_template/__manifest__.py b/base_comment_template/__manifest__.py index 495f53901..342b545df 100644 --- a/base_comment_template/__manifest__.py +++ b/base_comment_template/__manifest__.py @@ -5,7 +5,7 @@ "name": "Base Comments Templates", "summary": "Add conditional mako template to any report" "on models that inherits comment.template.", - "version": "14.0.2.0.1", + "version": "14.0.3.0.0", "category": "Reporting", "website": "https://github.com/OCA/reporting-engine", "author": "Camptocamp, Odoo Community Association (OCA)", @@ -15,6 +15,7 @@ "data": [ "security/ir.model.access.csv", "security/security.xml", + "wizard/base_comment_template_preview_views.xml", "views/base_comment_template_view.xml", "views/res_partner_view.xml", ], diff --git a/base_comment_template/i18n/am.po b/base_comment_template/i18n/am.po index a80a48785..9b9ea00aa 100644 --- a/base_comment_template/i18n/am.po +++ b/base_comment_template/i18n/am.po @@ -18,6 +18,14 @@ msgstr "" "Content-Transfer-Encoding: \n" "Plural-Forms: nplurals=2; plural=(n > 1);\n" +#. module: base_comment_template +#: model_terms:ir.ui.view,arch_db:base_comment_template.base_comment_template_preview_form +msgid "" +" + +
- + -

-

+ +

- - -

+ You should always use t-if since the method returns False if no template is found. + +If you want to use Qweb templates, or different context, you can specify it just like in +mail.render.mixin with parameters: + +- engine: "jinja" or "qweb", +- add_context: dict with your own context, +- post_process: perform a post processing on rendered result + +so you could use it : + + +
+ + diff --git a/base_comment_template/security/ir.model.access.csv b/base_comment_template/security/ir.model.access.csv index d2db39e34..af72e1c8f 100644 --- a/base_comment_template/security/ir.model.access.csv +++ b/base_comment_template/security/ir.model.access.csv @@ -1,3 +1,4 @@ id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink access_base_comment_template_user,access_base_comment_template_user,model_base_comment_template,,1,0,0,0 access_base_comment_template,access_base_comment_template no one,model_base_comment_template,base.group_no_one,1,1,1,1 +access_base_comment_template_preview,access.base.comment.template.preview,model_base_comment_template_preview,base.group_user,1,1,1,0 diff --git a/base_comment_template/static/description/index.html b/base_comment_template/static/description/index.html index 9bb4d0780..cd4bfc9b4 100644 --- a/base_comment_template/static/description/index.html +++ b/base_comment_template/static/description/index.html @@ -433,15 +433,30 @@ It has a priority field (smaller number = higher priority)

If you create a new template with the same configuration (Model, Domain, Position) and set it as Default, the previous one will be deselected as a default one.

The template is a html field which will be rendered just like a mail template, so you can use variables like ${object}, ${user}, ${ctx} to add dynamic content.

Change the report related to the model from configuration and add a statement like:

-

<p t-if=”o.get_comment_template(‘before_lines’, o.company_id.id, o.partner_id and o.partner_id.id or False)”>

-
-<span t-raw=”o.get_comment_template(‘before_lines’, o.company_id.id, o.partner_id and o.partner_id.id or False)”/>
-

</p>

-

<p t-if=”o.get_comment_template(‘after_lines’, o.company_id.id, o.partner_id and o.partner_id.id or False)”>

-
-<span t-raw=”o.get_comment_template(‘after_lines’, o.company_id.id, o.partner_id and o.partner_id.id or False)”/>
-

</p>

+
+
<t t-foreach=”o.comment_template_ids.filtered(lambda x: x.position == ‘before_lines’)” t-as=”comment_template_top”>
+
<div t-raw=”o.render_comment(comment_template_top)” />
+
+

</t>

+
+
<t t-foreach=”o.comment_template_ids.filtered(lambda x: x.position == ‘after_lines’)” t-as=”comment_template_bottom”>
+
<div t-raw=”o.render_comment(comment_template_bottom)” />
+
+

</t>

You should always use t-if since the method returns False if no template is found.

+

If you want to use Qweb templates, or different context, you can specify it just like in +mail.render.mixin with parameters:

+
    +
  • engine: “jinja” or “qweb”,
  • +
  • add_context: dict with your own context,
  • +
  • post_process: perform a post processing on rendered result
  • +
+

so you could use it :

+
+
<t t-foreach=”o.comment_template_ids.filtered(lambda x: x.position == ‘before_lines’)” t-as=”comment_template_top”>
+
<div t-raw=”o.render_comment(comment_template_top, engine=’qweb’, add_context={my dict}, postprocess=True)” />
+
+

</t>

Bug Tracker

diff --git a/base_comment_template/tests/test_base_comment_template.py b/base_comment_template/tests/test_base_comment_template.py index 30ae71a52..58ff8209b 100644 --- a/base_comment_template/tests/test_base_comment_template.py +++ b/base_comment_template/tests/test_base_comment_template.py @@ -2,6 +2,7 @@ # Copyright 2021 Tecnativa - Víctor Martínez # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from odoo.tests import common +from odoo.tools.misc import mute_logger from .fake_models import ResUsers, setup_test_model, teardown_test_model @@ -16,6 +17,7 @@ class TestCommentTemplate(common.SavepointCase): cls.user2 = cls.env.ref("base.demo_user0") cls.partner_id = cls.env.ref("base.res_partner_12") cls.partner2_id = cls.env.ref("base.res_partner_10") + cls.ResPartnerTitle = cls.env["res.partner.title"] cls.main_company = cls.env.ref("base.main_company") cls.company = cls.env["res.company"].create({"name": "Test company"}) cls.before_template_id = cls.env["base.comment.template"].create( @@ -87,3 +89,61 @@ class TestCommentTemplate(common.SavepointCase): self.assertTrue( self.before_template_id not in self.partner_id.base_comment_template_ids ) + + def test_render_comment_text(self): + expected_text = "Test comment render %s" % self.user.name + self.before_template_id.text = "Test comment render ${object.name}" + with self.with_user(self.user.login): + self.assertEqual( + self.user.render_comment(self.before_template_id), expected_text + ) + + def test_render_comment_text_(self): + with mute_logger("odoo.addons.base.models.ir_translation"): + self.env["base.language.install"].create( + {"lang": "ro_RO", "overwrite": True} + ).lang_install() + with mute_logger("odoo.tools.translate"): + self.env["base.update.translations"].create({"lang": "ro_RO"}).act_update() + partner_title = self.ResPartnerTitle.create( + {"name": "Ambassador", "shortcut": "Amb."} + ) + # Adding translated terms + ctx = dict(lang="ro_RO") + partner_title.with_context(ctx).write({"name": "Ambasador", "shortcut": "Amb."}) + self.user.partner_id.title = partner_title + self.before_template_id.text = "Test comment render ${object.title.name}" + + expected_en_text = "Test comment render Ambassador" + expected_ro_text = "Test comment render Ambasador" + with self.with_user(self.user.login): + self.assertEqual( + self.user.render_comment(self.before_template_id), expected_en_text + ) + self.assertEqual( + self.user.with_context(ctx).render_comment(self.before_template_id), + expected_ro_text, + ) + + def test_partner_template_wizaard(self): + partner_preview = ( + self.env["base.comment.template.preview"] + .with_context(default_base_comment_template_id=self.before_template_id.id) + .create({}) + ) + self.assertTrue(partner_preview) + default = ( + self.env["base.comment.template.preview"] + .with_context(default_base_comment_template_id=self.before_template_id.id) + .default_get(partner_preview._fields) + ) + self.assertTrue(default.get("base_comment_template_id")) + resource_ref = partner_preview._selection_target_model() + self.assertTrue(len(resource_ref) >= 2) + partner_preview._compute_no_record() + self.assertTrue(partner_preview.no_record) + + def test_partner_commercial_fields(self): + self.assertTrue( + "base_comment_template_ids" in self.env["res.partner"]._commercial_fields() + ) diff --git a/base_comment_template/views/base_comment_template_view.xml b/base_comment_template/views/base_comment_template_view.xml index 79b4979a5..bcacdeb44 100644 --- a/base_comment_template/views/base_comment_template_view.xml +++ b/base_comment_template/views/base_comment_template_view.xml @@ -44,6 +44,14 @@ bg_color="bg-danger" attrs="{'invisible': [('active', '=', True)]}" /> +

diff --git a/base_comment_template/wizard/__init__.py b/base_comment_template/wizard/__init__.py new file mode 100644 index 000000000..9a0d64bbf --- /dev/null +++ b/base_comment_template/wizard/__init__.py @@ -0,0 +1 @@ +from . import base_comment_template_preview diff --git a/base_comment_template/wizard/base_comment_template_preview.py b/base_comment_template/wizard/base_comment_template_preview.py new file mode 100644 index 000000000..23dbe4eaf --- /dev/null +++ b/base_comment_template/wizard/base_comment_template_preview.py @@ -0,0 +1,83 @@ +from odoo import api, fields, models +from odoo.tools.safe_eval import safe_eval + + +class BaseCommentTemplatePreview(models.TransientModel): + _name = "base.comment.template.preview" + _description = "Base Comment Template Preview" + + @api.model + def _selection_target_model(self): + models = self.env["ir.model"].search([("is_comment_template", "=", True)]) + return [(model.model, model.name) for model in models] + + @api.model + def _selection_languages(self): + return self.env["res.lang"].get_installed() + + @api.model + def default_get(self, fields): + result = super(BaseCommentTemplatePreview, self).default_get(fields) + base_comment_template_id = self.env.context.get( + "default_base_comment_template_id" + ) + if not base_comment_template_id or "resource_ref" not in fields: + return result + base_comment_template = self.env["base.comment.template"].browse( + base_comment_template_id + ) + result["model_ids"] = base_comment_template.model_ids + domain = safe_eval(base_comment_template.domain) + model = ( + base_comment_template.model_ids[0] + if base_comment_template.model_ids + else False + ) + res = self.env[model.model].search(domain, limit=1) + if res: + result["resource_ref"] = "%s,%s" % (model.model, res.id) + return result + + base_comment_template_id = fields.Many2one( + "base.comment.template", required=True, ondelete="cascade" + ) + lang = fields.Selection(_selection_languages, string="Template Preview Language") + engine = fields.Selection( + [("jinja", "Jinja"), ("qweb", "QWeb")], + string="Template Preview Engine", + defult="jinja", + ) + model_ids = fields.Many2many( + "ir.model", related="base_comment_template_id.model_ids" + ) + model_id = fields.Many2one("ir.model") + body = fields.Char("Body", compute="_compute_base_comment_template_fields") + resource_ref = fields.Reference( + string="Record reference", selection="_selection_target_model" + ) + no_record = fields.Boolean("No Record", compute="_compute_no_record") + + @api.depends("model_id") + def _compute_no_record(self): + for preview in self: + domain = safe_eval(self.base_comment_template_id.domain) + preview.no_record = ( + (self.env[preview.model_id.model].search_count(domain) == 0) + if preview.model_id + else True + ) + + @api.depends("lang", "resource_ref", "engine") + def _compute_base_comment_template_fields(self): + for wizard in self: + if ( + wizard.model_id + and wizard.resource_ref + and wizard.lang + and wizard.engine + ): + wizard.body = wizard.resource_ref.with_context( + lang=wizard.lang + ).render_comment(self.base_comment_template_id, engine=wizard.engine) + else: + wizard.body = wizard.base_comment_template_id.text diff --git a/base_comment_template/wizard/base_comment_template_preview_views.xml b/base_comment_template/wizard/base_comment_template_preview_views.xml new file mode 100644 index 000000000..513bab63d --- /dev/null +++ b/base_comment_template/wizard/base_comment_template_preview_views.xml @@ -0,0 +1,80 @@ + + + + + + base.comment.template.preview.form + base.comment.template.preview + +
+

Preview of + +

+ + +
+ Choose an example + + record: + +
+ + No records + +
+
+

Choose a language: + +

+

Choose an engine: + +

+
+ + + Template Preview + base.comment.template.preview + ir.actions.act_window + form + + new + {'default_base_comment_template_id':active_id} + + + +
+