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
20 changes: 10 additions & 10 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 All @@ -32,8 +28,10 @@ Document Page Reference

|badge1| |badge2| |badge3| |badge4| |badge5|

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

If you also want to link pages between them using those references,
install ``document_page_reference_link``.

**Table of contents**

Expand All @@ -43,9 +41,10 @@ the link between document pages.
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.
When editing a document page, set a reference to identify it uniquely.
If no reference is provided, one is generated automatically from the
page name. References can only contain letters, numbers and underscores,
and must be unique.

Bug Tracker
===========
Expand All @@ -69,6 +68,7 @@ Contributors
------------

- Enric Tobella <etobella@creublanca.es>
- Miquel Rosell <miquelroselltarrago@gmail.com>

Maintainers
-----------
Expand Down
8 changes: 1 addition & 7 deletions document_page_reference/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@
"name": "Document Page Reference",
"summary": """
Include references on document pages""",
"version": "18.0.2.1.3",
"version": "18.0.3.0.0",
"license": "AGPL-3",
"author": "Creu Blanca,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/knowledge",
"depends": ["document_page"],
"data": [
"views/document_page.xml",
"views/report_document_page.xml",
],
"assets": {
"web.assets_backend": [
"document_page_reference/static/src/js/editor.esm.js",
],
},
"maintainers": ["etobella"],
}
65 changes: 0 additions & 65 deletions document_page_reference/models/document_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@

import re

from markupsafe import Markup

from odoo import api, fields, models
from odoo.exceptions import ValidationError
from odoo.tools import html_escape


class DocumentPage(models.Model):
Expand All @@ -18,20 +15,6 @@ class DocumentPage(models.Model):
reference = fields.Char(
help="Used to find the document, it can contain letters, numbers and _"
)
content_parsed = fields.Html(
"Parsed Content", compute="_compute_content_parsed", sanitize=False, store=True
)

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("content")
def _compute_content_parsed(self):
for record in self:
record.content_parsed = record.get_content()

@api.constrains("reference")
def _check_reference_validity(self):
Expand All @@ -45,54 +28,6 @@ def _check_reference_validity(self):
if self.search(domain):
raise ValidationError(self.env._("Reference must be unique"))

def _get_document(self, code):
return self.search([("reference", "=", code)], limit=1)

def get_content(self):
self.ensure_one()
raw = str(self.content or "")
content_parsed = Markup(raw)
for text in re.findall(r"\{\{.*?\}\}", raw):
reference = re.sub(r"<[^>]*>", "", text).replace("{{", "").replace("}}", "")
content_parsed = content_parsed.replace(
text, self._resolve_reference(reference)
)
link_regex = (
r"<a[^>]*class=['\"][^'\"]*oe_direct_line[^'\"]*['\"]"
r"[^>]*name=['\"]([^'\"]*)['\"][^>]*>.*?</a>"
)
for match in re.finditer(link_regex, raw):
full_link = match.group(0)
reference = match.group(1)
content_parsed = content_parsed.replace(
Markup(full_link), self._resolve_reference(reference)
)
return content_parsed

def _inverse_content(self):
for rec in self:
if rec.type == "content":
rec.content = rec.get_content()
return super()._inverse_content()

def _resolve_reference(self, code):
doc = self._get_document(code)
if self.env.context.get("raw_reference", False):
return html_escape(doc.display_name if doc else code)
sanitized_code = html_escape(code)
oe_model = doc._name if doc else self._name
oe_id = doc.id if doc else ""
name = html_escape(doc.display_name) if doc else sanitized_code
href = doc.backend_url if doc else "#"
return Markup(
f"<a href='{href}' class='oe_direct_line' data-oe-model='{oe_model}' "
f"data-oe-id='{oe_id}' name='{sanitized_code}'>"
f"{name}</a>"
)

def get_raw_content(self):
return Markup(self.with_context(raw_reference=True).get_content())

@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
Expand Down
1 change: 1 addition & 0 deletions document_page_reference/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Enric Tobella \<etobella@creublanca.es\>
- Miquel Rosell \<miquelroselltarrago@gmail.com\>
6 changes: 4 additions & 2 deletions document_page_reference/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
This module allows to add a reference name on documents and simplifies
the link between document pages.
This module allows to add a reference name on documents.

If you also want to link pages between them using those references,
install `document_page_reference_link`.
7 changes: 4 additions & 3 deletions document_page_reference/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
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.
When editing a document page, set a reference to identify it uniquely.
If no reference is provided, one is generated automatically from the
page name. References can only contain letters, numbers and underscores,
and must be unique.
39 changes: 18 additions & 21 deletions document_page_reference/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<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">

