From 7fd9078fede491efc51a072384dc69df09ff0b8e Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 26 Sep 2011 12:07:44 +0200 Subject: [PATCH] [ADD] basic auto-matching of imported file columns to model fields todo: if exact equality with field name and field string fail, a levenshtein distance test with the field string would be pretty cool (mostly for importing stuff) bzr revid: xmo@openerp.com-20110926100744-1mcucy3njp9fd3r5 --- addons/web/static/src/js/data_import.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/addons/web/static/src/js/data_import.js b/addons/web/static/src/js/data_import.js index cbcaa10d7e4..9c686846751 100644 --- a/addons/web/static/src/js/data_import.js +++ b/addons/web/static/src/js/data_import.js @@ -159,13 +159,15 @@ openerp.web.DataImport = openerp.web.Dialog.extend({ }, on_import_results: function(results) { this.$element.find('#result').empty(); - var result_node = this.$element.find("#result"); + var headers, result_node = this.$element.find("#result"); if (results['records']) { - var lines_to_skip = parseInt(this.$element.find('#csv_skip').val(), 10); - var with_headers = this.$element.find('#file_has_headers').prop('checked'); + var lines_to_skip = parseInt(this.$element.find('#csv_skip').val(), 10), + with_headers = this.$element.find('#file_has_headers').prop('checked'); + headers = with_headers ? results.records[0] : null; + result_node.append(QWeb.render('ImportView.result', { - 'headers': with_headers ? results.records[0] : null, + 'headers': headers, 'records': lines_to_skip ? results.records.slice(lines_to_skip) : with_headers ? results.records.slice(1) : results.records @@ -183,7 +185,7 @@ openerp.web.DataImport = openerp.web.Dialog.extend({ var self = this; this.ready.then(function () { - self.$element.find('.sel_fields').bind('blur', function () { + var $fields = self.$element.find('.sel_fields').bind('blur', function () { if (this.value && !_(self.all_fields).contains(this.value)) { this.value = ''; } @@ -194,6 +196,16 @@ openerp.web.DataImport = openerp.web.Dialog.extend({ }).focus(function () { $(this).autocomplete('search'); }); + // Column auto-detection + _(headers).each(function (header, index) { + var f =_(self.fields).detect(function (field) { + // TODO: levenshtein between header and field.string + return field.name === header || field.string.toLowerCase() === header; + }); + if (f) { + $fields.eq(index).val(f.name); + } + }); self.on_check_field_values(); }); },