68 lines
3.1 KiB
Python
68 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import os
|
|
import xmlrpc.client
|
|
import json
|
|
|
|
# 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"
|
|
source_website_id = int(os.environ.get("ODOO_WEBSITE_ID") or 1)
|
|
export_folder = "tmp"
|
|
lang_codes = ["de_CH"]
|
|
|
|
# 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_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})
|
|
|
|
# Get translation for every menu and every translated field
|
|
translated_menu_fields = ["name"]
|
|
for field in translated_menu_fields:
|
|
for menu in menus:
|
|
menu[field] = {
|
|
"en": menu[field]
|
|
}
|
|
for lang in lang_codes:
|
|
print(f"Export menu {menu['id']} {lang} translation for field {field}.")
|
|
translated_menu = models.execute_kw(db, uid, password, "website.menu", "read", [[menu["id"]]], {"fields": [field], "context": {"lang": lang}})
|
|
menu[field][lang] = translated_menu[0].get(field)
|
|
|
|
# Save menus to website.menu.json
|
|
filepath = os.path.join(export_folder, 'website.menu.json')
|
|
with open(filepath, 'w') as jsonfile:
|
|
json.dump(menus, jsonfile, indent=4)
|
|
print(f"Saved {len(menus)} menus to {filepath}.")
|
|
|
|
# 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", "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
|
|
filepath = os.path.join(export_folder, 'website.page.json')
|
|
with open(filepath, 'w') as jsonfile:
|
|
json.dump(pages, jsonfile, indent=4)
|
|
print(f"Saved {len(pages)} pages to {filepath}.")
|
|
|
|
# Get all attachments
|
|
attachment_ids = models.execute_kw(db, uid, password, "ir.attachment", "search", [[("mimetype", "=ilike", "image%"),("website_id", "=", source_website_id)]])
|
|
attachment_fields = ["id", "name", "datas", "url", "public", "res_model", "mimetype", "website_id"]
|
|
attachments = models.execute_kw(db, uid, password, "ir.attachment", "read", [attachment_ids], {"fields": attachment_fields})
|
|
|
|
# Save attachments to ir.attachment.json
|
|
filepath = os.path.join(export_folder, 'ir.attachment.json')
|
|
with open(filepath, 'w') as jsonfile:
|
|
json.dump(attachments, jsonfile, indent=4)
|
|
print(f"Saved {len(attachments)} attachments to {filepath}.")
|