2012-08-14 12:16:10 +00:00
|
|
|
openerp.base_import = function (instance) {
|
|
|
|
var QWeb = instance.web.qweb;
|
2012-08-14 14:14:56 +00:00
|
|
|
var _t = instance.web._t;
|
2012-08-14 12:16:10 +00:00
|
|
|
var _lt = instance.web._lt;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Safari does not deal well at all with raw JSON data being
|
|
|
|
* returned. As a result, we're going to cheat by using a
|
|
|
|
* pseudo-jsonp: instead of getting JSON data in the iframe, we're
|
|
|
|
* getting a ``script`` tag which consists of a function call and
|
|
|
|
* the returned data (the json dump).
|
|
|
|
*
|
|
|
|
* The function is an auto-generated name bound to ``window``,
|
|
|
|
* which calls back into the callback provided here.
|
|
|
|
*
|
|
|
|
* @param {Object} form the form element (DOM or jQuery) to use in the call
|
|
|
|
* @param {Object} attributes jquery.form attributes object
|
|
|
|
* @param {Function} callback function to call with the returned data
|
|
|
|
*/
|
|
|
|
function jsonp(form, attributes, callback) {
|
|
|
|
attributes = attributes || {};
|
|
|
|
var options = {jsonp: _.uniqueId('import_callback_')};
|
|
|
|
window[options.jsonp] = function () {
|
|
|
|
delete window[options.jsonp];
|
|
|
|
callback.apply(null, arguments);
|
|
|
|
};
|
|
|
|
if ('data' in attributes) {
|
|
|
|
_.extend(attributes.data, options);
|
|
|
|
} else {
|
|
|
|
_.extend(attributes, {data: options});
|
|
|
|
}
|
|
|
|
_.extend(attributes, {
|
|
|
|
dataType: 'script',
|
|
|
|
});
|
|
|
|
$(form).ajaxSubmit(attributes);
|
|
|
|
}
|
|
|
|
|
2012-09-11 06:38:49 +00:00
|
|
|
// if true, the 'Import', 'Export', etc... buttons will be shown
|
|
|
|
instance.web.ListView.prototype.defaults.import_enabled = true;
|
|
|
|
instance.web.ListView.include({
|
|
|
|
on_loaded: function () {
|
|
|
|
var self = this;
|
|
|
|
var add_button = false;
|
|
|
|
if (!this.$buttons) {
|
|
|
|
add_button = true;
|
|
|
|
}
|
|
|
|
this._super.apply(this, arguments);
|
|
|
|
if(add_button) {
|
|
|
|
this.$buttons.on('click', '.oe_list_button_import', function() {
|
2012-10-03 12:11:37 +00:00
|
|
|
self.do_action({
|
|
|
|
type: 'ir.actions.client',
|
|
|
|
tag: 'import',
|
|
|
|
params: {
|
|
|
|
model: self.dataset.model
|
|
|
|
}
|
|
|
|
});
|
2012-09-11 06:38:49 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-10-03 12:11:37 +00:00
|
|
|
instance.web.client_actions.add(
|
|
|
|
'import', 'instance.web.DataImport');
|
|
|
|
instance.web.DataImport = instance.web.Widget.extend({
|
2012-08-14 12:16:10 +00:00
|
|
|
template: 'ImportView',
|
2012-08-30 14:12:26 +00:00
|
|
|
opts: [
|
|
|
|
{name: 'encoding', label: _lt("Encoding:"), value: 'utf-8'},
|
|
|
|
{name: 'separator', label: _lt("Separator:"), value: ','},
|
|
|
|
{name: 'quoting', label: _lt("Quoting:"), value: '"'}
|
|
|
|
],
|
2012-08-14 12:16:10 +00:00
|
|
|
events: {
|
2012-09-26 13:50:13 +00:00
|
|
|
// 'change .oe_import_grid input': 'import_dryrun',
|
2012-08-14 17:23:22 +00:00
|
|
|
'change input.oe_import_file': 'file_update',
|
2012-09-03 09:12:11 +00:00
|
|
|
'change input.oe_import_has_header, .oe_import_options input': 'settings_updated',
|
2012-08-14 17:23:22 +00:00
|
|
|
'click a.oe_import_csv': function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
},
|
|
|
|
'click a.oe_import_export': function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
},
|
2012-08-30 12:52:32 +00:00
|
|
|
'click a.oe_import_toggle': function (e) {
|
2012-08-14 17:23:22 +00:00
|
|
|
e.preventDefault();
|
2012-08-30 12:52:32 +00:00
|
|
|
var $el = $(e.target);
|
|
|
|
($el.next().length
|
|
|
|
? $el.next()
|
|
|
|
: $el.parent().next())
|
|
|
|
.toggle();
|
2012-09-26 16:50:52 +00:00
|
|
|
},
|
|
|
|
'click .oe_import_report a.oe_import_report_count': function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
$(e.target).parent().toggleClass('oe_import_report_showmore');
|
2012-09-27 07:48:45 +00:00
|
|
|
},
|
|
|
|
'click .oe_import_moreinfo_action a': function (e) {
|
|
|
|
e.preventDefault();
|
2012-09-27 09:49:50 +00:00
|
|
|
// #data will parse the attribute on its own, we don't like
|
|
|
|
// that sort of things
|
2012-09-27 07:48:45 +00:00
|
|
|
var action = JSON.parse($(e.target).attr('data-action'));
|
2012-09-27 09:49:50 +00:00
|
|
|
// FIXME: when JS-side clean_action
|
|
|
|
action.views = _(action.views).map(function (view) {
|
|
|
|
var id = view[0], type = view[1];
|
|
|
|
return [
|
|
|
|
id,
|
|
|
|
type !== 'tree' ? type
|
|
|
|
: action.view_type === 'form' ? 'list'
|
|
|
|
: 'tree'
|
|
|
|
];
|
|
|
|
});
|
|
|
|
this.do_action(_.extend(action, {target: 'new'}));
|
2012-10-03 12:11:37 +00:00
|
|
|
},
|
|
|
|
// buttons
|
|
|
|
'click .oe_import_validate': 'import_dryrun',
|
|
|
|
'click .oe_import_import': 'do_import',
|
|
|
|
'click .oe_import_cancel': function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.exit();
|
2012-08-14 17:23:22 +00:00
|
|
|
}
|
2012-08-14 12:16:10 +00:00
|
|
|
},
|
2012-10-03 12:11:37 +00:00
|
|
|
init: function (parent, params) {
|
|
|
|
this._super.apply(this, arguments);
|
|
|
|
this.res_model = params.model;
|
2012-08-14 12:16:10 +00:00
|
|
|
// import object id
|
|
|
|
this.id = null;
|
|
|
|
this.Import = new instance.web.Model('base_import.import');
|
|
|
|
},
|
|
|
|
start: function () {
|
|
|
|
var self = this;
|
2012-10-03 12:11:37 +00:00
|
|
|
|
|
|
|
return $.when(
|
|
|
|
this._super(),
|
|
|
|
this.Import.call('create', [{
|
|
|
|
'res_model': this.res_model
|
|
|
|
}]).then(function (id) {
|
|
|
|
self.id = id;
|
|
|
|
self.$('input[name=import_id]').val(id);
|
|
|
|
})
|
|
|
|
)
|
2012-10-01 10:52:35 +00:00
|
|
|
},
|
2012-08-14 12:16:10 +00:00
|
|
|
|
2012-08-14 14:14:56 +00:00
|
|
|
import_options: function () {
|
2012-08-30 14:12:26 +00:00
|
|
|
var self = this;
|
|
|
|
var options = {
|
|
|
|
headers: this.$('input.oe_import_has_header').prop('checked')
|
2012-08-14 14:14:56 +00:00
|
|
|
};
|
2012-08-30 14:12:26 +00:00
|
|
|
_(this.opts).each(function (opt) {
|
|
|
|
options[opt.name] =
|
|
|
|
self.$('input.oe_import_' + opt.name).val();
|
|
|
|
});
|
|
|
|
return options;
|
2012-08-14 14:14:56 +00:00
|
|
|
},
|
|
|
|
|
2012-08-14 17:23:22 +00:00
|
|
|
//- File & settings change section
|
2012-08-14 12:16:10 +00:00
|
|
|
file_update: function (e) {
|
2012-10-03 12:11:37 +00:00
|
|
|
this.$('.oe_import_button').prop('disabled', true);
|
2012-08-14 12:16:10 +00:00
|
|
|
if (!this.$('input.oe_import_file').val()) { return; }
|
|
|
|
|
2012-08-30 10:32:45 +00:00
|
|
|
this.$el.removeClass('oe_import_preview oe_import_error');
|
|
|
|
jsonp(this.$el, {
|
2012-08-14 12:16:10 +00:00
|
|
|
url: '/base_import/set_file'
|
2012-08-14 17:23:22 +00:00
|
|
|
}, this.proxy('settings_updated'));
|
2012-08-14 12:16:10 +00:00
|
|
|
},
|
2012-08-14 17:23:22 +00:00
|
|
|
settings_updated: function () {
|
2012-08-30 10:32:45 +00:00
|
|
|
this.$el.addClass('oe_import_with_file');
|
2012-08-14 12:16:10 +00:00
|
|
|
// TODO: test that write // succeeded?
|
2012-08-14 14:14:56 +00:00
|
|
|
this.Import.call(
|
|
|
|
'parse_preview', [this.id, this.import_options()])
|
|
|
|
.then(this.proxy('preview'));
|
2012-08-14 12:16:10 +00:00
|
|
|
},
|
|
|
|
preview: function (result) {
|
2012-09-26 13:50:13 +00:00
|
|
|
this.$el.removeClass('oe_import_preview_error oe_import_error');
|
2012-08-30 12:52:32 +00:00
|
|
|
this.$el.toggleClass(
|
|
|
|
'oe_import_noheaders',
|
|
|
|
!this.$('input.oe_import_has_header').prop('checked'));
|
2012-08-14 12:48:35 +00:00
|
|
|
if (result.error) {
|
2012-10-01 14:22:27 +00:00
|
|
|
this.$('.oe_import_options').show();
|
2012-09-26 13:50:13 +00:00
|
|
|
this.$el.addClass('oe_import_preview_error oe_import_error');
|
2012-08-14 12:48:35 +00:00
|
|
|
this.$('.oe_import_error_report').html(
|
2012-10-01 14:22:27 +00:00
|
|
|
QWeb.render('ImportView.preview.error', result))
|
|
|
|
.get(0).scrollIntoView();
|
2012-09-03 09:12:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-10-03 12:11:37 +00:00
|
|
|
this.$('.oe_import_button').prop('disabled', false);
|
2012-09-03 09:12:11 +00:00
|
|
|
this.$el.addClass('oe_import_preview');
|
|
|
|
this.$('table').html(QWeb.render('ImportView.preview', result));
|
|
|
|
|
2012-10-01 14:22:27 +00:00
|
|
|
if (result.headers.length === 1) {
|
|
|
|
this.$('.oe_import_options').show();
|
|
|
|
this.render_import_result([{
|
|
|
|
type: 'warning',
|
|
|
|
message: _t("A single column was found in the file, this often means the file separator is incorrect")
|
|
|
|
}]);
|
|
|
|
}
|
|
|
|
|
2012-09-03 09:12:11 +00:00
|
|
|
var $fields = this.$('.oe_import_fields input');
|
|
|
|
this.render_fields_matches(result, $fields);
|
2012-09-06 10:35:16 +00:00
|
|
|
var data = this.generate_fields_completion(result);
|
|
|
|
var item_finder = function (id, items) {
|
|
|
|
items = items || data;
|
|
|
|
for (var i=0; i < items.length; ++i) {
|
|
|
|
var item = items[i];
|
|
|
|
if (item.id === id) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
var val;
|
|
|
|
if (item.children && (val = item_finder(id, item.children))) {
|
|
|
|
return val;
|
|
|
|
}
|
2012-09-03 11:24:18 +00:00
|
|
|
}
|
2012-09-06 10:35:16 +00:00
|
|
|
return '';
|
2012-09-03 11:24:18 +00:00
|
|
|
};
|
2012-09-06 10:35:16 +00:00
|
|
|
$fields.select2({
|
|
|
|
allowClear: true,
|
|
|
|
minimumInputLength: 0,
|
|
|
|
data: data,
|
|
|
|
initSelection: function (element, callback) {
|
|
|
|
var default_value = element.val();
|
|
|
|
if (!default_value) {
|
|
|
|
callback('');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(item_finder(default_value));
|
|
|
|
},
|
|
|
|
|
|
|
|
width: 'resolve',
|
|
|
|
dropdownCssClass: 'oe_import_selector'
|
|
|
|
});
|
2012-09-26 13:50:13 +00:00
|
|
|
//this.import_dryrun();
|
2012-09-03 09:12:11 +00:00
|
|
|
},
|
|
|
|
generate_fields_completion: function (root) {
|
2012-09-03 11:24:18 +00:00
|
|
|
var basic = [];
|
2012-09-06 10:35:16 +00:00
|
|
|
var regulars = [];
|
|
|
|
var o2m = [];
|
2012-09-03 11:24:18 +00:00
|
|
|
function traverse(field, ancestors, collection) {
|
|
|
|
var subfields = field.fields;
|
2012-09-03 09:12:11 +00:00
|
|
|
var field_path = ancestors.concat(field);
|
|
|
|
var label = _(field_path).pluck('string').join(' / ');
|
2012-09-06 10:35:16 +00:00
|
|
|
var id = _(field_path).pluck('name').join('/');
|
2012-09-03 09:12:11 +00:00
|
|
|
|
2012-09-03 11:24:18 +00:00
|
|
|
// If non-relational, m2o or m2m, collection is regulars
|
|
|
|
if (!collection) {
|
|
|
|
if (field.name === 'id') {
|
|
|
|
collection = basic
|
|
|
|
} else if (_.isEmpty(subfields)
|
|
|
|
|| _.isEqual(_.pluck(subfields, 'name'), ['id', '.id'])) {
|
|
|
|
collection = regulars;
|
|
|
|
} else {
|
|
|
|
collection = o2m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
collection.push({
|
2012-09-06 10:35:16 +00:00
|
|
|
id: id,
|
|
|
|
text: label,
|
2012-09-03 11:24:18 +00:00
|
|
|
required: field.required
|
|
|
|
});
|
|
|
|
|
2012-09-03 09:12:11 +00:00
|
|
|
for(var i=0, end=subfields.length; i<end; ++i) {
|
2012-09-03 11:24:18 +00:00
|
|
|
traverse(subfields[i], field_path, collection);
|
2012-09-03 09:12:11 +00:00
|
|
|
}
|
2012-08-14 12:48:35 +00:00
|
|
|
}
|
2012-09-03 09:12:11 +00:00
|
|
|
_(root.fields).each(function (field) {
|
|
|
|
traverse(field, []);
|
|
|
|
});
|
|
|
|
|
2012-09-10 07:20:32 +00:00
|
|
|
var cmp = function (field1, field2) {
|
|
|
|
return field1.text.localeCompare(field2.text);
|
|
|
|
|
|
|
|
};
|
|
|
|
regulars.sort(cmp);
|
|
|
|
o2m.sort(cmp);
|
2012-09-06 10:35:16 +00:00
|
|
|
return basic.concat([
|
|
|
|
{ text: _t("Normal Fields"), children: regulars },
|
|
|
|
{ text: _t("Relation Fields"), children: o2m }
|
|
|
|
]);
|
2012-09-03 09:12:11 +00:00
|
|
|
},
|
|
|
|
render_fields_matches: function (result, $fields) {
|
|
|
|
if (_(result.matches).isEmpty()) { return; }
|
|
|
|
$fields.each(function (index, input) {
|
|
|
|
var match = result.matches[index];
|
|
|
|
if (!match) { return; }
|
|
|
|
|
|
|
|
var current_field = result;
|
|
|
|
input.value = _(match).chain()
|
|
|
|
.map(function (name) {
|
|
|
|
// WARNING: does both mapping and folding (over the
|
|
|
|
// ``field`` iterator variable)
|
|
|
|
return current_field = _(current_field.fields).find(function (subfield) {
|
|
|
|
return subfield.name === name;
|
|
|
|
});
|
|
|
|
})
|
2012-09-06 10:35:16 +00:00
|
|
|
.pluck('name')
|
2012-09-03 09:12:11 +00:00
|
|
|
.value()
|
2012-09-06 10:35:16 +00:00
|
|
|
.join('/');
|
2012-09-03 09:12:11 +00:00
|
|
|
});
|
2012-08-14 12:16:10 +00:00
|
|
|
},
|
2012-08-14 14:14:56 +00:00
|
|
|
|
|
|
|
//- import itself
|
2012-09-03 14:21:10 +00:00
|
|
|
call_import: function (options) {
|
2012-09-06 10:35:16 +00:00
|
|
|
var fields = this.$('.oe_import_fields input.oe_import_match_field').map(function (index, el) {
|
|
|
|
return $(el).select2('val') || false;
|
2012-08-14 14:14:56 +00:00
|
|
|
}).get();
|
2012-09-03 14:21:10 +00:00
|
|
|
return this.Import.call(
|
|
|
|
'do', [this.id, fields, this.import_options()], options);
|
2012-08-14 14:14:56 +00:00
|
|
|
},
|
2012-09-03 14:21:10 +00:00
|
|
|
import_dryrun: function () {
|
2012-09-26 13:50:13 +00:00
|
|
|
return this.call_import({ dryrun: true })
|
2012-09-26 16:50:52 +00:00
|
|
|
.then(this.proxy('render_import_result'));
|
2012-09-03 14:21:10 +00:00
|
|
|
},
|
|
|
|
do_import: function () {
|
|
|
|
var self = this;
|
2012-09-26 16:50:52 +00:00
|
|
|
return this.call_import({ dryrun: false }).then(function (message) {
|
|
|
|
if (!_.any(message, function (message) {
|
|
|
|
return message.type === 'error' })) {
|
2012-10-03 12:11:37 +00:00
|
|
|
self.exit();
|
2012-09-03 14:21:10 +00:00
|
|
|
return;
|
2012-08-14 14:14:56 +00:00
|
|
|
}
|
2012-09-26 16:50:52 +00:00
|
|
|
self.render_import_result(message);
|
2012-09-03 14:21:10 +00:00
|
|
|
});
|
|
|
|
},
|
2012-10-03 12:11:37 +00:00
|
|
|
exit: function () {
|
|
|
|
this.do_action({
|
|
|
|
type: 'ir.actions.client',
|
|
|
|
tag: 'history_back'
|
|
|
|
});
|
|
|
|
},
|
2012-09-26 16:50:52 +00:00
|
|
|
render_import_result: function (message) {
|
|
|
|
if (_.isEmpty(message)) {
|
2012-10-03 12:11:37 +00:00
|
|
|
this.$('.oe_import_import').addClass('oe_highlight');
|
2012-10-01 14:06:22 +00:00
|
|
|
message.push({
|
|
|
|
type: 'info',
|
|
|
|
message: _t("Everything seems valid.")
|
|
|
|
});
|
2012-08-14 14:14:56 +00:00
|
|
|
}
|
2012-09-26 16:50:52 +00:00
|
|
|
// row indexes come back 0-indexed, spreadsheets
|
|
|
|
// display 1-indexed.
|
|
|
|
var offset = 1;
|
|
|
|
// offset more if header
|
2012-10-01 13:52:17 +00:00
|
|
|
if (this.import_options().headers) { offset += 1; }
|
2012-09-26 16:50:52 +00:00
|
|
|
|
2012-08-30 10:32:45 +00:00
|
|
|
this.$el.addClass('oe_import_error');
|
2012-08-14 14:14:56 +00:00
|
|
|
this.$('.oe_import_error_report').html(
|
2012-09-26 16:50:52 +00:00
|
|
|
QWeb.render('ImportView.error', {
|
|
|
|
errors: _(message).groupBy('message'),
|
|
|
|
at: function (rows) {
|
|
|
|
var from = rows.from + offset;
|
|
|
|
var to = rows.to + offset;
|
|
|
|
if (from === to) {
|
|
|
|
return _.str.sprintf(_t("at row %d"), from);
|
|
|
|
}
|
|
|
|
return _.str.sprintf(_t("between rows %d and %d"),
|
|
|
|
from, to);
|
|
|
|
},
|
|
|
|
more: function (n) {
|
2012-09-27 07:48:45 +00:00
|
|
|
return _.str.sprintf(_t("(%d more)"), n);
|
|
|
|
},
|
|
|
|
info: function (msg) {
|
|
|
|
if (typeof msg === 'string') {
|
|
|
|
return _.str.sprintf(
|
|
|
|
'<div class="oe_import_moreinfo oe_import_moreinfo_message">%s</div>',
|
|
|
|
_.str.escapeHTML(msg));
|
|
|
|
}
|
|
|
|
if (msg instanceof Array) {
|
|
|
|
return _.str.sprintf(
|
|
|
|
'<div class="oe_import_moreinfo oe_import_moreinfo_choices">%s <ul>%s</ul></div>',
|
|
|
|
_.str.escapeHTML(_t("Here are the possible values:")),
|
|
|
|
_(msg).map(function (msg) {
|
|
|
|
return '<li>'
|
|
|
|
+ _.str.escapeHTML(msg)
|
|
|
|
+ '</li>';
|
2012-09-27 09:49:50 +00:00
|
|
|
}).join(''));
|
2012-09-27 07:48:45 +00:00
|
|
|
}
|
|
|
|
// Final should be object, action descriptor
|
|
|
|
return [
|
|
|
|
'<div class="oe_import_moreinfo oe_import_moreinfo_action">',
|
|
|
|
_.str.sprintf('<a href="#" data-action="%s">',
|
|
|
|
_.str.escapeHTML(JSON.stringify(msg))),
|
|
|
|
_.str.escapeHTML(
|
|
|
|
_t("Get all possible values")),
|
|
|
|
'</a>',
|
|
|
|
'</div>'
|
|
|
|
].join('')
|
2012-09-26 16:50:52 +00:00
|
|
|
},
|
2012-10-01 14:22:27 +00:00
|
|
|
})).get(0).scrollIntoView();
|
2012-08-14 14:14:56 +00:00
|
|
|
},
|
2012-08-14 12:16:10 +00:00
|
|
|
});
|
|
|
|
};
|