From d5259a99465c662a6c249ff6ff4e876fbe59860d Mon Sep 17 00:00:00 2001 From: Janik von Rotz Date: Fri, 13 Jun 2025 09:46:29 +0200 Subject: [PATCH] fix(export-import): improve odoo website data export and import scripts --- bin/export-website-data | 7 ++++--- bin/import-website-data | 39 ++++++++++++++++++++++++++++++++------- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/bin/export-website-data b/bin/export-website-data index 6a0674b9..38a26698 100644 --- a/bin/export-website-data +++ b/bin/export-website-data @@ -9,17 +9,18 @@ url = os.environ.get("ODOO_URL") or "http://localhost:8069" db = os.environ.get("ODOO_DATABASE") or "odoo" username = os.getenv("ODOO_USERNAME") or "admin" password = os.environ.get("ODOO_PASSWORD") or "admin" -source_website_id = os.environ.get("ODOO_WEBSITE_ID") or 1 +source_website_id = int(os.environ.get("ODOO_WEBSITE_ID") or 1) export_folder = "tmp" # Connect to Odoo +print(f"Export website data from {url}/{db}") common = xmlrpc.client.ServerProxy(f"{url}/xmlrpc/2/common") uid = common.authenticate(db, username, password, {}) models = xmlrpc.client.ServerProxy(f"{url}/xmlrpc/2/object", allow_none=True) # Get all website menus print(f"Export menus from website {source_website_id}.") -menu_ids = models.execute_kw(db, uid, password, "website.menu", "search", [[("website_id", "=", source_website_id), ("url","not in", ["/default-main-menu", "/", "#", "/contactus"])]]) +menu_ids = models.execute_kw(db, uid, password, "website.menu", "search", [[("website_id", "=", source_website_id), ("url","not in", ["/default-main-menu", "/", "/contactus"])]]) menu_fields = ["id", "name", "url", "page_id", "parent_id", "sequence", "website_id"] menus = models.execute_kw(db, uid, password, "website.menu", "read", [menu_ids], {"fields": menu_fields}) @@ -32,7 +33,7 @@ with open(filepath, 'w') as jsonfile: # Get all website pages print(f"Export pages from website {source_website_id}.") page_ids = models.execute_kw(db, uid, password, "website.page", "search", [[("website_id", "=", source_website_id)]]) -page_fields = ["id", "name", "url", "website_url", "key", "is_published", "type", "arch", "website_id"] +page_fields = ["id", "name", "url", "website_url", "key", "track", "is_published", "type", "arch", "website_id"] pages = models.execute_kw(db, uid, password, "website.page", "read", [page_ids], {"fields": page_fields}) # Save pages to website.page.json diff --git a/bin/import-website-data b/bin/import-website-data index 57450984..722e6104 100644 --- a/bin/import-website-data +++ b/bin/import-website-data @@ -4,16 +4,18 @@ import os import xmlrpc.client import json import sys +import re # Get environment variables url = os.environ.get("ODOO_URL") or "http://localhost:8069" db = os.environ.get("ODOO_DATABASE") or "odoo" username = os.getenv("ODOO_USERNAME") or "admin" password = os.environ.get("ODOO_PASSWORD") or "admin" -target_website_id = os.environ.get("ODOO_WEBSITE_ID") or 2 +target_website_id = int(os.environ.get("ODOO_WEBSITE_ID") or 1) import_folder = "tmp" # Connect to Odoo +print(f"Import website data to {url}/{db}") common = xmlrpc.client.ServerProxy(f"{url}/xmlrpc/2/common") uid = common.authenticate(db, username, password, {}) models = xmlrpc.client.ServerProxy(f"{url}/xmlrpc/2/object", allow_none=True) @@ -25,7 +27,7 @@ with open(filepath, 'r') as jsonfile: # For each attachment check if already exists for attachment in attachments: - attachment_dict = {'name': attachment['name'], 'datas': attachment['datas'], 'url': attachment['url'],, 'mimetype': attachment['mimetype'], 'website_id': target_website_id} + attachment_dict = {'name': attachment['name'], 'datas': attachment['datas'], 'url': attachment['url'], 'mimetype': attachment['mimetype'], 'website_id': target_website_id} existing_attachment_id = models.execute_kw(db, uid, password, "ir.attachment", "search", [[("name", "=", attachment['name']), ("website_id","=",target_website_id)]]) if existing_attachment_id: # If it exits update the attachment @@ -33,8 +35,8 @@ for attachment in attachments: models.execute_kw(db, uid, password, "ir.attachment", "write", [existing_attachment_id[0], attachment_dict]) else: # Otherwise create the attachment + print(f"Create attachment: {attachment_dict['name']}") attachment_id = models.execute_kw(db, uid, password, "ir.attachment", "create", [attachment_dict]) - print(f"Created attachment: {attachment_id}") # Load all pages from json filepath=os.path.join(import_folder, 'website.page.json') @@ -43,7 +45,7 @@ with open(filepath, 'r') as jsonfile: # For each page check if already exists for page in pages: - page_dict = {'name': page['name'], 'type': page['type'], 'key': page['key'], 'arch_base': page['arch'], 'is_published': page['is_published'], 'url': page['url'], 'website_url': page['website_url'], 'website_id': target_website_id} + page_dict = {'name': page['name'], 'type': page['type'], 'key': page['key'], 'track': page['track'], 'arch_base': page['arch'], 'is_published': page['is_published'], 'url': page['url'], 'website_url': page['website_url'], 'website_id': target_website_id} existing_page_id = models.execute_kw(db, uid, password, "website.page", "search", [[("name", "=", page['name']), ("website_id","=",target_website_id)]]) if existing_page_id: # If it exits update the page @@ -51,8 +53,8 @@ for page in pages: models.execute_kw(db, uid, password, "website.page", "write", [existing_page_id[0], page_dict]) else: # Otherwise create the page + print(f"Create page: {page_dict['name']}") page_id = models.execute_kw(db, uid, password, "website.page", "create", [page_dict]) - print(f"Created page: {page_id}") # Load all menus from json filepath=os.path.join(import_folder, 'website.menu.json') @@ -61,13 +63,36 @@ with open(filepath, 'r') as jsonfile: # For each menu check if already exists for menu in menus: - menu_dict = {'name': menu['name'], 'url': menu['url'],'page_id': menu['page_id'], 'parent_id': menu['parent_id'], 'sequence': menu['sequence'], 'website_id': target_website_id} + menu_dict = {'name': menu['name'], 'url': menu['url'],'page_id': menu['page_id'], 'parent_id': menu['parent_id'] or False, 'sequence': menu['sequence'], 'website_id': target_website_id} existing_menu_id = models.execute_kw(db, uid, password, "website.menu", "search", [[("name", "=", menu['name']), ("website_id","=",target_website_id)]]) + + # Lookup parent menu + if menu_dict['parent_id']: + parent_menu_name = re.sub(r'\[.+\]', '', menu_dict['parent_id'][1]).strip() + print(f"Lookup parent menu: {parent_menu_name}") + parent_menu_id = models.execute_kw(db, uid, password, "website.menu", "search", [[("display_name", "=", parent_menu_name), ("website_id","=",target_website_id)]], {'limit': 1}) + if not parent_menu_id: + print(f"Parent menu '{parent_menu_name}' not found.") + del menu_dict['parent_id'] + else: + menu_dict['parent_id'] = parent_menu_id[0] + + # Lookup page id + if menu_dict['page_id']: + page_name = menu_dict['page_id'][1] + print(f"Lookup page: {page_name}") + page_id = models.execute_kw(db, uid, password, "website.page", "search", [[("name", "=", page_name), ("website_id","=",target_website_id)]], {'limit': 1}) + if not page_id: + print(f"Parent menu '{page_name}' not found.") + del menu_dict['page_id'] + else: + menu_dict['page_id'] = page_id[0] + if existing_menu_id: # If it exits update the menu print(f"Update menu: {existing_menu_id}") models.execute_kw(db, uid, password, "website.menu", "write", [existing_menu_id[0], menu_dict]) else: # Otherwise create the menu + print(f"Create menu: {menu_dict['name']}") menu_id = models.execute_kw(db, uid, password, "website.menu", "create", [menu_dict]) - print(f"Created menu: {menu_id}") \ No newline at end of file