mirror of
https://github.com/OCA/knowledge.git
synced 2025-12-19 03:42:19 -06:00
[IMP] document_page_reference: black, isort, prettier
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
# Copyright 2019 Creu Blanca
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import api, fields, models, tools, _
|
||||
import logging
|
||||
|
||||
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:
|
||||
@@ -14,14 +15,14 @@ try:
|
||||
from jinja2.lexer import name_re as old_name_re
|
||||
import re
|
||||
|
||||
name_re = re.compile(u'^%s$' % old_name_re.pattern)
|
||||
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)
|
||||
return self.parent["ref"](key)
|
||||
|
||||
class Environment(SandboxedEnvironment):
|
||||
context_class = Context
|
||||
@@ -35,7 +36,7 @@ try:
|
||||
comment_end_string="</%doc>",
|
||||
line_statement_prefix="%",
|
||||
line_comment_prefix="##",
|
||||
trim_blocks=True, # do not output newline after blocks
|
||||
trim_blocks=True, # do not output newline after blocks
|
||||
autoescape=False,
|
||||
)
|
||||
except Exception:
|
||||
@@ -44,34 +45,33 @@ except Exception:
|
||||
|
||||
class DocumentPage(models.Model):
|
||||
|
||||
_inherit = 'document.page'
|
||||
_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')
|
||||
content_parsed = fields.Html(compute="_compute_content_parsed")
|
||||
|
||||
@api.depends('history_head')
|
||||
@api.depends("history_head")
|
||||
def _compute_content_parsed(self):
|
||||
for record in self:
|
||||
record.content_parsed = record.get_content()
|
||||
|
||||
@api.constrains('reference')
|
||||
@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 is not valid"))
|
||||
if self.search(
|
||||
[("reference", "=", record.reference), ("id", "!=", record.id)]
|
||||
):
|
||||
raise ValidationError(_('Reference must be unique'))
|
||||
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)])
|
||||
document = self.search([("reference", "=", code)])
|
||||
if document:
|
||||
return document
|
||||
else:
|
||||
@@ -79,23 +79,23 @@ class DocumentPage(models.Model):
|
||||
|
||||
def get_reference(self, code):
|
||||
element = self._get_document(code)
|
||||
if self.env.context.get('raw_reference', False):
|
||||
if self.env.context.get("raw_reference", False):
|
||||
return html_escape(element.display_name)
|
||||
text = """<a href="#" class="oe_direct_line"
|
||||
data-oe-model="%s" data-oe-id="%s" name="%s">%s</a>
|
||||
"""
|
||||
if not element:
|
||||
text = '<i>%s</i>' % text
|
||||
text = "<i>%s</i>" % text
|
||||
res = text % (
|
||||
element._name,
|
||||
element and element.id or '',
|
||||
element and element.id or "",
|
||||
code,
|
||||
html_escape(element.display_name or code),
|
||||
)
|
||||
return res
|
||||
|
||||
def _get_template_variables(self):
|
||||
return {'ref': self.get_reference}
|
||||
return {"ref": self.get_reference}
|
||||
|
||||
def get_content(self):
|
||||
try:
|
||||
@@ -104,8 +104,7 @@ class DocumentPage(models.Model):
|
||||
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)
|
||||
_logger.error("Template from page %s cannot be processed" % self.id)
|
||||
return self.content
|
||||
|
||||
def get_raw_content(self):
|
||||
|
||||
Reference in New Issue
Block a user