From 68700e0b205703aab02b79959fdd0341c1442cb5 Mon Sep 17 00:00:00 2001 From: Chill Date: Thu, 17 Aug 2023 12:27:06 +0800 Subject: [PATCH] =?UTF-8?q?fix=20#I7TB8W=20[app=5Fcommon]=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E9=80=9A=E7=94=A8=E7=9A=84=E5=AF=BC=E5=85=A5=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app_common/__manifest__.py | 2 +- app_common/models/app_import.py | 44 +++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 app_common/models/app_import.py diff --git a/app_common/__manifest__.py b/app_common/__manifest__.py index bb840355..1be0a196 100644 --- a/app_common/__manifest__.py +++ b/app_common/__manifest__.py @@ -39,7 +39,7 @@ { 'name': "odooai Odooapp Common Func", - 'version': '16.23.05.09', + 'version': '16.23.08.17', 'author': 'odooai.cn', 'category': 'Base', 'website': 'https://www.odooai.cn', diff --git a/app_common/models/app_import.py b/app_common/models/app_import.py new file mode 100644 index 00000000..47cea556 --- /dev/null +++ b/app_common/models/app_import.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- + +import base64 +import io +import csv +import os.path + +from odoo import api, fields, models, modules, tools, SUPERUSER_ID, _ +from odoo.tools import pycompat + + +def app_quick_import(cr, content_path, sep=None): + if not sep: + sep = '/' + dir_split = content_path.split(sep) + module_name = dir_split[0] + file_name = dir_split[2] + file_path, file_type = os.path.splitext(content_path) + model_name = file_name.replace(file_type, '') + file_path = modules.get_module_resource(module_name, dir_split[1], file_name) + content = open(file_path, 'rb').read() + env = api.Environment(cr, SUPERUSER_ID, {}) + if file_type == '.csv': + file_type = 'text/csv' + elif file_type in ['.xls', '.xlsx']: + file_type = 'application/vnd.ms-excel' + import_wizard = env['base_import.import'].create({ + 'res_model': model_name, + 'file_name': file_name, + 'file_type': file_type, + 'file': content, + }) + preview = import_wizard.parse_preview({ + # 'separator': ',', + 'has_headers': True, + # 'quoting': '"', + }) + result = import_wizard.execute_import( + preview["headers"], + preview["headers"], + preview["options"] + ) + +