156 lines
5.1 KiB
Plaintext
156 lines
5.1 KiB
Plaintext
|
#!/opt/odoo/bin/python
|
||
|
# pylint: disable=print-used
|
||
|
import csv
|
||
|
import json
|
||
|
import os
|
||
|
import requests
|
||
|
from bs4 import BeautifulSoup
|
||
|
from datetime import datetime
|
||
|
from io import StringIO
|
||
|
|
||
|
# Config
|
||
|
csv_export_db = os.getenv('ODOO_CSV_EXPORT_DB', 'odoo')
|
||
|
csv_export_dir = os.getenv('ODOO_CSV_EXPORT_DIR', 'csv')
|
||
|
csv_export_login = os.getenv('ODOO_CSV_EXPORT_USER', 'admin')
|
||
|
csv_export_password = os.getenv('ODOO_CSV_EXPORT_PASSWORD', 'admin')
|
||
|
csv_export_url = os.getenv('ODOO_CSV_EXPORT_URL', 'http://localhost:8069')
|
||
|
|
||
|
PRODUCT_FIELD = [
|
||
|
(".id", "openerp_id#key"),
|
||
|
("code", "default_code"),
|
||
|
"name",
|
||
|
("categ_id/.id", "categ_id|openerp_id"),
|
||
|
"standard_price",
|
||
|
("tag_ids/.id", "tag_ids|openerp_id"),
|
||
|
"type",
|
||
|
("taxes_id/description", "taxes_id|1|name"),
|
||
|
("supplier_taxes_id/description", "supplier_taxes_id|1|name"),
|
||
|
"sale_ok",
|
||
|
"purchase_ok",
|
||
|
("seller_ids/name/.id", "seller_ids|1|name|openerp_id"),
|
||
|
("seller_ids/pricelist_ids/price", "seller_ids|1|price"),
|
||
|
"lst_price",
|
||
|
]
|
||
|
|
||
|
TODO = [
|
||
|
{
|
||
|
"filename": "product.tag.csv",
|
||
|
"model": "product.tag",
|
||
|
"fields": [
|
||
|
(".id", "openerp_id#key"),
|
||
|
"name",
|
||
|
],
|
||
|
},
|
||
|
{
|
||
|
"filename": "product.category.csv",
|
||
|
"model": "product.category",
|
||
|
"fields": [
|
||
|
(".id", "openerp_id#key"),
|
||
|
"name",
|
||
|
("parent_id/.id", "parent_id|openerp_id"),
|
||
|
],
|
||
|
"domain": [("name", "!=", "")],
|
||
|
},
|
||
|
{
|
||
|
"filename": "res.partner.csv",
|
||
|
"model": "res.partner",
|
||
|
"fields": [
|
||
|
(".id", "openerp_id#key"),
|
||
|
"name",
|
||
|
("parent_id/.id", "parent_id|openerp_id"),
|
||
|
"is_company",
|
||
|
("customer", "customer_rank"),
|
||
|
("supplier", "supplier_rank"),
|
||
|
"type",
|
||
|
"street",
|
||
|
"street2",
|
||
|
"zip",
|
||
|
"city",
|
||
|
("country_id/code", "country_id|code"),
|
||
|
"website",
|
||
|
"function",
|
||
|
"phone",
|
||
|
"mobile",
|
||
|
"email",
|
||
|
"lang",
|
||
|
"fax",
|
||
|
],
|
||
|
"domain": [("supplier", "=", True)],
|
||
|
},
|
||
|
{
|
||
|
"filename": "product.product.csv",
|
||
|
"model": "product.product",
|
||
|
"fields": PRODUCT_FIELD,
|
||
|
"domain": [("name", "!=", "")],
|
||
|
},
|
||
|
]
|
||
|
|
||
|
def main():
|
||
|
print(csv_export_url)
|
||
|
print(csv_export_db)
|
||
|
print(csv_export_login)
|
||
|
session = requests.Session()
|
||
|
response = session.get(
|
||
|
"%s/web/login?db=%s" % (csv_export_url,csv_export_db),
|
||
|
)
|
||
|
print(response.status_code)
|
||
|
print(response.headers)
|
||
|
soup = BeautifulSoup(response.text, 'lxml')
|
||
|
csrf_token = soup.select_one('input[name="csrf_token"]')['value']
|
||
|
response = session.post(
|
||
|
"%s/web/login" % csv_export_url,
|
||
|
params={"login": csv_export_login, "password": csv_export_password, "csrf_token": csrf_token},
|
||
|
)
|
||
|
if response.status_code == 200:
|
||
|
if "Wrong login/password" in response.text:
|
||
|
raise Exception("ERROR: login failed due to wrong login/password, please check ODOO_CSV_EXPORT_USER and ODOO_CSV_EXPORT_PASSWORD environment variables")
|
||
|
else:
|
||
|
raise Exception("ERROR: unable to connect to odoo, did you set ODOO_CSV_EXPORT_URL, ODOO_CSV_EXPORT_USER and ODOO_CSV_EXPORT_PASSWORD environment variables ?")
|
||
|
|
||
|
for todo in TODO:
|
||
|
start = datetime.now()
|
||
|
fields = []
|
||
|
for field in todo["fields"]:
|
||
|
if isinstance(field, tuple):
|
||
|
fields.append({"name": field[0], "label": field[1]})
|
||
|
else:
|
||
|
fields.append({"name": field, "label": field})
|
||
|
response = session.get(
|
||
|
"%s/web/export/csv?data=foo&token=bar" % csv_export_url,
|
||
|
)
|
||
|
if response.status_code == 404:
|
||
|
raise Exception("ERROR: unable to find export csv route, please install pattern-import-export addon")
|
||
|
print (response.text)
|
||
|
soup = BeautifulSoup(response.text, 'lxml')
|
||
|
csrf_token = soup.find('input',attrs = {'name':'csrf_token'})['value']
|
||
|
print (csrf_token)
|
||
|
response = session.post(
|
||
|
"%s/web/export/csv" % csv_export_url,
|
||
|
params={
|
||
|
"data": json.dumps(
|
||
|
{
|
||
|
"model": todo["model"],
|
||
|
"fields": fields,
|
||
|
"domain": todo.get("domain", []),
|
||
|
"ids": False,
|
||
|
"import_compat": False,
|
||
|
"context": {"lang": "fr_FR"},
|
||
|
}
|
||
|
),
|
||
|
"token": "foobar",
|
||
|
"csrf_token": csrf_token,
|
||
|
},
|
||
|
)
|
||
|
if response.status_code == 500:
|
||
|
raise Exception("ERROR: unable to retrieve csv, please check your import/export patterns")
|
||
|
text = response.text
|
||
|
|
||
|
with open("%s/%s" % (csv_export_dir, todo["filename"]), "w") as output:
|
||
|
if todo["filename"] == "res.partner.csv":
|
||
|
text = text.replace('"True"', "1").replace('"False"', "0")
|
||
|
output.write(text)
|
||
|
print("File %s/%s exported in %s s" % (csv_export_dir, todo["filename"], (datetime.now() - start).seconds))
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|