Skip to content

Commit

Permalink
Merge abb58bb into da79d10
Browse files Browse the repository at this point in the history
  • Loading branch information
etobella committed Nov 27, 2019
2 parents da79d10 + abb58bb commit 497b041
Show file tree
Hide file tree
Showing 15 changed files with 761 additions and 0 deletions.
81 changes: 81 additions & 0 deletions document_page_reference/README.rst
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.
1 change: 1 addition & 0 deletions document_page_reference/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
21 changes: 21 additions & 0 deletions document_page_reference/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 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',
],
'demo': [
],
}
2 changes: 2 additions & 0 deletions document_page_reference/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import base
from . import document_page
14 changes: 14 additions & 0 deletions document_page_reference/models/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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,
'res_id': self.id,
}
return '/mail/view?' + url_encode(params)
88 changes: 88 additions & 0 deletions document_page_reference/models/document_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# 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
import logging
_logger = logging.getLogger(__name__)

try:
from jinja2.sandbox import SandboxedEnvironment
from jinja2 import Undefined

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()
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 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 element.display_name
return '<a href="%s">%s</a>' % (
element.get_direct_access_url(), element.display_name)

def _get_template_variables(self):
return {'ref': self.get_reference}

def get_content(self):
content = self.content
mako_env = mako_template_env
template = mako_env.from_string(tools.ustr(content))
return template.render(self._get_template_variables())

def get_raw_content(self):
return self.with_context(raw_reference=True).get_content()
1 change: 1 addition & 0 deletions document_page_reference/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Enric Tobella <etobella@creublanca.es>
2 changes: 2 additions & 0 deletions document_page_reference/readme/DESCRIPTION.rst
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.
3 changes: 3 additions & 0 deletions document_page_reference/readme/USAGE.rst
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.

0 comments on commit 497b041

Please sign in to comment.