1 openerp.base_import = function (instance) {
2 var QWeb = instance.web.qweb;
3 var _t = instance.web._t;
4 var _lt = instance.web._lt;
7 * Safari does not deal well at all with raw JSON data being
8 * returned. As a result, we're going to cheat by using a
9 * pseudo-jsonp: instead of getting JSON data in the iframe, we're
10 * getting a ``script`` tag which consists of a function call and
11 * the returned data (the json dump).
13 * The function is an auto-generated name bound to ``window``,
14 * which calls back into the callback provided here.
16 * @param {Object} form the form element (DOM or jQuery) to use in the call
17 * @param {Object} attributes jquery.form attributes object
18 * @param {Function} callback function to call with the returned data
20 function jsonp(form, attributes, callback) {
21 attributes = attributes || {};
22 var options = {jsonp: _.uniqueId('import_callback_')};
23 window[options.jsonp] = function () {
24 delete window[options.jsonp];
25 callback.apply(null, arguments);
27 if ('data' in attributes) {
28 _.extend(attributes.data, options);
30 _.extend(attributes, {data: options});
32 _.extend(attributes, {
35 $(form).ajaxSubmit(attributes);
38 instance.web.DataImport = instance.web.Dialog.extend({
39 template: 'ImportView',
40 dialog_title: _lt("Import Data"),
42 'change input.oe_import_file': 'file_update',
43 'click input.oe_import_has_header': function (e) {
44 this.$element.toggleClass(
45 'oe_import_noheaders', !e.target.checked);
46 this.settings_updated();
48 'click a.oe_import_csv': function (e) {
51 'click a.oe_import_export': function (e) {
54 'click dt a': function (e) {
56 $(e.target).parent().next().toggle();
59 init: function (parent, dataset) {
63 {text: _t("Import File"), click: function () {
65 }, 'class': 'oe_import_dialog_button'}
68 this.res_model = parent.model;
71 this.Import = new instance.web.Model('base_import.import');
75 return this.Import.call('create', [{
76 'res_model': this.res_model
77 }]).then(function (id) {
79 self.$('input[name=import_id]').val(id);
83 import_options: function () {
85 // TODO: customizable gangnam style
88 headers: this.$('input.oe_import_has_header').prop('checked'),
92 //- File & settings change section
93 file_update: function (e) {
94 if (!this.$('input.oe_import_file').val()) { return; }
96 this.$element.removeClass('oe_import_preview oe_import_error');
97 jsonp(this.$element, {
98 url: '/base_import/set_file'
99 }, this.proxy('settings_updated'));
101 settings_updated: function () {
102 this.$element.addClass('oe_import_with_file');
103 // TODO: test that write // succeeded?
105 'parse_preview', [this.id, this.import_options()])
106 .then(this.proxy('preview'));
108 preview: function (result) {
110 this.$element.addClass('oe_import_error');
111 this.$('.oe_import_error_report').html(
112 QWeb.render('ImportView.preview.error', result));
114 this.$element.addClass('oe_import_preview');
115 this.$('table').html(
116 QWeb.render('ImportView.preview', result));
121 do_import: function () {
122 var fields = this.$('.oe_import_fields input').map(function (index, el) {
123 return el.value || false;
126 'do', [this.id, fields, this.import_options()], {
127 // maybe could do a dryrun after successful
128 // preview or something (note: don't go to
129 // this.result if dryrun=true)
132 .then(this.proxy('result'));
134 result: function (errors) {
135 if (!errors.length) {
136 if (this.getParent().reload_content) {
137 this.getParent().reload_content();
142 // import failed (or maybe just warnings, if we ever get
144 this.$element.addClass('oe_import_error');
145 this.$('.oe_import_error_report').html(
146 QWeb.render('ImportView.error', {errors: errors}));