add replace chatter script
This commit is contained in:
parent
dd595904a2
commit
232b00e719
55
odoo/upgrade_code/17.5-03-replace-chatter.py
Normal file
55
odoo/upgrade_code/17.5-03-replace-chatter.py
Normal file
@ -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 '<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))
|
Loading…
Reference in New Issue
Block a user