Skip to content
Snippets Groups Projects
Commit a1ea1b12d41b authored by Vincent Hatakeyama's avatar Vincent Hatakeyama
Browse files

:sparkles: add option to specify language, default to no language

parent 3fbab90723e3
No related branches found
No related tags found
No related merge requests found
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
import hmac import hmac
import logging import logging
import os import os
import re
import sys import sys
from datetime import datetime from datetime import datetime
from typing import List, Tuple from typing import List, Tuple
...@@ -22,5 +23,5 @@ ...@@ -22,5 +23,5 @@
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
__version__ = '1.0.1' __version__ = '1.0.2'
__date__ = '2019-07-29' __date__ = '2019-07-29'
...@@ -26,5 +27,5 @@ ...@@ -26,5 +27,5 @@
__date__ = '2019-07-29' __date__ = '2019-07-29'
__updated__ = '2019-08-01' __updated__ = '2019-08-30'
def import_csv( def import_csv(
...@@ -32,7 +33,7 @@ ...@@ -32,7 +33,7 @@
database: str, database: str,
protocol: str, host: str, port: int, protocol: str, host: str, port: int,
timeout: int, timeout: int,
model_filenames: List[Tuple[str, str]], model_filenames: List[Tuple[str, str, str]],
update_parameter: bool = True, update_parameter: bool = True,
) -> int: ) -> int:
_logger.info( _logger.info(
...@@ -59,7 +60,6 @@ ...@@ -59,7 +60,6 @@
import_obj = o.env["base_import.import"] import_obj = o.env["base_import.import"]
param_obj = o.env["ir.config_parameter"] param_obj = o.env["ir.config_parameter"]
# user_obj = o.env["res.users"]
_logger.info("Getting a CSRF token") _logger.info("Getting a CSRF token")
# Reproduce what odoo.http::csrf_token does to build a CSRF token. # Reproduce what odoo.http::csrf_token does to build a CSRF token.
...@@ -86,9 +86,17 @@ ...@@ -86,9 +86,17 @@
"headers": True, "headers": True,
} }
_logger.info(f"{len(model_filenames)} files to import") _logger.info(f"{len(model_filenames)} files to import")
for model, csv_file in model_filenames: for model, csv_file, lang in model_filenames:
_logger.info('Importing - %s in model %s', csv_file, model) _logger.info(
import_dlg_id = import_obj.create({'res_model': model}) 'Importing - %s in model %s (lang %s)', csv_file, model, lang)
context = {
'lang': lang,
# TODO add tz maybe
}
import_dlg_id = import_obj.create(
{'res_model': model},
context=context,
)
with open(csv_file, 'rb') as f: with open(csv_file, 'rb') as f:
m = MultipartEncoder( m = MultipartEncoder(
fields={ fields={
...@@ -103,5 +111,6 @@ ...@@ -103,5 +111,6 @@
data=data, data=data,
headers={'Content-Type': m.content_type} headers={'Content-Type': m.content_type}
) )
# in Odoo, context contains user lang, but that is ok so far
_logger.debug('response: %s', response) _logger.debug('response: %s', response)
# This returns a lot of stuff we don't need, except for the field list. # This returns a lot of stuff we don't need, except for the field list.
...@@ -106,6 +115,7 @@ ...@@ -106,6 +115,7 @@
_logger.debug('response: %s', response) _logger.debug('response: %s', response)
# This returns a lot of stuff we don't need, except for the field list. # This returns a lot of stuff we don't need, except for the field list.
fields = import_obj.parse_preview(import_dlg_id, import_options)[ fields = import_obj.parse_preview(
"headers"] import_dlg_id, import_options,
)["headers"]
# Got fields; now run the import. # Got fields; now run the import.
errors = list() errors = list()
...@@ -110,6 +120,9 @@ ...@@ -110,6 +120,9 @@
# Got fields; now run the import. # Got fields; now run the import.
errors = list() errors = list()
results = import_obj.do(import_dlg_id, fields, import_options) # this is where setting the context is necessary
results = import_obj.do(
import_dlg_id, fields, import_options, context=context
)
for result in results: for result in results:
if result['type'] == 'error': if result['type'] == 'error':
errors.append(result['message']) errors.append(result['message'])
...@@ -226,7 +239,14 @@ ...@@ -226,7 +239,14 @@
) )
model_filenames = list() model_filenames = list()
# ⚠ assume files are named ".* model_name.csv" or ".* model_name" # files need to be named according to the following regexp
pattern = re.compile(
r'(?P<path>.*/)'
r'(?:\d+\s)?'
r'(?P<model>[\w.]+?)'
r'(?:@(?P<lang>\w{2,3}(?:@latin|_\w{2})))?'
r'(?:.csv)?'
)
file_list = list() file_list = list()
if nmspc.file: if nmspc.file:
file_list.extend(nmspc.file) file_list.extend(nmspc.file)
...@@ -236,7 +256,9 @@ ...@@ -236,7 +256,9 @@
file_list.extend(os.path.join(root, f) for f in sorted(files)) file_list.extend(os.path.join(root, f) for f in sorted(files))
for filename in file_list: for filename in file_list:
_logger.debug(f"{filename} to import") _logger.debug(f"{filename} to import")
model_csv = os.path.basename(filename).split(' ')[-1] result = pattern.fullmatch(filename)
if model_csv[-4:] == '.csv': if result:
model = model_csv[:-4] model = result.group('model')
lang = result.group('lang')
model_filenames.append((model, filename, lang))
else: else:
...@@ -242,6 +264,5 @@ ...@@ -242,6 +264,5 @@
else: else:
model = model_csv _logger.warning('Unrecognized file name "%s", ignored', filename)
model_filenames.append((model, filename))
return import_csv( return import_csv(
login=nmspc.login, login=nmspc.login,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment