-
-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
779 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <https://github.com/OCA/knowledge/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 <https://github.com/OCA/knowledge/issues/new?body=module:%20document_page_reference%0Aversion:%2011.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_. | ||
|
||
Do not contact contributors directly about support or help with technical issues. | ||
|
||
Credits | ||
======= | ||
|
||
Authors | ||
~~~~~~~ | ||
|
||
* Creu Blanca | ||
|
||
Contributors | ||
~~~~~~~~~~~~ | ||
|
||
* Enric Tobella <etobella@creublanca.es> | ||
|
||
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 <https://github.com/OCA/knowledge/tree/11.0/document_page_reference>`_ project on GitHub. | ||
|
||
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# 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', | ||
], | ||
'data': [ | ||
'views/document_page.xml', | ||
'views/report_document_page.xml', | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from . import base | ||
from . import document_page |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from odoo import models | ||
from werkzeug.urls import url_encode | ||
|
||
|
||
class Base(models.AbstractModel): | ||
_inherit = 'base' | ||
|
||
def get_direct_access_url(self): | ||
self.ensure_one() | ||
params = { | ||
'model': self._name, | ||
'id': self.id, | ||
'view_type': 'form', | ||
} | ||
return '/web#?' + url_encode(params) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# 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="</%doc>", | ||
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) | ||
return '<a href="%s">%s</a>' % ( | ||
element.get_direct_access_url(), 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* Enric Tobella <etobella@creublanca.es> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
This module allows to add a reference name on documents and simplifies the link | ||
between document pages. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.