[IMP] html_text: black, isort, prettier

pull/2590/head
Víctor Martínez 2021-02-19 10:49:09 +01:00 committed by dsolanki
parent 18481b71ac
commit f5cab2b92f
3 changed files with 21 additions and 23 deletions

View File

@ -8,18 +8,12 @@
"category": "Tools", "category": "Tools",
"website": "https://github.com/OCA/server-tools", "website": "https://github.com/OCA/server-tools",
"author": "Grupo ESOC Ingeniería de Servicios, " "author": "Grupo ESOC Ingeniería de Servicios, "
"Tecnativa, " "Tecnativa, "
"Onestein, " "Onestein, "
"Odoo Community Association (OCA)", "Odoo Community Association (OCA)",
"license": "AGPL-3", "license": "AGPL-3",
"application": False, "application": False,
"installable": True, "installable": True,
"external_dependencies": { "external_dependencies": {"python": ["lxml",],},
"python": [ "depends": ["base",],
"lxml",
],
},
"depends": [
"base",
],
} }

View File

@ -3,7 +3,9 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
import logging import logging
from lxml import etree, html from lxml import etree, html
from odoo import api, models from odoo import api, models
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
@ -13,8 +15,9 @@ class IrFieldsConverter(models.AbstractModel):
_inherit = "ir.fields.converter" _inherit = "ir.fields.converter"
@api.model @api.model
def text_from_html(self, html_content, max_words=None, max_chars=None, def text_from_html(
ellipsis=u"", fail=False): self, html_content, max_words=None, max_chars=None, ellipsis=u"", fail=False
):
"""Extract text from an HTML field in a generator. """Extract text from an HTML field in a generator.
:param str html_content: :param str html_content:
@ -45,8 +48,7 @@ class IrFieldsConverter(models.AbstractModel):
if fail: if fail:
raise raise
else: else:
_logger.exception("Failure parsing this HTML:\n%s", _logger.exception("Failure parsing this HTML:\n%s", html_content)
html_content)
return "" return ""
# Get words # Get words
@ -63,7 +65,7 @@ class IrFieldsConverter(models.AbstractModel):
# Truncate text # Truncate text
suffix = suffix or max_chars and len(text) > max_chars suffix = suffix or max_chars and len(text) > max_chars
if max_chars: if max_chars:
text = text[:max_chars - (len(ellipsis) if suffix else 0)].strip() text = text[: max_chars - (len(ellipsis) if suffix else 0)].strip()
# Append ellipsis if needed # Append ellipsis if needed
if suffix: if suffix:

View File

@ -1,8 +1,8 @@
# Copyright 2016-2017 Jairo Llopis <jairo.llopis@tecnativa.com> # Copyright 2016-2017 Jairo Llopis <jairo.llopis@tecnativa.com>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo.tools import mute_logger
from odoo.tests.common import TransactionCase from odoo.tests.common import TransactionCase
from odoo.tools import mute_logger
class ExtractorCase(TransactionCase): class ExtractorCase(TransactionCase):
@ -27,16 +27,18 @@ class ExtractorCase(TransactionCase):
self.assertEqual( self.assertEqual(
self.text_from_html(html), self.text_from_html(html),
u"I'm a title I'm a paragraph ¡Pues yo soy español!") u"I'm a title I'm a paragraph ¡Pues yo soy español!",
)
self.assertEqual( self.assertEqual(
self.text_from_html(html, 8), self.text_from_html(html, 8), u"I'm a title I'm a paragraph ¡Pues yo…"
u"I'm a title I'm a paragraph ¡Pues yo…") )
self.assertEqual( self.assertEqual(
self.text_from_html(html, 8, 31), self.text_from_html(html, 8, 31), u"I'm a title I'm a paragraph ¡P…"
u"I'm a title I'm a paragraph ¡P…") )
self.assertEqual( self.assertEqual(
self.text_from_html(html, 7, ellipsis=""), self.text_from_html(html, 7, ellipsis=""),
u"I'm a title I'm a paragraph ¡Pues") u"I'm a title I'm a paragraph ¡Pues",
)
@mute_logger("odoo.addons.html_text.models.ir_fields_converter") @mute_logger("odoo.addons.html_text.models.ir_fields_converter")
def test_empty_html(self): def test_empty_html(self):