From 232b00e7197f0360e1e365865387d89dc4ecff73 Mon Sep 17 00:00:00 2001 From: KaySar12 Date: Mon, 10 Mar 2025 14:44:05 +0700 Subject: [PATCH] add replace chatter script --- odoo/upgrade_code/17.5-03-replace-chatter.py | 55 ++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 odoo/upgrade_code/17.5-03-replace-chatter.py diff --git a/odoo/upgrade_code/17.5-03-replace-chatter.py b/odoo/upgrade_code/17.5-03-replace-chatter.py new file mode 100644 index 000000000..b225bc1f8 --- /dev/null +++ b/odoo/upgrade_code/17.5-03-replace-chatter.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- + +import re +from bs4 import BeautifulSoup as bs +from pathlib import Path +import logging + +# Set up logging +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + + +def upgrade(file_manager): + """Replace all '
' sections with '' in XML files.""" + # Filter files to only XML files + files = [file for file in file_manager if file.path.suffix == ".xml"] + if not files: + logger.info("No XML files found to process") + return + + # Regex pattern to match
and its content + chatter_re = re.compile( + r""" + # Opening div with class="oe_chatter" + .*? # Any content (non-greedy) +
# Closing div + """, + re.VERBOSE | re.DOTALL, + ) # DOTALL allows . to match newlines + + # Process each file + for fileno, file in enumerate(files, start=1): + content = file.content + + # Check if the pattern exists in the file + if not chatter_re.search(content): + continue + + # Use BeautifulSoup for precise replacement + soup = bs(content, "xml") + chatter_divs = soup.select("div.oe_chatter") + + if not chatter_divs: + logger.debug(f"No
found in {file.path}") + continue + + # Replace each
with + for div in chatter_divs: + new_tag = soup.new_tag("chatter") + div.replace_with(new_tag) + + # Update file content + file.content = str(soup) + logger.info(f"Replaced chatter in {file.path}") + file_manager.print_progress(fileno, len(files))