[FIX] website, website_blog: remove stars from blog description
Current behavior: When adding a "Heading" in the first 200 characters of a blog with /Heading, the short description of the blog preview would have unwanted stars (*) around the heading Steps to reproduce: - Create a blog article - In the first 200 characters of the blog use a heading (e.g. /Heading1) - Go back to the blog list, the description of the new article contains unwanted stars (*) opw-2798595 closes odoo/odoo#88352 X-original-commit: 6cb57d2f1ffb0adceaa3199cc0bf5140589d9328 Signed-off-by: Romain Derie (rde) <rde@odoo.com>
This commit is contained in:
@@ -366,7 +366,6 @@ class WebsiteSearchableMixin(models.AbstractModel):
|
||||
for result, data in zip(self, results_data):
|
||||
for html_field in html_fields:
|
||||
if data[html_field]:
|
||||
text = text_from_html(data[html_field])
|
||||
text = re.sub('\\s+', ' ', text).strip()
|
||||
text = text_from_html(data[html_field], True)
|
||||
data[html_field] = text
|
||||
return results_data
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
import contextlib
|
||||
import re
|
||||
from lxml import etree
|
||||
from unittest.mock import Mock, MagicMock, patch
|
||||
|
||||
@@ -144,7 +145,7 @@ def similarity_score(s1, s2):
|
||||
score -= len(set1.symmetric_difference(s2)) / (len(s1) + len(s2))
|
||||
return score
|
||||
|
||||
def text_from_html(html_fragment):
|
||||
def text_from_html(html_fragment, collapse_whitespace=False):
|
||||
"""
|
||||
Returns the plain non-tag text from an html
|
||||
|
||||
@@ -154,4 +155,7 @@ def text_from_html(html_fragment):
|
||||
"""
|
||||
# lxml requires one single root element
|
||||
tree = etree.fromstring('<p>%s</p>' % html_fragment, etree.XMLParser(recover=True))
|
||||
return ' '.join(tree.itertext())
|
||||
content = ' '.join(tree.itertext())
|
||||
if collapse_whitespace:
|
||||
content = re.sub('\\s+', ' ', content).strip()
|
||||
return content
|
||||
|
||||
@@ -6,9 +6,9 @@ import random
|
||||
|
||||
from odoo import api, models, fields, _
|
||||
from odoo.addons.http_routing.models.ir_http import slug, unslug
|
||||
from odoo.addons.website.tools import text_from_html
|
||||
from odoo.tools.json import scriptsafe as json_scriptsafe
|
||||
from odoo.tools.translate import html_translate
|
||||
from odoo.tools import html2plaintext
|
||||
|
||||
|
||||
class Blog(models.Model):
|
||||
@@ -197,7 +197,7 @@ class BlogPost(models.Model):
|
||||
if blog_post.teaser_manual:
|
||||
blog_post.teaser = blog_post.teaser_manual
|
||||
else:
|
||||
content = html2plaintext(blog_post.content).replace('\n', ' ')
|
||||
content = text_from_html(blog_post.content, True)
|
||||
blog_post.teaser = content[:200] + '...'
|
||||
|
||||
def _set_teaser(self):
|
||||
|
||||
@@ -108,3 +108,11 @@ class TestWebsiteBlogFlow(TestWebsiteBlogCommon):
|
||||
|
||||
self.assertFalse(self.env['mail.message'].sudo().search(
|
||||
[('model', '=', 'blog.post'), ('attachment_ids', 'in', second_attachment.ids)]))
|
||||
|
||||
def test_website_blog_teaser_content(self):
|
||||
""" Make sure that the content of the post is correctly rendered in
|
||||
proper plain text. """
|
||||
|
||||
self.test_blog_post.content = "<h2>Test Content</h2>"
|
||||
|
||||
self.assertEqual(self.test_blog_post.teaser, "Test Content...")
|
||||
|
||||
Reference in New Issue
Block a user