[IMP] document_page: Use html_diff for checking differences

This commit is contained in:
Enric Tobella
2025-12-15 09:59:54 +01:00
parent 90a8ed45f6
commit c01b26cb29
12 changed files with 162 additions and 70 deletions

View File

@@ -3,8 +3,16 @@
import difflib
try:
import html_diff
except ImportError:
html_diff = None
import logging
from odoo import api, fields, models
_logger = logging.getLogger(__name__)
class DocumentPageHistory(models.Model):
"""This model is necessary to manage a document history."""
@@ -43,11 +51,16 @@ class DocumentPageHistory(models.Model):
)
rec.diff = self._get_diff(prev.id, rec.id)
@api.model
def _get_diff(self, v1, v2):
"""Return the difference between two version of document version."""
text1 = v1 and self.browse(v1).content or ""
text2 = v2 and self.browse(v2).content or ""
if html_diff:
return html_diff.diff(text1, text2)
# Keeping old logic. to be removed in 19 or 18.abs
_logger.warning(
"_get_diff: using fallback logic, please install 'html_diff' library "
"for better diff rendering"
)
# Include line breaks to make it more readable
# TODO: consider using a beautify library directly on the content
text1 = text1.replace("</p><p>", "</p>\r\n<p>")