[IMP] base: improved import language wizard
- more user-friendly error - proper file format check
This commit is contained in:
committed by
David Monjoie
parent
b11f1ba70d
commit
d61bb94c0f
@@ -1,11 +1,18 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
import os
|
||||
import base64
|
||||
import logging
|
||||
from tempfile import TemporaryFile
|
||||
|
||||
from openerp import tools
|
||||
from openerp.osv import osv, fields
|
||||
from openerp.exceptions import UserError
|
||||
from openerp.tools.translate import _
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class base_language_import(osv.osv_memory):
|
||||
""" Language Import """
|
||||
@@ -16,6 +23,7 @@ class base_language_import(osv.osv_memory):
|
||||
'name': fields.char('Language Name', required=True),
|
||||
'code': fields.char('ISO Code', size=5, help="ISO Language and Country code, e.g. en_US", required=True),
|
||||
'data': fields.binary('File', required=True),
|
||||
'filename': fields.char('File Name', required=True),
|
||||
'overwrite': fields.boolean('Overwrite Existing Terms',
|
||||
help="If you enable this option, existing translations (including custom ones) "
|
||||
"will be overwritten and replaced by those in this file"),
|
||||
@@ -33,11 +41,12 @@ class base_language_import(osv.osv_memory):
|
||||
|
||||
# now we determine the file format
|
||||
fileobj.seek(0)
|
||||
first_line = fileobj.readline().strip().replace('"', '').replace(' ', '')
|
||||
fileformat = first_line.endswith("type,name,res_id,src,value") and 'csv' or 'po'
|
||||
fileobj.seek(0)
|
||||
fileformat = os.path.splitext(this.filename)[-1][1:].lower()
|
||||
|
||||
tools.trans_load_data(cr, fileobj, fileformat, this.code, lang_name=this.name, context=context)
|
||||
except Exception, e:
|
||||
_logger.exception('File unsuccessfully imported, due to format mismatch.')
|
||||
raise UserError(_('File not imported due to format mismatch or a malformed file. (Valid formats are .csv, .po, .pot)\n\nTechnical Details:\n%s') % tools.ustr(e))
|
||||
finally:
|
||||
fileobj.close()
|
||||
return True
|
||||
|
||||
@@ -10,7 +10,8 @@
|
||||
<group>
|
||||
<field name="name" placeholder="e.g. English"/>
|
||||
<field name="code" string="Code" placeholder="e.g. en_US"/>
|
||||
<field name="data"/>
|
||||
<field name="data" filename="filename"/>
|
||||
<field name="filename" invisible="1"/>
|
||||
<field name="overwrite"/>
|
||||
</group>
|
||||
<footer>
|
||||
|
||||
@@ -1028,7 +1028,7 @@ def trans_load_data(cr, fileobj, fileformat, lang, lang_name=None, verbose=True,
|
||||
# and we try to find the corresponding
|
||||
# /path/to/xxx/i18n/xxx.pot file.
|
||||
# (Sometimes we have 'i18n_extra' instead of just 'i18n')
|
||||
addons_module_i18n, _ = os.path.split(fileobj.name)
|
||||
addons_module_i18n, _ignored = os.path.split(fileobj.name)
|
||||
addons_module, i18n_dir = os.path.split(addons_module_i18n)
|
||||
addons, module = os.path.split(addons_module)
|
||||
pot_handle = misc.file_open(os.path.join(
|
||||
@@ -1039,7 +1039,7 @@ def trans_load_data(cr, fileobj, fileformat, lang, lang_name=None, verbose=True,
|
||||
|
||||
else:
|
||||
_logger.info('Bad file format: %s', fileformat)
|
||||
raise Exception(_('Bad file format'))
|
||||
raise Exception(_('Bad file format: %s') % fileformat)
|
||||
|
||||
# Read the POT references, and keep them indexed by source string.
|
||||
class Target(object):
|
||||
@@ -1049,7 +1049,7 @@ def trans_load_data(cr, fileobj, fileformat, lang, lang_name=None, verbose=True,
|
||||
self.comments = None
|
||||
|
||||
pot_targets = defaultdict(Target)
|
||||
for type, name, res_id, src, _, comments in pot_reader:
|
||||
for type, name, res_id, src, _ignored, comments in pot_reader:
|
||||
if type is not None:
|
||||
target = pot_targets[src]
|
||||
target.targets.add((type, name, res_id))
|
||||
|
||||
Reference in New Issue
Block a user