[FIX] calendar: can create an event with start_date as readonly

Before this fix, if the start field is readonly, the user could not create
an event. With this commit, it is also ensured that the context sends the
right default values to the view
This commit is contained in:
Christophe Matthieu
2017-06-06 16:17:30 +02:00
parent ab0bd7be25
commit 1b07daf2ed
4 changed files with 80 additions and 4 deletions
@@ -170,12 +170,12 @@ var CalendarController = AbstractController.extend({
var context = _.extend({}, this.context, event.options && event.options.context);
context.default_name = data.name || null;
context['default_' + this.mapping.date_start] = data.start || null;
context['default_' + this.mapping.date_start] = data[this.mapping.date_start] || null;
if (this.mapping.date_stop) {
context['default_' + this.mapping.date_stop] = data.stop || null;
context['default_' + this.mapping.date_stop] = data[this.mapping.date_stop] || null;
}
if (this.mapping.date_delay) {
context['default_' + this.mapping.date_delay] = data.duration || null;
context['default_' + this.mapping.date_delay] = data[this.mapping.date_delay] || null;
}
if (this.mapping.all_day) {
context['default_' + this.mapping.all_day] = data[this.mapping.all_day] || null;
@@ -181,7 +181,7 @@ return AbstractModel.extend({
// Check whether the date field is editable (i.e. if the events can be
// dragged and dropped)
this.editable = params.editable;
this.creatable = this.editable;
this.creatable = params.creatable;
// display more button when there are too much event on one day
this.eventLimit = params.eventLimit;
@@ -125,6 +125,7 @@ var CalendarView = AbstractView.extend({
this.loadParams.fields = fields;
this.loadParams.fieldsInfo = viewInfo.fieldsInfo;
this.loadParams.editable = !fields[mapping.date_start].readonly;
this.loadParams.creatable = true;
this.loadParams.eventLimit = eventLimit;
this.loadParams.fieldColor = attrs.color;
@@ -770,6 +770,81 @@ QUnit.module('Views', {
assert.strictEqual($('#ui-datepicker-div:empty').length, 0, "should have a clean body");
});
QUnit.test('readonly date_start field', function (assert) {
assert.expect(4);
this.data.event.fields.start.readonly = true;
var calendar = createView({
View: CalendarView,
model: 'event',
data: this.data,
arch:
'<calendar class="o_calendar_test" '+
'string="Events" ' +
'date_start="start" '+
'date_stop="stop" '+
'all_day="allday" '+
'mode="month" '+
'readonly_form_view_id="1">'+
'<field name="name"/>'+
'</calendar>',
archs: archs,
viewOptions: {
initialDate: initialDate,
},
});
assert.strictEqual(calendar.$('.fc-resizer').length, 0, "should not have resize button");
// click on an existing event to open the form view
testUtils.intercept(calendar, 'do_action', function (event) {
assert.deepEqual(event.data.action,
{
type: "ir.actions.act_window",
res_id: 4,
res_model: "event",
views: [[false, "form"]],
target: "current",
context: {}
},
"should open the form view");
});
calendar.$('.fc-event:contains(event 4) .fc-content').trigger('click');
// create a new event and edit it
var $cell = calendar.$('.fc-day-grid .fc-row:eq(4) .fc-day:eq(2)');
testUtils.triggerMouseEvent($cell, "mousedown");
testUtils.triggerMouseEvent($cell, "mouseup");
$('.modal-body input:first').val('coucou').trigger('input');
testUtils.intercept(calendar, 'do_action', function (event) {
assert.deepEqual(event.data.action,
{
type: "ir.actions.act_window",
res_model: "event",
views: [[false, "form"]],
target: "current",
context: {
"default_name": "coucou",
"default_start": "2016-12-27 00:00:00",
"default_stop": "2016-12-27 00:00:00",
"default_allday": true
}
},
"should open the form view with the context default values");
});
$('.modal button.btn:contains(Edit)').trigger('click');
calendar.destroy();
assert.strictEqual($('#ui-datepicker-div:empty').length, 0, "should have a clean body");
});
});
});