[IMP] web: mock server: enhance default get

Before this commit, the mock server "_mockDefaultGet" method was a bit
naive:
- it accepted any 'default_' field coming from the 'kwargs.context'
  regardless of the existence of the field
- it accepted any value for these fields

Now the method is looks a bit more like its original server-side
counterpart:
- the context keys are added if they correspond to actual fields
- a pseudo validation removes many2one fields values not corresponding
  to any record
This commit is contained in:
Julien Mougenot
2020-06-10 13:08:33 +00:00
parent 001e26cd97
commit a425c58d99
+25 -14
View File
@@ -482,27 +482,38 @@ var MockServer = Class.extend({
* @private
* @param {string} modelName
* @param {array[]} args a list with a list of fields in the first position
* @param {Object} [kwargs]
* @param {Object} [kwargs={}]
* @param {Object} [kwargs.context] the context to eventually read default
* values
* @returns {Object}
*/
_mockDefaultGet: function (modelName, args, kwargs) {
var result = {};
var fields = args[0];
var model = this.data[modelName];
_.each(fields, function (name) {
var field = model.fields[name];
_mockDefaultGet: function (modelName, args, kwargs={}) {
const fields = args[0];
const model = this.data[modelName];
const result = {};
for (const fieldName of fields) {
const key = "default_" + fieldName;
if (kwargs.context && key in kwargs.context) {
result[fieldName] = kwargs.context[key];
continue;
}
const field = model.fields[fieldName];
if ('default' in field) {
result[name] = field.default;
result[fieldName] = field.default;
continue;
}
});
if (kwargs && kwargs.context)
_.each(kwargs.context, function (value, key) {
if ('default_' === key.slice(0, 8)) {
result[key.slice(8)] = value;
}
for (const fieldName in result) {
const field = model.fields[fieldName];
if (field.type === "many2one") {
const recordExists = this.data[field.relation].records.some(
(r) => r.id === result[fieldName]
);
if (!recordExists) {
delete result[fieldName];
}
}
});
}
return result;
},
/**