Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions document_page_reference/README.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
.. image:: https://odoo-community.org/readme-banner-image
:target: https://odoo-community.org/get-involved?utm_source=readme
:alt: Odoo Community Association

=======================
Document Page Reference
=======================
Expand All @@ -17,7 +13,7 @@ Document Page Reference
.. |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/license-AGPL--3-blue.png
.. |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
Expand Down
3 changes: 3 additions & 0 deletions document_page_reference/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
"web.assets_backend": [
"document_page_reference/static/src/js/**/*",
],
"web.assets_tests": [
"document_page_reference/static/tests/**/*",
],
},
"maintainers": ["etobella"],
}
50 changes: 40 additions & 10 deletions document_page_reference/models/document_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import logging

from markupsafe import Markup

from odoo import _, api, fields, models, tools
from odoo.exceptions import ValidationError
from odoo.tools.misc import html_escape
Expand Down Expand Up @@ -52,25 +54,53 @@ class DocumentPage(models.Model):
reference = fields.Char(
help="Used to find the document, it can contain letters, numbers and _"
)
content_parsed = fields.Html(compute="_compute_content_parsed")
content_parsed = fields.Html(compute="_compute_content_parsed", sanitize=False)

def _get_page_index(self, link=True):
"""Override to use oe_direct_line links compatible with the widget."""
self.ensure_one()
index = [
Markup("<li>") + subpage._get_page_index() + Markup("</li>")
for subpage in self.child_ids
]
r = Markup("")
if link:
r = (
Markup(
'<a href="#" class="oe_direct_line"'
' data-oe-model="%s" data-oe-id="%s">'
)
% (
self._name,
self.id,
)
+ html_escape(self.name)
+ Markup("</a>")
)
if index:
r += Markup("<ul>") + Markup("").join(index) + Markup("</ul>")
return r

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")
@api.depends("history_head", "type")
def _compute_content_parsed(self):
for record in self:
content = record.get_content()
if content == "<p>" and self.content != "<p>":
_logger.error(
"Template from page with id = %s cannot be processed correctly"
% self.id
)
content = self.content
record.content_parsed = content
if record.type == "category":
record.content_parsed = record.content
else:
content = record.get_content()
if content == "<p>" and record.content != "<p>":
_logger.error(
"Template from page with id = %s cannot be "
"processed correctly" % record.id
)
content = record.content
record.content_parsed = content

@api.constrains("reference")
def _check_reference(self):
Expand Down
37 changes: 14 additions & 23 deletions document_page_reference/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
<title>README.rst</title>
<title>Document Page Reference</title>
<style type="text/css">

