修复数据导入模块,字段显示不能翻译的问题

Odoo 数据导入BUG 修正


 


通过提交issue ,官方已经修复了此BUG。对应链接 : github 
Odoo 原生自带的数据导入模块是一个非常优秀的模块。特别对于实施顾问初期导入数据有非常的帮助。 但是数据对应的字段始终都是显示为英文,为一般用户的使用带来特别大的困难。 
如下图,相信很多用户看了都头疼,都是英文的。
但是,如果是这样的呢?

我相信很多人都会知道怎么处理了。
为什么上下的图不一致呢。原因在于Odoo 代码存在的bug.
我们来跟踪一下代码:
fields.py, 742行
def _description_string(self, env):
if self.string and env.lang:
model_name = self.base_field.model_name
field_string = env['ir.translation'].get_field_string(model_name)
return field_string.get(self.name) or self.string
return self.string
从上面代码可以看出, 系统取字段string ,会根据当前的语言,获取翻译值。可惜, 在对代码的跟踪过程中,发现导入文件选择后, env.lang 默认奇妙的成为了空值。
实际代码发生在base_import.py, 435 行:
@api.multi
def parse_preview(self, options, count=10):
""" Generates a preview of the uploaded files, and performs
fields-matching between the import's file data and the model's
columns.

If the headers are not requested (not options.headers),
``matches`` and ``headers`` are both ``False``.

:param int count: number of preview lines to generate
:param options: format-specific options.
CSV: {encoding, quoting, separator, headers}
:type options: {str, str, str, bool}
:returns: {fields, matches, headers, preview} | {error, preview}
:rtype: {dict(str: dict(...)), dict(int, list(str)), list(str), list(list(str))} | {str, str}
"""
self.ensure_one()
fields = self.get_fields(self.res_model)
try:
rows = self._read_file(options)
headers, matches = self._match_headers(rows, fields, options)
# Match should have consumed the first row (iif headers), get
# the ``count`` next rows for preview
preview = list(itertools.islice(rows, count))
assert preview, "CSV file seems to have no content"
header_types = self._find_type_from_preview(options, preview)
if options.get('keep_matches', False) and len(options.get('fields', [])):
matches = {}
for index, match in enumerate(options.get('fields')):
if match:
matches[index] = match.split('/')

return {
'fields': fields,
'matches': matches or False,
'headers': headers or False,
'headers_type': header_types or False,
'preview': preview,
'options': options,
'advanced_mode': any([len(models.fix_import_export_id_paths(col)) > 1 for col in headers or []]),
'debug': self.user_has_groups('base.group_no_one'),
}
except Exception as error:
# Due to lazy generators, UnicodeDecodeError (for
# instance) may only be raised when serializing the
# preview to a list in the return.
_logger.debug("Error during parsing preview", exc_info=True)
preview = None
if self.file_type == 'text/csv':
preview = self.file[:ERROR_PREVIEW_BYTES].decode('iso-8859-1')
return {
'error': str(error),
# iso-8859-1 ensures decoding will always succeed,
# even if it yields non-printable characters. This is
# in case of UnicodeDecodeError (or csv.Error
# compounded with UnicodeDecodeError)
'preview': preview,
}
实在是找不到为啥会丢失了。只能在这里重新获取了一下值,进行修正(红色部分)
@api.multi
def parse_preview(self, options, count=10):
""" Generates a preview of the uploaded files, and performs
fields-matching between the import's file data and the model's
columns.

If the headers are not requested (not options.headers),
``matches`` and ``headers`` are both ``False``.

:param int count: number of preview lines to generate
:param options: format-specific options.
CSV: {encoding, quoting, separator, headers}
:type options: {str, str, str, bool}
:returns: {fields, matches, headers, preview} | {error, preview}
:rtype: {dict(str: dict(...)), dict(int, list(str)), list(str), list(list(str))} | {str, str}
"""
from odoo.http import request
self.env=request.env

self.ensure_one()
fields = self.get_fields(self.res_model)
try:
rows = self._read_file(options)
headers, matches = self._match_headers(rows, fields, options)
# Match should have consumed the first row (iif headers), get
# the ``count`` next rows for preview
preview = list(itertools.islice(rows, count))
assert preview, "CSV file seems to have no content"
header_types = self._find_type_from_preview(options, preview)
if options.get('keep_matches', False) and len(options.get('fields', [])):
matches = {}
for index, match in enumerate(options.get('fields')):
if match:
matches[index] = match.split('/')

return {
'fields': fields,
'matches': matches or False,
'headers': headers or False,
'headers_type': header_types or False,
'preview': preview,
'options': options,
'advanced_mode': any([len(models.fix_import_export_id_paths(col)) > 1 for col in headers or []]),
'debug': self.user_has_groups('base.group_no_one'),
}
except Exception as error:
# Due to lazy generators, UnicodeDecodeError (for
# instance) may only be raised when serializing the
# preview to a list in the return.
_logger.debug("Error during parsing preview", exc_info=True)
preview = None
if self.file_type == 'text/csv':
preview = self.file[:ERROR_PREVIEW_BYTES].decode('iso-8859-1')
return {
'error': str(error),
# iso-8859-1 ensures decoding will always succeed,
# even if it yields non-printable characters. This is
# in case of UnicodeDecodeError (or csv.Error
# compounded with UnicodeDecodeError)
'preview': preview,
}
欢迎大家讨论,修正。
版权所有, 转载,请著名出处。