diff --git a/document_page_reference/README.rst b/document_page_reference/README.rst new file mode 100644 index 00000000000..d9d808a2b1b --- /dev/null +++ b/document_page_reference/README.rst @@ -0,0 +1,89 @@ +======================= +Document Page Reference +======================= + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fknowledge-lightgray.png?logo=github + :target: https://github.com/OCA/knowledge/tree/15.0/document_page_reference + :alt: OCA/knowledge +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/knowledge-15-0/knowledge-15-0-document_page_reference + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/118/15.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module allows to add a reference name on documents and simplifies the link +between document pages. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +When editing a document page add elements like ${XXX} where XXX is the reference +of another page. Now, when viewing the document, it will link directly to the page. +Also, the name will be parsed as the display name. + +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 smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Creu Blanca + +Contributors +~~~~~~~~~~~~ + +* Enric Tobella + +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. + +.. |maintainer-etobella| image:: https://github.com/etobella.png?size=40px + :target: https://github.com/etobella + :alt: etobella + +Current `maintainer `__: + +|maintainer-etobella| + +This module is part of the `OCA/knowledge `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/document_page_reference/__init__.py b/document_page_reference/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/document_page_reference/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/document_page_reference/__manifest__.py b/document_page_reference/__manifest__.py new file mode 100644 index 00000000000..9ca95755e41 --- /dev/null +++ b/document_page_reference/__manifest__.py @@ -0,0 +1,23 @@ +# Copyright 2019 Creu Blanca +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Document Page Reference", + "summary": """ + Include references on document pages""", + "version": "16.0.1.0.0", + "license": "AGPL-3", + "author": "Creu Blanca,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/knowledge", + "depends": ["document_page", "web_editor"], + "data": [ + "views/document_page.xml", + "views/report_document_page.xml", + ], + "assets": { + "web.assets_backend": [ + "document_page_reference/static/src/js/**/*", + ], + }, + "maintainers": ["etobella"], +} diff --git a/document_page_reference/i18n/document_page_reference.pot b/document_page_reference/i18n/document_page_reference.pot new file mode 100644 index 00000000000..f9102683e33 --- /dev/null +++ b/document_page_reference/i18n/document_page_reference.pot @@ -0,0 +1,51 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * document_page_reference +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 15.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: document_page_reference +#: model:ir.model.fields,field_description:document_page_reference.field_document_page__content_parsed +msgid "Content Parsed" +msgstr "" + +#. module: document_page_reference +#: model:ir.model,name:document_page_reference.model_document_page +msgid "Document Page" +msgstr "" + +#. module: document_page_reference +#: model:ir.model.fields,field_description:document_page_reference.field_document_page__reference +msgid "Reference" +msgstr "" + +#. module: document_page_reference +#: code:addons/document_page_reference/models/document_page.py:0 +#, python-format +msgid "Reference is not valid" +msgstr "" + +#. module: document_page_reference +#: code:addons/document_page_reference/models/document_page.py:0 +#, python-format +msgid "Reference must be unique" +msgstr "" + +#. module: document_page_reference +#: model:ir.model.fields,help:document_page_reference.field_document_page__reference +msgid "Used to find the document, it can contain letters, numbers and _" +msgstr "" + +#. module: document_page_reference +#: model_terms:ir.ui.view,arch_db:document_page_reference.document_page_form_view +msgid "internal_reference (autofilled if not value is provided)" +msgstr "" diff --git a/document_page_reference/i18n/it.po b/document_page_reference/i18n/it.po new file mode 100644 index 00000000000..2091e1786db --- /dev/null +++ b/document_page_reference/i18n/it.po @@ -0,0 +1,54 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * document_page_reference +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 15.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2023-06-23 10:10+0000\n" +"Last-Translator: mymage \n" +"Language-Team: none\n" +"Language: it\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" +"X-Generator: Weblate 4.17\n" + +#. module: document_page_reference +#: model:ir.model.fields,field_description:document_page_reference.field_document_page__content_parsed +msgid "Content Parsed" +msgstr "Contenuto analizzato" + +#. module: document_page_reference +#: model:ir.model,name:document_page_reference.model_document_page +msgid "Document Page" +msgstr "Pagina documento" + +#. module: document_page_reference +#: model:ir.model.fields,field_description:document_page_reference.field_document_page__reference +msgid "Reference" +msgstr "Riferimento" + +#. module: document_page_reference +#: code:addons/document_page_reference/models/document_page.py:0 +#, python-format +msgid "Reference is not valid" +msgstr "Il riferimento non è valido" + +#. module: document_page_reference +#: code:addons/document_page_reference/models/document_page.py:0 +#, python-format +msgid "Reference must be unique" +msgstr "Il riferimento deve essere unico" + +#. module: document_page_reference +#: model:ir.model.fields,help:document_page_reference.field_document_page__reference +msgid "Used to find the document, it can contain letters, numbers and _" +msgstr "Utilizzato per trovare il documento, può contenere lettere, numeri e _" + +#. module: document_page_reference +#: model_terms:ir.ui.view,arch_db:document_page_reference.document_page_form_view +msgid "internal_reference (autofilled if not value is provided)" +msgstr "internal_reference (auto compilato se non viene fornito un valore)" diff --git a/document_page_reference/models/__init__.py b/document_page_reference/models/__init__.py new file mode 100644 index 00000000000..427be24e72a --- /dev/null +++ b/document_page_reference/models/__init__.py @@ -0,0 +1 @@ +from . import document_page diff --git a/document_page_reference/models/document_page.py b/document_page_reference/models/document_page.py new file mode 100644 index 00000000000..5dea1ea47d0 --- /dev/null +++ b/document_page_reference/models/document_page.py @@ -0,0 +1,150 @@ +# Copyright 2019 Creu Blanca +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import logging + +from odoo import _, api, fields, models, tools +from odoo.exceptions import ValidationError +from odoo.tools.misc import html_escape + +from odoo.addons.http_routing.models.ir_http import slugify + +_logger = logging.getLogger(__name__) + +try: + import re + + from jinja2 import Undefined + from jinja2.lexer import name_re as old_name_re + from jinja2.sandbox import SandboxedEnvironment + + name_re = re.compile("^%s$" % old_name_re.pattern) + + class Context(SandboxedEnvironment.context_class): + def resolve(self, key): + res = super().resolve(key) + if not isinstance(res, Undefined): + return res + return self.parent["ref"](key) + + class Environment(SandboxedEnvironment): + context_class = Context + + mako_template_env = Environment( + block_start_string="<%", + block_end_string="%>", + variable_start_string="${", + variable_end_string="}", + comment_start_string="<%doc>", + comment_end_string="", + line_statement_prefix="%", + line_comment_prefix="##", + trim_blocks=True, # do not output newline after blocks + autoescape=False, + ) +except Exception: + _logger.error("Jinja2 is not available") + + +class DocumentPage(models.Model): + + _inherit = "document.page" + + reference = fields.Char( + help="Used to find the document, it can contain letters, numbers and _" + ) + content_parsed = fields.Html(compute="_compute_content_parsed") + + def get_formview_action(self, access_uid=None): + res = super().get_formview_action(access_uid) + view_id = self.env.ref("document_page.view_wiki_form").id + res["views"] = [(view_id, "form")] + return res + + @api.depends("history_head") + def _compute_content_parsed(self): + for record in self: + content = record.get_content() + if content == "