/*
:Author: David Goodger (goodger@python.org)
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.
Despite the name, some widely supported CSS2 features are used.

See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
customize this style sheet.
Expand Down Expand Up @@ -275,7 +274,7 @@
margin-left: 2em ;
margin-right: 2em }

pre.code .ln { color: gray; } /* line numbers */
pre.code .ln { color: grey; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
Expand All @@ -301,7 +300,7 @@
span.pre {
white-space: pre }

span.problematic, pre.problematic {
span.problematic {
color: red }

span.section-subtitle {
Expand Down Expand Up @@ -360,21 +359,16 @@
</style>
</head>
<body>
<div class="document">
<div class="document" id="document-page-reference">
<h1 class="title">Document Page Reference</h1>


<a class="reference external image-reference" href="https://odoo-community.org/get-involved?utm_source=readme">
<img alt="Odoo Community Association" src="https://odoo-community.org/readme-banner-image" />
</a>
<div class="section" id="document-page-reference">
<h1>Document Page Reference</h1>
<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:3604a4c11db5706c09226e3e0d5a5363cc7272e856131e06c4a5a47b43616e20
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/license-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/knowledge/tree/16.0/document_page_reference"><img alt="OCA/knowledge" src="https://img.shields.io/badge/github-OCA%2Fknowledge-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/knowledge-16-0/knowledge-16-0-document_page_reference"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/knowledge&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/knowledge/tree/16.0/document_page_reference"><img alt="OCA/knowledge" src="https://img.shields.io/badge/github-OCA%2Fknowledge-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/knowledge-16-0/knowledge-16-0-document_page_reference"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/knowledge&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module allows to add a reference name on documents and simplifies the link
between document pages.</p>
<p><strong>Table of contents</strong></p>
Expand All @@ -391,39 +385,37 @@ <h1>Document Page Reference</h1>
</ul>
</div>
<div class="section" id="usage">
<h2><a class="toc-backref" href="#toc-entry-1">Usage</a></h2>
<h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
<p>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.</p>
</div>
<div class="section" id="bug-tracker">
<h2><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h2>
<h1><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/knowledge/issues">GitHub Issues</a>.
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
<a class="reference external" href="https://github.com/OCA/knowledge/issues/new?body=module:%20document_page_reference%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
<h2><a class="toc-backref" href="#toc-entry-3">Credits</a></h2>
<h1><a class="toc-backref" href="#toc-entry-3">Credits</a></h1>
<div class="section" id="authors">
<h3><a class="toc-backref" href="#toc-entry-4">Authors</a></h3>
<h2><a class="toc-backref" href="#toc-entry-4">Authors</a></h2>
<ul class="simple">
<li>Creu Blanca</li>
</ul>
</div>
<div class="section" id="contributors">
<h3><a class="toc-backref" href="#toc-entry-5">Contributors</a></h3>
<h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
<ul class="simple">
<li>Enric Tobella &lt;<a class="reference external" href="mailto:etobella&#64;creublanca.es">etobella&#64;creublanca.es</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">
<h3><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h3>
<h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
</a>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<p>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.</p>
Expand All @@ -434,6 +426,5 @@ <h3><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h3>
</div>
</div>
</div>
</div>
</body>
</html>
42 changes: 42 additions & 0 deletions document_page_reference/static/src/js/editor.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* @odoo-module */

import {HtmlField} from "@web_editor/js/backend/html_field";
import {registry} from "@web/core/registry";
import {useService} from "@web/core/utils/hooks";
import {onMounted, onPatched} from "@odoo/owl";

export class DocumentPageReferenceField extends HtmlField {
setup() {
super.setup();
this.actionService = useService("action");
this.orm = useService("orm");
this._onClickDirectLink = this._onClickDirectLink.bind(this);
onMounted(() => this._bindLinks());
onPatched(() => this._bindLinks());
}
_bindLinks() {
const el = this.readonlyElementRef && this.readonlyElementRef.el;
if (!el) return;
// Remove target="_blank" from internal reference links
// (added by retargetLinks in HtmlField)
for (const link of el.querySelectorAll("a.oe_direct_line")) {
link.removeAttribute("target");
link.removeAttribute("rel");
link.removeEventListener("click", this._onClickDirectLink);
link.addEventListener("click", this._onClickDirectLink);
}
}
_onClickDirectLink(ev) {
ev.preventDefault();
ev.stopPropagation();
const target = ev.currentTarget;
const model = target.dataset.oeModel;
const id = parseInt(target.dataset.oeId, 10);
if (!model || !id) return;
this.orm.call(model, "get_formview_action", [[id]]).then((action) => {
this.actionService.doAction(action);
});
}
}

registry.category("fields").add("document_page_reference", DocumentPageReferenceField);
34 changes: 0 additions & 34 deletions document_page_reference/static/src/js/editor.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/** @odoo-module */

import tour from "web_tour.tour";

/*
* Test 1: Reference widget renders ${ref} as clickable links.
*/
tour.register(
"document_page_reference_widget_tour",
{
test: true,
url: "/web#action=document_page.action_page",
},
[
{
content: "Open Test Ref Page 1",
trigger: '.o_data_cell[name="name"]:contains("Test Ref Page 1")',
run: "click",
},
{
content: "Verify content_parsed renders reference as link",
trigger:
'.o_form_view .o_field_widget[name="content_parsed"] a.oe_direct_line',
timeout: 20000,
run: function () {
var link = this.$anchor[0];
if (!link.dataset.oeModel || !link.dataset.oeId) {
throw new Error("Reference link missing data-oe-model/data-oe-id");
}
if (link.getAttribute("target") === "_blank") {
throw new Error(
"Internal reference link should not have target=_blank"
);
}
},
},
]
);

/*
* Test 2: Category page index uses oe_direct_line links.
*/
tour.register(
"document_page_reference_category_tour",
{
test: true,
url: "/web#action=document_page.action_page",
},
[
{
content: "Open Test Ref Page 1 to navigate to its category",
trigger: '.o_data_cell[name="name"]:contains("Test Ref Page 1")',
run: "click",
},
{
content: "Navigate to category via parent_id link",
trigger: '.o_form_view .o_field_widget[name="parent_id"] a',
timeout: 20000,
run: "click",
},
{
content: "Verify category shows child links as oe_direct_line",
trigger:
'.o_form_view .o_field_widget[name="content_parsed"] a.oe_direct_line',
timeout: 20000,
run: function () {
var links = document.querySelectorAll(
'.o_field_widget[name="content_parsed"] a.oe_direct_line'
);
if (links.length < 1) {
throw new Error("Category should have child page links");
}
// Verify links have correct attributes
var link = links[0];
if (!link.dataset.oeModel || !link.dataset.oeId) {
throw new Error("Category index link missing data-oe-model/id");
}
if (link.getAttribute("target") === "_blank") {
throw new Error("Category index link should not open in new tab");
}
},
},
]
);
1 change: 1 addition & 0 deletions document_page_reference/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import test_document_reference
from . import test_document_reference_tour
2 changes: 1 addition & 1 deletion document_page_reference/tests/test_document_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ def test_get_formview_action(self):
def test_compute_content_parsed(self):
self.page1.content = "<p>"
self.page1._compute_content_parsed()
self.assertEqual(str(self.page1.content_parsed), "<p></p>")
self.assertEqual(str(self.page1.content_parsed), "<p>")
Loading
Loading