/*
Expand Down Expand Up @@ -360,23 +360,19 @@
</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:7ae4eea4b6e7501b923f3597e005c064c4b58658b2913d8c3bfd25b1a7f9494c
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<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/18.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-18-0/knowledge-18-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=18.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><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/18.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-18-0/knowledge-18-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=18.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.</p>
<p>If you also want to link pages between them using those references,
install <tt class="docutils literal">document_page_reference_link</tt>.</p>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
Expand All @@ -391,35 +387,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>
<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>
<h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
<p>When editing a document page, set a reference to identify it uniquely.
If no reference is provided, one is generated automatically from the
page name. References can only contain letters, numbers and underscores,
and must be unique.</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:%2018.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>
<li>Miquel Rosell &lt;<a class="reference external" href="mailto:miquelroselltarrago&#64;gmail.com">miquelroselltarrago&#64;gmail.com</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" />
Expand All @@ -434,6 +432,5 @@ <h3><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h3>
</div>
</div>
</div>
</div>
</body>
</html>
41 changes: 2 additions & 39 deletions document_page_reference/tests/test_document_reference.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright 2019 Creu Blanca
# Copyright 2025 Tecnativa - Víctor Martínez
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from markupsafe import Markup

from odoo.exceptions import ValidationError

Expand All @@ -13,13 +12,8 @@ class TestDocumentReference(BaseCommon):
def setUpClass(cls):
super().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": Markup("{{r2}}"), "reference": "R1"}
)
cls.page2 = cls.page_obj.create(
{"name": "Test Page 2", "content": Markup("{{r1}}"), "reference": "r2"}
)
cls.page1 = cls.page_obj.create({"name": "Test Page 1", "reference": "R1"})
cls.page2 = cls.page_obj.create({"name": "Test Page 2", "reference": "r2"})

def test_constraints_duplicate_reference(self):
"""Should raise if reference is not unique (same as another)."""
Expand All @@ -37,11 +31,6 @@ def test_no_constrains(self):
self.page2.write({"reference": False})
self.assertFalse(self.page2.reference)

def test_check_raw(self):
self.assertEqual(
str(self.page2.display_name), str(self.page1.get_raw_content())
)

def test_auto_reference(self):
"""Test if reference is proposed when saving a page without one."""
self.assertEqual(self.page1.reference, "R1")
Expand All @@ -58,29 +47,3 @@ def test_auto_reference(self):
}
)
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_keys = {
"type": "ir.actions.act_window",
"res_model": "document.page",
"res_id": self.page1.id,
"target": "current",
"views": [(view_id, "form")],
}
for key, expected_value in expected_keys.items():
self.assertEqual(res.get(key), expected_value, f"Mismatch in key: {key}")

def test_compute_content_parsed(self):
self.page1.content = Markup("<p>{{r2}}</p>")
self.assertIn("data-oe-model='document.page'", self.page1.content_parsed)
self.assertIn(f"data-oe-id='{self.page2.id}'", self.page1.content_parsed)
self.assertIn(f"href='{self.page2.backend_url}'", self.page1.content_parsed)
self.assertIn("Test Page 2", self.page1.content_parsed)

def test_inverse_content_replacement(self):
self.page1.content = "{{r2}}"
self.assertIn(f"data-oe-id='{self.page2.id}'", self.page1.content)
self.assertIn(f"href='{self.page2.backend_url}'", self.page1.content_parsed)
self.assertNotIn("&lt;a", self.page1.content)
28 changes: 0 additions & 28 deletions document_page_reference/views/document_page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,6 @@
/>
</h2>
</xpath>
<field name="content" position="attributes">
<attribute name="class">oe_edit_only</attribute>
</field>
<field name="content" position="before">
<field
name="content_parsed"
class="oe_read_only"
widget="document_page_reference"
/>
</field>
</field>
</record>

<record id="view_wiki_menu_form" model="ir.ui.view">
<field name="name">document.page.menu.form</field>
<field name="model">document.page</field>
<field name="inherit_id" ref="document_page.view_wiki_menu_form" />
<field name="arch" type="xml">
<field name="content" position="attributes">
<attribute name="class">oe_edit_only</attribute>
</field>
<field name="content" position="before">
<field
name="content_parsed"
class="oe_read_only"
widget="document_page_reference"
/>
</field>
</field>
</record>

Expand Down
Loading
Loading