" and self.content != "

": + _logger.error( + "Template from page with id = %s cannot be processed correctly" + % self.id + ) + content = self.content + record.content_parsed = content + + @api.constrains("reference") + def _check_reference(self): + for record in self: + if not record.reference: + continue + record._validate_reference(record=record) + + @api.model + def _validate_reference(self, record=None, reference=None): + if not reference: + reference = self.reference + if not name_re.match(reference): + raise ValidationError(_("Reference is not valid")) + uniq_domain = [("reference", "=", reference)] + if record: + uniq_domain += [("id", "!=", record.id)] + if self.search(uniq_domain): + raise ValidationError(_("Reference must be unique")) + + def _get_document(self, code): + # Hook created in order to add check on other models + document = self.search([("reference", "=", code)]) + if document: + return document + else: + return self.env[self._name] + + def get_reference(self, code): + element = self._get_document(code) + if self.env.context.get("raw_reference", False): + return html_escape(element.display_name) + text = """%s + """ + if not element: + text = "%s" % text + res = text % ( + element._name, + element and element.id or "", + code, + html_escape(element.display_name or code), + ) + return res + + def _get_template_variables(self): + return {"ref": self.get_reference} + + def get_content(self): + try: + content = self.content + mako_env = mako_template_env + template = mako_env.from_string(tools.ustr(content)) + return template.render(self._get_template_variables()) + except Exception: + _logger.error( + "Template from page with id = %s cannot be processed" % self.id + ) + return self.content + + def get_raw_content(self): + return self.with_context(raw_reference=True).get_content() + + @api.model_create_multi + def create(self, vals_list): + for vals in vals_list: + if not vals.get("reference"): + # Propose a default reference + reference = slugify(vals.get("name")).replace("-", "_") + try: + self._validate_reference(reference=reference) + vals["reference"] = reference + except ValidationError: # pylint: disable=W7938 + # Do not fill reference. + pass + return super(DocumentPage, self).create(vals_list) diff --git a/document_page_reference/readme/CONTRIBUTORS.rst b/document_page_reference/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000000..93ec993e044 --- /dev/null +++ b/document_page_reference/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Enric Tobella diff --git a/document_page_reference/readme/DESCRIPTION.rst b/document_page_reference/readme/DESCRIPTION.rst new file mode 100644 index 00000000000..dfddc47b4ff --- /dev/null +++ b/document_page_reference/readme/DESCRIPTION.rst @@ -0,0 +1,2 @@ +This module allows to add a reference name on documents and simplifies the link +between document pages. diff --git a/document_page_reference/readme/USAGE.rst b/document_page_reference/readme/USAGE.rst new file mode 100644 index 00000000000..5f341f02648 --- /dev/null +++ b/document_page_reference/readme/USAGE.rst @@ -0,0 +1,3 @@ +When editing a document page add elements like ${XXX} where XXX is the reference +of another page. Now, when viewing the document, it will link directly to the page. +Also, the name will be parsed as the display name. diff --git a/document_page_reference/static/description/icon.png b/document_page_reference/static/description/icon.png new file mode 100644 index 00000000000..3a0328b516c Binary files /dev/null and b/document_page_reference/static/description/icon.png differ diff --git a/document_page_reference/static/description/index.html b/document_page_reference/static/description/index.html new file mode 100644 index 00000000000..8c4ae9c013a --- /dev/null +++ b/document_page_reference/static/description/index.html @@ -0,0 +1,429 @@ + + + + + + +Document Page Reference + + + +

+

Document Page Reference

+ + +

Beta License: AGPL-3 OCA/knowledge Translate me on Weblate Try me on Runbot

+

This module allows to add a reference name on documents and simplifies the link +between document pages.

+

Table of contents

+ +
+

Usage

+

When editing a document page add elements like ${XXX} where XXX is the reference +of another page. Now, when viewing the document, it will link directly to the page. +Also, the name will be parsed as the display name.

+
+
+

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 smashing it by providing a detailed and welcomed +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Creu Blanca
  • +
+
+
+

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.

+

Current maintainer:

+

etobella

+

This module is part of the OCA/knowledge project on GitHub.

+

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

+
+
+
+ + diff --git a/document_page_reference/static/src/js/editor.js b/document_page_reference/static/src/js/editor.js new file mode 100644 index 00000000000..852df084ca9 --- /dev/null +++ b/document_page_reference/static/src/js/editor.js @@ -0,0 +1,34 @@ +odoo.define("document_page_reference.backend", function (require) { + "use strict"; + + var field_registry = require("web.field_registry"); + var FieldTextHtmlSimple = require("web_editor.field.html"); + var FieldDocumentPage = FieldTextHtmlSimple.extend({ + events: _.extend({}, FieldTextHtmlSimple.prototype.events, { + "click .oe_direct_line": "_onClickDirectLink", + }), + _onClickDirectLink: function (event) { + var self = this; + event.preventDefault(); + event.stopPropagation(); + var element = $(event.target).closest(".oe_direct_line")[0]; + var default_reference = element.name; + var model = $(event.target).data("oe-model"); + var id = $(event.target).data("oe-id"); + var context = this.record.getContext(this.recordParams); + if (default_reference) { + context.default_reference = default_reference; + } + this._rpc({ + model: model, + method: "get_formview_action", + args: [[parseInt(id, 10)]], + context: context, + }).then(function (action) { + self.trigger_up("do_action", {action: action}); + }); + }, + }); + field_registry.add("document_page_reference", FieldDocumentPage); + return FieldDocumentPage; +}); diff --git a/document_page_reference/tests/__init__.py b/document_page_reference/tests/__init__.py new file mode 100644 index 00000000000..ca802a6bb24 --- /dev/null +++ b/document_page_reference/tests/__init__.py @@ -0,0 +1 @@ +from . import test_document_reference diff --git a/document_page_reference/tests/test_document_reference.py b/document_page_reference/tests/test_document_reference.py new file mode 100644 index 00000000000..cdaea8854b4 --- /dev/null +++ b/document_page_reference/tests/test_document_reference.py @@ -0,0 +1,78 @@ +# Copyright 2019 Creu Blanca +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.exceptions import ValidationError +from odoo.tests.common import TransactionCase + + +class TestDocumentReference(TransactionCase): + @classmethod + def setUpClass(cls): + super(TestDocumentReference, cls).setUpClass() + cls.page_obj = cls.env["document.page"] + cls.history_obj = cls.env["document.page.history"] + cls.page1 = cls.page_obj.create( + {"name": "Test Page 1", "content": "${r2}", "reference": "R1"} + ) + cls.page2 = cls.page_obj.create( + {"name": "Test Page 1", "content": "${r1}", "reference": "r2"} + ) + + def test_constrains_01(self): + with self.assertRaises(ValidationError): + self.page2.write({"reference": self.page1.reference}) + + def test_constrains_02(self): + with self.assertRaises(ValidationError): + self.page2.write({"reference": self.page2.reference + "-02"}) + + def test_no_contrains(self): + self.page1.write({"reference": False}) + self.page2.write({"reference": False}) + self.assertEqual(self.page1.reference, self.page2.reference) + + def test_check_raw(self): + self.assertEqual(self.page2.display_name, self.page1.get_raw_content()) + + def test_check_reference(self): + self.assertRegex(self.page1.content_parsed, ".*%s.*" % self.page2.display_name) + + def test_no_reference(self): + self.page2.reference = "r3" + self.assertRegex(self.page1.content_parsed, ".*r2.*") + + def test_auto_reference(self): + """Test if reference is proposed when saving a page without one.""" + self.assertEqual(self.page1.reference, "R1") + new_page = self.page_obj.create( + {"name": "Test Page with no rEfErenCe", "content": "some content"} + ) + self.assertEqual(new_page.reference, "test_page_with_no_reference") + new_page_duplicated_name = self.page_obj.create( + { + "name": "test page with no reference", + "content": "this should have an empty reference " + "because reference must be unique", + } + ) + self.assertFalse(new_page_duplicated_name.reference) + + def test_get_formview_action(self): + res = self.page1.get_formview_action() + view_id = self.env.ref("document_page.view_wiki_form").id + expected_result = { + "type": "ir.actions.act_window", + "context": {}, + "res_model": "document.page", + "res_id": self.page1.id, + "view_mode": "form", + "view_type": "form", + "target": "current", + "views": [(view_id, "form")], + } + self.assertEqual(res, expected_result) + + def test_compute_content_parsed(self): + self.page1.content = "

" + self.page1._compute_content_parsed() + self.assertEqual(str(self.page1.content_parsed), "

") diff --git a/document_page_reference/views/document_page.xml b/document_page_reference/views/document_page.xml new file mode 100644 index 00000000000..a9459fcceb2 --- /dev/null +++ b/document_page_reference/views/document_page.xml @@ -0,0 +1,67 @@ + + + + + document.page.form (in knowledge_reference) + document.page + + + +

+ +

+
+ + oe_edit_only + + + + + +
+ + document.page.menu.form + document.page + + + + oe_edit_only + + + + + + + + document.page.search (in knowledge_reference) + document.page + + + + + + + + + document.page.tree (in knowledge_reference) + document.page + + + + + + + +
diff --git a/document_page_reference/views/report_document_page.xml b/document_page_reference/views/report_document_page.xml new file mode 100644 index 00000000000..5ecaa1509ee --- /dev/null +++ b/document_page_reference/views/report_document_page.xml @@ -0,0 +1,14 @@ + + + + diff --git a/setup/document_page_reference/odoo/addons/document_page_reference b/setup/document_page_reference/odoo/addons/document_page_reference new file mode 120000 index 00000000000..670c634ed3f --- /dev/null +++ b/setup/document_page_reference/odoo/addons/document_page_reference @@ -0,0 +1 @@ +../../../../document_page_reference \ No newline at end of file diff --git a/setup/document_page_reference/setup.py b/setup/document_page_reference/setup.py new file mode 100644 index 00000000000..28c57bb6403 --- /dev/null +++ b/setup/document_page_reference/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)