Odoo18-Base/odoo/upgrade_code/17.5-03-replace-chatter.py
KaySar12 232b00e719
All checks were successful
Setup Native Action / native (3.12.7) (push) Has been skipped
Setup Native Action / docker (3.12.7) (push) Has been skipped
add replace chatter script
2025-03-10 14:44:05 +07:00

56 lines
1.7 KiB
Python

# -*- 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 '<div class="oe_chatter">' sections with '<chatter/>' 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 <div class="oe_chatter"> and its content
chatter_re = re.compile(
r"""
<div\ class="oe_chatter"> # Opening div with class="oe_chatter"
.*? # Any content (non-greedy)
</div> # 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 <div class='oe_chatter'> found in {file.path}")
continue
# Replace each <div class="oe_chatter"> with <chatter/>
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))