diff --git a/document_page_reference/README.rst b/document_page_reference/README.rst new file mode 100644 index 00000000..adc253cb --- /dev/null +++ b/document_page_reference/README.rst @@ -0,0 +1,81 @@ +======================= +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/11.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-11-0/knowledge-11-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/11.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. + +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 00000000..0650744f --- /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 00000000..c3a6a981 --- /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': '11.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/assets.xml', + 'views/document_page.xml', + 'views/report_document_page.xml', + ], + 'maintainers': ['etobella'], + 'development_status': 'Alpha', +} diff --git a/document_page_reference/models/__init__.py b/document_page_reference/models/__init__.py new file mode 100644 index 00000000..427be24e --- /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 00000000..1f5a6709 --- /dev/null +++ b/document_page_reference/models/document_page.py @@ -0,0 +1,108 @@ +# Copyright 2019 Creu Blanca +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models, tools, _ +from odoo.exceptions import ValidationError +from odoo.tools.misc import html_escape + +import logging +_logger = logging.getLogger(__name__) + +try: + from jinja2.sandbox import SandboxedEnvironment + from jinja2 import Undefined + from jinja2.lexer import name_re as old_name_re + import re + + name_re = re.compile(u'^%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') + + @api.depends('history_head') + def _compute_content_parsed(self): + for record in self: + record.content_parsed = record.get_content() + + @api.constrains('reference') + def _check_reference(self): + for record in self: + if not record.reference: + continue + if not name_re.match(record.reference): + raise ValidationError(_('Reference is not valid')) + if self.search([ + ('reference', '=', record.reference), + ('id', '!=', record.id)] + ): + 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 + return False + + def get_reference(self, code): + element = self._get_document(code) + if not element: + return code + if self.env.context.get('raw_reference', False): + return html_escape(element.display_name) + text = '%s' + return text % ( + element._name, + element.id, + html_escape(element.display_name) + ) + + 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 %s cannot be processed' % self.id) + return self.content + + def get_raw_content(self): + return self.with_context(raw_reference=True).get_content() diff --git a/document_page_reference/readme/CONTRIBUTORS.rst b/document_page_reference/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..93ec993e --- /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 00000000..dfddc47b --- /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 00000000..5f341f02 --- /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 00000000..3a0328b5 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 00000000..87f2d10f --- /dev/null +++ b/document_page_reference/static/description/index.html @@ -0,0 +1,427 @@ + + + + + + +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.

+

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 00000000..b3533a87 --- /dev/null +++ b/document_page_reference/static/src/js/editor.js @@ -0,0 +1,30 @@ +odoo.define('document_page_reference.backend', function (require) { + 'use strict'; + + var field_registry = require('web.field_registry'); + var backend = require('web_editor.backend'); + var FieldTextHtmlSimple = backend.FieldTextHtmlSimple; + + 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]; + this._rpc({ + model: element.name, + method: 'get_formview_action', + args: [[parseInt(element.dataset.id)]], + context: this.record.getContext(this.recordParams), + }) + .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 00000000..ca802a6b --- /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 00000000..4be351b4 --- /dev/null +++ b/document_page_reference/tests/test_document_reference.py @@ -0,0 +1,49 @@ +# Copyright 2019 Creu Blanca +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.tests.common import TransactionCase +from odoo.exceptions import ValidationError + + +class TestDocumentReference(TransactionCase): + + def setUp(self): + super().setUp() + self.page_obj = self.env['document.page'] + self.history_obj = self.env['document.page.history'] + self.page1 = self.page_obj.create({ + 'name': 'Test Page 1', + 'content': '${r2}', + 'reference': 'R1' + }) + self.page2 = self.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.*') diff --git a/document_page_reference/views/assets.xml b/document_page_reference/views/assets.xml new file mode 100644 index 00000000..e292a0aa --- /dev/null +++ b/document_page_reference/views/assets.xml @@ -0,0 +1,8 @@ + + + + diff --git a/document_page_reference/views/document_page.xml b/document_page_reference/views/document_page.xml new file mode 100644 index 00000000..f4bf3486 --- /dev/null +++ b/document_page_reference/views/document_page.xml @@ -0,0 +1,62 @@ + + + + + + + document.page.form (in knowledge_reference) + document.page + + + +

+ +

+
+ + oe_edit_only + + + + + +
+ + + document.page.menu.form + document.page + + + + 1 + + + + + + + + + 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 100755 index 00000000..77946569 --- /dev/null +++ b/document_page_reference/views/report_document_page.xml @@ -0,0 +1,13 @@ + + + + + +