diff --git a/base_comment_template/README.rst b/base_comment_template/README.rst new file mode 100644 index 0000000000..9caf8adc98 --- /dev/null +++ b/base_comment_template/README.rst @@ -0,0 +1,204 @@ +======================= +Base Comments Templates +======================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:72251979ce578742037aa71ca933ba9c4ed9951b7e3f68951ca232766f72d000 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Freporting--engine-lightgray.png?logo=github + :target: https://github.com/OCA/reporting-engine/tree/17.0/base_comment_template + :alt: OCA/reporting-engine +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/reporting-engine-17-0/reporting-engine-17-0-base_comment_template + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/reporting-engine&target_branch=17.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Add a new mixin class to define templates of comments to print on +documents. The comment templates can be defined like make templates, so +you can use variables from linked models. + +Two positions are available for the comments: + +- above document lines (before_lines) +- below document lines (after_lines) + +The template are general, and can be attached to any Model and based on +some domain defined in the template. You can define one default template +per Model and domain, which can be overwritten for any company and +partners. It has a priority field (smaller number = higher priority) + +In existing reports, if you add this line will get the comment template +if you created one like + +- ( or without any parameter) + +This module is the base module for following modules: + +- sale_comment_template +- purchase_comment_template +- account_comment_template +- stock_picking_comment_template + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +Go to *Settings > Technical > Reporting > Comment Templates* and start +designing you comment templates. + +This module is the base module for following modules: + +- sale_comment_template +- purchase_comment_template +- invoice_comment_template +- stock_picking_comment_template + +Usage +===== + +1. Go to *Settings* and activate the developer mode. +2. Go to *Settings > Technical > Reporting > Comment Templates*. +3. Create a new record. +4. Define the Company the template is linked or leave default for all + companies. +5. Define the Partner the template is linked or leave default for all + partners. +6. Define the Model, Domain the template is linked. +7. Define the Position where the template will be printed: + + - above document lines + - below document lines + +You should have at least one template with Default field set, if you +choose a Partner the template is deselected as a Default one. 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: + +| +|
+ + + +| +|
+ + + +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: "inline_template", "qweb" or "qweb_view", +- add_context: dict with your own context, +- post_process: perform a post processing on rendered result + +so you could use it : + +| +|
+ + + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Camptocamp + +Contributors +------------ + +- Xavier Jimenez +- Nicolas Bessi +- Yannick Vaucher +- Guewen Baconnier +- Simone Rubino +- `DynApps `__: + + - Raf Ven + +- `Druidoo `__: + + - Iván Todorovich + +- Pierre Verkest +- `NextERP Romania `__: + + - Fekete Mihai + +- `Tecnativa `__: + + - Carlos Roca + - Víctor Martínez + +- `Jarsa `__: + + - Alan Ramos + +- `Bloopark systems `__: + + - Achraf Mhadhbi + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/reporting-engine `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/base_comment_template/__init__.py b/base_comment_template/__init__.py new file mode 100644 index 0000000000..f987065d30 --- /dev/null +++ b/base_comment_template/__init__.py @@ -0,0 +1,4 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import models +from . import wizard diff --git a/base_comment_template/__manifest__.py b/base_comment_template/__manifest__.py new file mode 100644 index 0000000000..88b705f921 --- /dev/null +++ b/base_comment_template/__manifest__.py @@ -0,0 +1,22 @@ +# © 2013-2014 Nicolas Bessi (Camptocamp SA) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +{ + "name": "Base Comments Templates", + "summary": "Add conditional mako template to any report" + "on models that inherits comment.template.", + "version": "17.0.1.0.0", + "category": "Reporting", + "website": "https://github.com/OCA/reporting-engine", + "author": "Camptocamp, Odoo Community Association (OCA)", + "license": "AGPL-3", + "installable": True, + "depends": ["base", "mail"], + "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 new file mode 100644 index 0000000000..6744183695 --- /dev/null +++ b/base_comment_template/i18n/am.po @@ -0,0 +1,369 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * base_comment_template +# +# Translators: +# OCA Transbot , 2017 +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 9.0c\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2017-05-16 11:59+0000\n" +"PO-Revision-Date: 2017-05-16 11:59+0000\n" +"Last-Translator: OCA Transbot , 2017\n" +"Language-Team: Amharic (https://www.transifex.com/oca/teams/23907/am/)\n" +"Language: am\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"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 "" +", 2017 +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 9.0c\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2017-05-16 11:59+0000\n" +"PO-Revision-Date: 2019-07-05 04:42+0000\n" +"Last-Translator: Yoshi Tashiro \n" +"Language-Team: Japanese (https://www.transifex.com/oca/teams/23907/ja/)\n" +"Language: ja\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=1; plural=0;\n" +"X-Generator: Weblate 3.7.1\n" + +#. module: base_comment_template +#: model_terms:ir.ui.view,arch_db:base_comment_template.base_comment_template_preview_form +msgid "" +" Technical \> Reporting \> Comment Templates* and +start designing you comment templates. + +This module is the base module for following modules: + +- sale_comment_template +- purchase_comment_template +- invoice_comment_template +- stock_picking_comment_template diff --git a/base_comment_template/readme/CONTRIBUTORS.md b/base_comment_template/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..d132fae14b --- /dev/null +++ b/base_comment_template/readme/CONTRIBUTORS.md @@ -0,0 +1,19 @@ +- Xavier Jimenez \<\> +- Nicolas Bessi \<\> +- Yannick Vaucher \<\> +- Guewen Baconnier \<\> +- Simone Rubino \<\> +- [DynApps](https://www.dynapps.be): + - Raf Ven \<\> +- [Druidoo](https://www.druidoo.io): + - Iván Todorovich \<\> +- Pierre Verkest \<\> +- [NextERP Romania](https://www.nexterp.ro): + - Fekete Mihai \<\> +- [Tecnativa](https://www.tecnativa.com): + - Carlos Roca + - Víctor Martínez +- [Jarsa](https://www.jarsa.com): + - Alan Ramos \<\> +- [Bloopark systems](https://www.bloopark.de/): + - Achraf Mhadhbi \<\> diff --git a/base_comment_template/readme/DESCRIPTION.md b/base_comment_template/readme/DESCRIPTION.md new file mode 100644 index 0000000000..9a9d073ddf --- /dev/null +++ b/base_comment_template/readme/DESCRIPTION.md @@ -0,0 +1,27 @@ +Add a new mixin class to define templates of comments to print on +documents. The comment templates can be defined like make templates, so +you can use variables from linked models. + +Two positions are available for the comments: + +- above document lines (before_lines) +- below document lines (after_lines) + +The template are general, and can be attached to any Model and based on +some domain defined in the template. You can define one default template +per Model and domain, which can be overwritten for any company and +partners. It has a priority field (smaller number = higher priority) + +In existing reports, if you add this line will get the comment template +if you created one like + +- \ ( or without any parameter) + +This module is the base module for following modules: + +- sale_comment_template +- purchase_comment_template +- account_comment_template +- stock_picking_comment_template diff --git a/base_comment_template/readme/USAGE.md b/base_comment_template/readme/USAGE.md new file mode 100644 index 0000000000..8792b757a8 --- /dev/null +++ b/base_comment_template/readme/USAGE.md @@ -0,0 +1,52 @@ +1. Go to *Settings* and activate the developer mode. +2. Go to *Settings \> Technical \> Reporting \> Comment Templates*. +3. Create a new record. +4. Define the Company the template is linked or leave default for all + companies. +5. Define the Partner the template is linked or leave default for all + partners. +6. Define the Model, Domain the template is linked. +7. Define the Position where the template will be printed: + - above document lines + - below document lines + +You should have at least one template with Default field set, if you +choose a Partner the template is deselected as a Default one. 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: + +\ +\
+ +\ + +\ +\
+ +\ + +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: "inline_template", "qweb" or "qweb_view", +- 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 new file mode 100644 index 0000000000..acfeb4685f --- /dev/null +++ b/base_comment_template/security/ir.model.access.csv @@ -0,0 +1,3 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +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/security/security.xml b/base_comment_template/security/security.xml new file mode 100644 index 0000000000..440091427b --- /dev/null +++ b/base_comment_template/security/security.xml @@ -0,0 +1,12 @@ + + + + + Base comment multi-company + + + + ['|',('company_id','=',False),('company_id','in',company_ids)] + + + diff --git a/base_comment_template/static/description/icon.png b/base_comment_template/static/description/icon.png new file mode 100644 index 0000000000..3a0328b516 Binary files /dev/null and b/base_comment_template/static/description/icon.png differ diff --git a/base_comment_template/static/description/index.html b/base_comment_template/static/description/index.html new file mode 100644 index 0000000000..487b5e8f01 --- /dev/null +++ b/base_comment_template/static/description/index.html @@ -0,0 +1,549 @@ + + + + + +Base Comments Templates + + + +
+

Base Comments Templates

+ + +

Beta License: AGPL-3 OCA/reporting-engine Translate me on Weblate Try me on Runboat

+

Add a new mixin class to define templates of comments to print on +documents. The comment templates can be defined like make templates, so +you can use variables from linked models.

+

Two positions are available for the comments:

+
    +
  • above document lines (before_lines)
  • +
  • below document lines (after_lines)
  • +
+

The template are general, and can be attached to any Model and based on +some domain defined in the template. You can define one default template +per Model and domain, which can be overwritten for any company and +partners. It has a priority field (smaller number = higher priority)

+

In existing reports, if you add this line will get the comment template +if you created one like

+
    +
  • <span +t-out=”o.get_comment_template(‘position’,company_id=o.company_id, +partner_id=o.parnter_id )”/> ( or without any parameter)
  • +
+

This module is the base module for following modules:

+
    +
  • sale_comment_template
  • +
  • purchase_comment_template
  • +
  • account_comment_template
  • +
  • stock_picking_comment_template
  • +
+

Table of contents

+ +
+

Configuration

+

Go to Settings > Technical > Reporting > Comment Templates and start +designing you comment templates.

+

This module is the base module for following modules:

+
    +
  • sale_comment_template
  • +
  • purchase_comment_template
  • +
  • invoice_comment_template
  • +
  • stock_picking_comment_template
  • +
+
+
+

Usage

+
    +
  1. Go to Settings and activate the developer mode.
  2. +
  3. Go to Settings > Technical > Reporting > Comment Templates.
  4. +
  5. Create a new record.
  6. +
  7. Define the Company the template is linked or leave default for all +companies.
  8. +
  9. Define the Partner the template is linked or leave default for all +partners.
  10. +
  11. Define the Model, Domain the template is linked.
  12. +
  13. Define the Position where the template will be printed:
      +
    • above document lines
    • +
    • below document lines
    • +
    +
  14. +
+

You should have at least one template with Default field set, if you +choose a Partner the template is deselected as a Default one. 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:

+
+
<t t-foreach=”o.comment_template_ids.filtered(lambda x: x.position == +‘before_lines’)” t-as=”comment_template_top”>
+
<div t-out=”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-out=”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: “inline_template”, “qweb” or “qweb_view”,
  • +
  • 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-out=”o.render_comment(comment_template_top, engine=’qweb’, +add_context={my dict}, postprocess=True)” />
+
+

</t>

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Camptocamp
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/reporting-engine project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/base_comment_template/tests/__init__.py b/base_comment_template/tests/__init__.py new file mode 100644 index 0000000000..e198115e4e --- /dev/null +++ b/base_comment_template/tests/__init__.py @@ -0,0 +1,2 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from . import test_base_comment_template diff --git a/base_comment_template/tests/fake_models.py b/base_comment_template/tests/fake_models.py new file mode 100644 index 0000000000..02ee0ca234 --- /dev/null +++ b/base_comment_template/tests/fake_models.py @@ -0,0 +1,34 @@ +# Copyright 2017 LasLabs Inc. +# Copyright 2018 ACSONE +# Copyright 2018 Camptocamp +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). + +from odoo import models + + +def setup_test_model(env, model_cls): + """Pass a test model class and initialize it. + + Courtesy of SBidoul from https://github.com/OCA/mis-builder :) + """ + model_cls._build_model(env.registry, env.cr) + env.registry.setup_models(env.cr) + env.registry.init_models( + env.cr, [model_cls._name], dict(env.context, update_custom_fields=True) + ) + + +def teardown_test_model(env, model_cls): + """Pass a test model class and deinitialize it. + + Courtesy of SBidoul from https://github.com/OCA/mis-builder :) + """ + if not getattr(model_cls, "_teardown_no_delete", False): + del env.registry.models[model_cls._name] + env.registry.setup_models(env.cr) + + +class ResUsers(models.Model): + _name = "res.users" + _inherit = ["res.users", "comment.template"] + _teardown_no_delete = True diff --git a/base_comment_template/tests/test_base_comment_template.py b/base_comment_template/tests/test_base_comment_template.py new file mode 100644 index 0000000000..d13a521685 --- /dev/null +++ b/base_comment_template/tests/test_base_comment_template.py @@ -0,0 +1,186 @@ +# Copyright 2020 NextERP Romania SRL +# Copyright 2021 Tecnativa - Víctor Martínez +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from odoo import Command +from odoo.exceptions import ValidationError +from odoo.tests import common +from odoo.tools.misc import mute_logger + +from .fake_models import ResUsers, setup_test_model, teardown_test_model + + +class TestCommentTemplate(common.TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + setup_test_model(cls.env, ResUsers) + cls.user_obj = cls.env.ref("base.model_res_users") + cls.user = cls.env.ref("base.user_demo") + 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( + { + "name": "Top template", + "text": "Text before lines", + "models": cls.user_obj.model, + "company_id": cls.company.id, + } + ) + cls.after_template_id = cls.env["base.comment.template"].create( + { + "name": "Bottom template", + "position": "after_lines", + "text": "Text after lines", + "models": cls.user_obj.model, + "company_id": cls.company.id, + } + ) + cls.user.partner_id.base_comment_template_ids = [ + (4, cls.before_template_id.id), + (4, cls.after_template_id.id), + ] + + @classmethod + def tearDownClass(cls): + teardown_test_model(cls.env, ResUsers) + return super().tearDownClass() + + def test_template_model_ids(self): + self.assertIn( + self.user_obj.model, self.before_template_id.mapped("model_ids.model") + ) + self.assertEqual(len(self.before_template_id.model_ids), 1) + self.assertIn( + self.user_obj.model, self.after_template_id.mapped("model_ids.model") + ) + self.assertEqual(len(self.after_template_id.model_ids), 1) + + def test_template_models_constrains(self): + with self.assertRaises(ValidationError): + self.env["base.comment.template"].create( + { + "name": "Custom template", + "text": "Text", + "models": "incorrect.model", + "company_id": self.company.id, + } + ) + + def test_template_display_name(self): + self.assertEqual( + self.before_template_id.display_name, + "Top template (Top)", + ) + self.assertEqual( + self.after_template_id.display_name, + "Bottom template (Bottom)", + ) + + def test_general_template(self): + # Need to force _compute because only trigger when partner_id have changed + self.user._compute_comment_template_ids() + # Check getting default comment template + self.assertTrue(self.before_template_id in self.user.comment_template_ids) + self.assertTrue(self.after_template_id in self.user.comment_template_ids) + + def test_partner_template(self): + self.partner2_id.base_comment_template_ids = [ + (4, self.before_template_id.id), + (4, self.after_template_id.id), + ] + self.assertTrue( + self.before_template_id in self.partner2_id.base_comment_template_ids + ) + self.assertTrue( + self.after_template_id in self.partner2_id.base_comment_template_ids + ) + + def test_partner_template_domain(self): + # Check getting the comment template if domain is set + self.partner2_id.base_comment_template_ids = [ + (4, self.before_template_id.id), + (4, self.after_template_id.id), + ] + self.before_template_id.domain = "[('id', 'in', %s)]" % self.user.ids + self.assertTrue( + self.before_template_id in self.partner2_id.base_comment_template_ids + ) + 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): + ro_RO_lang = ( + self.env["res.lang"] + .with_context(active_test=False) + .search([("code", "=", "ro_RO")]) + ) + with mute_logger("odoo.addons.base.models.ir_translation"): + self.env["base.language.install"].create( + {"overwrite": True, "lang_ids": [(6, 0, [ro_RO_lang.id])]} + ).lang_install() + + module = self.env.ref("base.module_test_translation_import") + export = self.env["base.language.export"].create( + {"lang": "ro_RO", "format": "po", "modules": [Command.set([module.id])]} + ) + export.act_getfile() + po_file = export.data + self.assertIsNotNone(po_file) + + 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 new file mode 100644 index 0000000000..362947d0a5 --- /dev/null +++ b/base_comment_template/views/base_comment_template_view.xml @@ -0,0 +1,102 @@ + + + + base.comment.template.search + base.comment.template + + + + + + + + + + + + + + + base.comment.template.search + base.comment.template + + + + + + + + + + + + + + + + base.comment.template.form + base.comment.template + +
+ +
+ +
+
+

+ +

+
+ + + + + + + + + + + + + + + + +
+
+
+
+ + Comment Templates + ir.actions.act_window + base.comment.template + tree,form + + + + +
diff --git a/base_comment_template/views/res_partner_view.xml b/base_comment_template/views/res_partner_view.xml new file mode 100644 index 0000000000..d1fd4c7c2e --- /dev/null +++ b/base_comment_template/views/res_partner_view.xml @@ -0,0 +1,28 @@ + + + res.partner + + + + + + + + + + + + + + + + + + diff --git a/base_comment_template/wizard/__init__.py b/base_comment_template/wizard/__init__.py new file mode 100644 index 0000000000..9a0d64bbf6 --- /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 0000000000..0146390757 --- /dev/null +++ b/base_comment_template/wizard/base_comment_template_preview.py @@ -0,0 +1,85 @@ +from odoo import api, fields, models +from odoo.tools.safe_eval import safe_eval + +from odoo.addons.base.models.res_partner import _lang_get + + +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 default_get(self, fields): + result = super().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"] = f"{model.model},{res.id}" + return result + + base_comment_template_id = fields.Many2one( + "base.comment.template", required=True, ondelete="cascade" + ) + lang = fields.Selection(_lang_get, string="Template Preview Language") + engine = fields.Selection( + [ + ("inline_template", "Inline template"), + ("qweb", "QWeb"), + ("qweb_view", "QWeb view"), + ], + string="Template Preview Engine", + default="inline_template", + ) + model_ids = fields.Many2many( + "ir.model", related="base_comment_template_id.model_ids" + ) + model_id = fields.Many2one("ir.model") + body = fields.Char(compute="_compute_base_comment_template_fields") + resource_ref = fields.Reference( + string="Record reference", selection="_selection_target_model" + ) + no_record = fields.Boolean(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 0000000000..c53194c690 --- /dev/null +++ b/base_comment_template/wizard/base_comment_template_preview_views.xml @@ -0,0 +1,63 @@ + + + + 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} + +