[IMP] add encoding csv option
and generify handling of options a bit, imprive styling, and rename 'quote' to 'quoting' bzr revid: xmo@openerp.com-20120830141226-exli9s1hh4vij6sd
This commit is contained in:
parent
302cabfccb
commit
cdb6869809
|
@ -124,10 +124,10 @@ class ir_import(orm.TransientModel):
|
|||
"""
|
||||
csv_iterator = csv.reader(
|
||||
StringIO(record.file),
|
||||
quotechar=options['quote'],
|
||||
quotechar=options['quoting'],
|
||||
delimiter=options['separator'])
|
||||
csv_nonempty = itertools.ifilter(None, csv_iterator)
|
||||
# TODO: guess encoding?
|
||||
# TODO: guess encoding with chardet? Or https://github.com/aadsm/jschardet
|
||||
encoding = options.get('encoding', 'utf-8')
|
||||
return itertools.imap(
|
||||
lambda row: [item.decode(encoding) for item in row],
|
||||
|
@ -206,7 +206,7 @@ class ir_import(orm.TransientModel):
|
|||
:param id: identifier of the import
|
||||
:param int count: number of preview lines to generate
|
||||
:param options: format-specific options.
|
||||
CSV: {encoding, quote, separator, headers}
|
||||
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}
|
||||
|
|
|
@ -33,3 +33,13 @@
|
|||
.oe_import a.oe_import_toggle:before {
|
||||
content: '> '
|
||||
}
|
||||
|
||||
.oe_import .oe_import_options p {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
.oe_import .oe_import_options label {
|
||||
display: inline-block;
|
||||
width: 10em;
|
||||
text-align: right;
|
||||
}
|
||||
|
|
|
@ -38,10 +38,11 @@ openerp.base_import = function (instance) {
|
|||
instance.web.DataImport = instance.web.Dialog.extend({
|
||||
template: 'ImportView',
|
||||
dialog_title: _lt("Import Data"),
|
||||
defaults: {
|
||||
quoting: '"',
|
||||
separator: ',',
|
||||
},
|
||||
opts: [
|
||||
{name: 'encoding', label: _lt("Encoding:"), value: 'utf-8'},
|
||||
{name: 'separator', label: _lt("Separator:"), value: ','},
|
||||
{name: 'quoting', label: _lt("Quoting:"), value: '"'}
|
||||
],
|
||||
events: {
|
||||
'change input.oe_import_file': 'file_update',
|
||||
'change input:not(.oe_import_file)': 'settings_updated',
|
||||
|
@ -85,11 +86,15 @@ openerp.base_import = function (instance) {
|
|||
},
|
||||
|
||||
import_options: function () {
|
||||
return {
|
||||
quote: this.$('input.oe_import_quoting').val(),
|
||||
separator: this.$('input.oe_import_separator').val(),
|
||||
headers: this.$('input.oe_import_has_header').prop('checked'),
|
||||
var self = this;
|
||||
var options = {
|
||||
headers: this.$('input.oe_import_has_header').prop('checked')
|
||||
};
|
||||
_(this.opts).each(function (opt) {
|
||||
options[opt.name] =
|
||||
self.$('input.oe_import_' + opt.name).val();
|
||||
});
|
||||
return options;
|
||||
},
|
||||
|
||||
//- File & settings change section
|
||||
|
|
|
@ -29,15 +29,15 @@
|
|||
<table class="oe_import_grid" width="100%"/>
|
||||
<a href="#" class="oe_import_toggle">
|
||||
File Format Options…</a>
|
||||
<div class="oe_import_toggled">
|
||||
<!-- no @name, avoid submission when file_update called -->
|
||||
<label t-attf-for="sep_#{_id}">Separator:</label>
|
||||
<input t-attf-id="sep_#{_id}" class="oe_import_separator"
|
||||
t-att-value="widget.defaults.separator"/>
|
||||
<br/>
|
||||
<label t-attf-for="quoting_#{_id}">Quoting:</label>
|
||||
<input t-attf-id="quoting_#{_id}" class="oe_import_quoting"
|
||||
t-att-value="widget.defaults.quoting"/>
|
||||
<div class="oe_import_toggled oe_import_options">
|
||||
<p t-foreach="widget.opts" t-as="option">
|
||||
<!-- no @name, avoid submission when file_update called -->
|
||||
<label t-attf-for="#{option.name}_#{_id}">
|
||||
<t t-esc="option.label"/></label>
|
||||
<input t-attf-id="#{option.name}_#{_id}"
|
||||
t-attf-class="oe_import_#{option.name}"
|
||||
t-att-value="option.value"/>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<h2>Frequently Asked Questions</h2>
|
||||
|
|
|
@ -179,7 +179,7 @@ class test_preview(TransactionCase):
|
|||
def test_encoding(self):
|
||||
Import, id = self.make_import()
|
||||
result = Import.parse_preview(self.cr, self.uid, id, {
|
||||
'quote': '"',
|
||||
'quoting': '"',
|
||||
'separator': ',',
|
||||
})
|
||||
self.assertTrue('error' in result)
|
||||
|
@ -188,7 +188,7 @@ class test_preview(TransactionCase):
|
|||
Import, id = self.make_import()
|
||||
|
||||
result = Import.parse_preview(self.cr, self.uid, id, {
|
||||
'quote': 'foo',
|
||||
'quoting': 'foo',
|
||||
'separator': ',',
|
||||
'encoding': 'euc_kr',
|
||||
})
|
||||
|
@ -198,7 +198,7 @@ class test_preview(TransactionCase):
|
|||
Import, id = self.make_import()
|
||||
|
||||
result = Import.parse_preview(self.cr, self.uid, id, {
|
||||
'quote': '"',
|
||||
'quoting': '"',
|
||||
'separator': 'bob',
|
||||
'encoding': 'euc_kr',
|
||||
})
|
||||
|
@ -215,7 +215,7 @@ class test_preview(TransactionCase):
|
|||
})
|
||||
|
||||
result = Import.parse_preview(self.cr, self.uid, id, {
|
||||
'quote': '"',
|
||||
'quoting': '"',
|
||||
'separator': ',',
|
||||
'headers': True,
|
||||
})
|
||||
|
@ -253,7 +253,7 @@ class test_convert_import_data(TransactionCase):
|
|||
record = Import.browse(self.cr, self.uid, id)
|
||||
data, fields = Import._convert_import_data(
|
||||
record, ['name', 'somevalue', 'othervalue'],
|
||||
{'quote': '"', 'separator': ',', 'headers': True,})
|
||||
{'quoting': '"', 'separator': ',', 'headers': True,})
|
||||
|
||||
self.assertItemsEqual(fields, ['name', 'somevalue', 'othervalue'])
|
||||
self.assertItemsEqual(data, [
|
||||
|
@ -277,7 +277,7 @@ class test_convert_import_data(TransactionCase):
|
|||
record = Import.browse(self.cr, self.uid, id)
|
||||
data, fields = Import._convert_import_data(
|
||||
record, ['name', False, 'othervalue'],
|
||||
{'quote': '"', 'separator': ',', 'headers': True,})
|
||||
{'quoting': '"', 'separator': ',', 'headers': True,})
|
||||
|
||||
self.assertItemsEqual(fields, ['name', 'othervalue'])
|
||||
self.assertItemsEqual(data, [
|
||||
|
@ -301,7 +301,7 @@ class test_convert_import_data(TransactionCase):
|
|||
record = Import.browse(self.cr, self.uid, id)
|
||||
data, fields = Import._convert_import_data(
|
||||
record, ['name', False, 'othervalue'],
|
||||
{'quote': '"', 'separator': ',', 'headers': True,})
|
||||
{'quoting': '"', 'separator': ',', 'headers': True,})
|
||||
|
||||
self.assertItemsEqual(fields, ['name', 'othervalue'])
|
||||
self.assertItemsEqual(data, [
|
||||
|
@ -323,7 +323,7 @@ class test_convert_import_data(TransactionCase):
|
|||
ValueError,
|
||||
Import._convert_import_data,
|
||||
record, [],
|
||||
{'quote': '"', 'separator': ',', 'headers': True,})
|
||||
{'quoting': '"', 'separator': ',', 'headers': True,})
|
||||
|
||||
def test_falsefields(self):
|
||||
Import = self.registry('base_import.import')
|
||||
|
@ -339,4 +339,4 @@ class test_convert_import_data(TransactionCase):
|
|||
ValueError,
|
||||
Import._convert_import_data,
|
||||
record, [False, False, False],
|
||||
{'quote': '"', 'separator': ',', 'headers': True,})
|
||||
{'quoting': '"', 'separator': ',', 'headers': True,})
|
||||
|
|
Loading…
Reference in New Issue