[IMP] html_text: black, isort, prettier
parent
18481b71ac
commit
f5cab2b92f
|
@ -14,12 +14,6 @@
|
|||
"license": "AGPL-3",
|
||||
"application": False,
|
||||
"installable": True,
|
||||
"external_dependencies": {
|
||||
"python": [
|
||||
"lxml",
|
||||
],
|
||||
},
|
||||
"depends": [
|
||||
"base",
|
||||
],
|
||||
"external_dependencies": {"python": ["lxml",],},
|
||||
"depends": ["base",],
|
||||
}
|
||||
|
|
|
@ -3,7 +3,9 @@
|
|||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||
|
||||
import logging
|
||||
|
||||
from lxml import etree, html
|
||||
|
||||
from odoo import api, models
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
@ -13,8 +15,9 @@ class IrFieldsConverter(models.AbstractModel):
|
|||
_inherit = "ir.fields.converter"
|
||||
|
||||
@api.model
|
||||
def text_from_html(self, html_content, max_words=None, max_chars=None,
|
||||
ellipsis=u"…", fail=False):
|
||||
def text_from_html(
|
||||
self, html_content, max_words=None, max_chars=None, ellipsis=u"…", fail=False
|
||||
):
|
||||
"""Extract text from an HTML field in a generator.
|
||||
|
||||
:param str html_content:
|
||||
|
@ -45,8 +48,7 @@ class IrFieldsConverter(models.AbstractModel):
|
|||
if fail:
|
||||
raise
|
||||
else:
|
||||
_logger.exception("Failure parsing this HTML:\n%s",
|
||||
html_content)
|
||||
_logger.exception("Failure parsing this HTML:\n%s", html_content)
|
||||
return ""
|
||||
|
||||
# Get words
|
||||
|
@ -63,7 +65,7 @@ class IrFieldsConverter(models.AbstractModel):
|
|||
# Truncate text
|
||||
suffix = suffix or max_chars and len(text) > 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
|
||||
if suffix:
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
# Copyright 2016-2017 Jairo Llopis <jairo.llopis@tecnativa.com>
|
||||
# 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.tools import mute_logger
|
||||
|
||||
|
||||
class ExtractorCase(TransactionCase):
|
||||
|
@ -27,16 +27,18 @@ class ExtractorCase(TransactionCase):
|
|||
|
||||
self.assertEqual(
|
||||
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.text_from_html(html, 8),
|
||||
u"I'm a title I'm a paragraph ¡Pues yo…")
|
||||
self.text_from_html(html, 8), u"I'm a title I'm a paragraph ¡Pues yo…"
|
||||
)
|
||||
self.assertEqual(
|
||||
self.text_from_html(html, 8, 31),
|
||||
u"I'm a title I'm a paragraph ¡P…")
|
||||
self.text_from_html(html, 8, 31), u"I'm a title I'm a paragraph ¡P…"
|
||||
)
|
||||
self.assertEqual(
|
||||
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")
|
||||
def test_empty_html(self):
|
||||
|
|
Loading…
Reference in New Issue