[FIX] web: Kanban: record quickcreate: fail handler

When the user quick creates a record in a grouped kanban view, a
name_create RPC is done. This RPC may fail, and there were
several issues with the fail handler.

First, those fail handlers take 2 params: an object describing
the error, and a JQuery event. The event was the first argument
in the handler, so it crashes on event.preventDefault().

Second, when the name_create fails, a FormViewDialog in edit
should be open. Former code opened a SelectCreateDialog.

This commit also ensures that the default values are correctly
given to the form in the dialog, through the context.
This commit is contained in:
Aaron Bohy
2017-04-11 19:44:38 +02:00
parent 6bad524bb3
commit 145e89604d
2 changed files with 90 additions and 28 deletions
@@ -8,9 +8,9 @@ odoo.define('web.KanbanController', function (require) {
*/
var BasicController = require('web.BasicController');
var core = require('web.core');
var Context = require('web.Context');
var form_common = require('web.view_dialogs');
var core = require('web.core');
var view_dialogs = require('web.view_dialogs');
var _t = core._t;
var qweb = core.qweb;
@@ -297,38 +297,37 @@ var KanbanController = BasicController.extend({
_onQuickCreateRecord: function (event) {
var self = this;
var column = event.target;
var data = this.model.get(column.db_id);
var name = event.data.value;
var state = this.model.get(this.handle, {raw: true});
var columnState = this.model.get(column.db_id, {raw: true});
var context = columnState.getContext();
context['default_' + state.groupedBy[0]] = columnState.res_id;
this._rpc(state.model, "name_create")
.args([name])
.withContext(context)
.exec()
.then(add_record)
.fail(function (error, event) {
event.preventDefault();
new view_dialogs.FormViewDialog(self, {
res_model: state.model,
context: _.extend({default_name: name}, context),
title: _t("Create"),
disable_multiple_selection: true,
on_saved: function(record) {
add_record([record.res_id]);
},
}).open();
});
function add_record(records) {
return self.model
.addRecordToGroup(data.id, records[0])
.addRecordToGroup(columnState.id, records[0])
.then(function (db_id) {
column.addRecord(self.model.get(db_id), {position: 'before'});
});
}
data.context['default_' + data.groupedBy[0]] = column.id;
this._rpc(data.model, "name_create")
.args([event.data.value])
.withContext(data.context)
.exec()
.then(
add_record,
function (event) {
event.preventDefault();
var popup = new form_common.SelectCreatePopup(this);
popup.select_element(
data.model,
{
title: _t("Create: "),
initial_view: "form",
disable_multiple_selection: true,
},
[],
{ default_name: event.data.value }
);
popup.on("elements_selected", null, add_record);
}
);
},
/**
* @param {OdooEvent} event
@@ -357,6 +357,69 @@ QUnit.module('Views', {
kanban.destroy();
});
QUnit.test('quick create fail in grouped', function (assert) {
assert.expect(7);
var kanban = createView({
View: KanbanView,
model: 'partner',
data: this.data,
arch: '<kanban class="o_kanban_test" on_create="quick_create">' +
'<field name="product_id"/>' +
'<templates><t t-name="kanban-box">' +
'<div><field name="foo"/></div>' +
'</t></templates>' +
'</kanban>',
archs: {
'partner,false,form': '<form string="Partner">' +
'<field name="product_id"/>' +
'<field name="foo"/>' +
'</form>',
},
groupBy: ['product_id'],
mockRPC: function (route, args) {
if (args.method === 'name_create') {
return $.Deferred().reject({
code: 200,
data: {},
message: 'Odoo server error',
});
}
return this._super.apply(this, arguments);
},
});
kanban.renderButtons();
assert.strictEqual(kanban.$('.o_kanban_group:first .o_kanban_record').length, 2,
"there should be 2 records in first column");
kanban.$buttons.find('.o-kanban-button-new').click(); // Click on 'Create'
assert.ok(kanban.$('.o_kanban_group:first() > div:nth(1)').hasClass('o_kanban_quick_create'),
"clicking on create should open the quick_create in the first column");
kanban.$('.o_kanban_quick_create input')
.val('test')
.trigger($.Event('keypress', {keyCode: $.ui.keyCode.ENTER}));
assert.strictEqual($('.modal .o_form_view.o_form_editable').length, 1,
"a form view dialog should have been opened (in edit)");
assert.strictEqual($('.modal .o_form_field_many2one input').val(), 'hello',
"the correct product_id should already be set");
// specify a name and save
$('.modal .o_form_input[name=foo]').val('test').trigger('input');
$('.modal-footer .btn-primary').click();
assert.strictEqual($('.modal').length, 0, "the modal should be closed");
assert.strictEqual(kanban.$('.o_kanban_group:first .o_kanban_record').length, 3,
"there should be 3 records in first column");
var $firstRecord = kanban.$('.o_kanban_group:first .o_kanban_record:first');
assert.strictEqual($firstRecord.text(), 'test',
"the first record of the first column should be the new one");
kanban.destroy();
});
QUnit.test('many2manytags are correctly fetched and displayed', function (assert) {
assert.expect(3);