Merge PR #2174 into 15.0

Signed-off-by pedrobaeza
pull/2258/head
OCA-git-bot 2022-07-16 13:48:59 +00:00
commit 9c22f50428
9 changed files with 178 additions and 137 deletions

View File

@ -115,6 +115,10 @@ If you disable one option, you can enable it for particular field by setting "cr
Whether the field should always show "Search more..." entry or not. Whether the field should always show "Search more..." entry or not.
``web_m2x_options.field_limit_entries`` *int*
Number of displayed lines on all One2many fields
To add these parameters go to Configuration -> Technical -> Parameters -> System Parameters and add new parameters like: To add these parameters go to Configuration -> Technical -> Parameters -> System Parameters and add new parameters like:
- web_m2x_options.create: False - web_m2x_options.create: False
@ -122,6 +126,7 @@ To add these parameters go to Configuration -> Technical -> Parameters -> System
- web_m2x_options.m2o_dialog: False - web_m2x_options.m2o_dialog: False
- web_m2x_options.limit: 10 - web_m2x_options.limit: 10
- web_m2x_options.search_more: True - web_m2x_options.search_more: True
- web_m2x_options.field_limit_entries: 5
Example Example
@ -178,6 +183,7 @@ Contributors
* Jairo Llopis <jairo.llopis@tecnativa.com> * Jairo Llopis <jairo.llopis@tecnativa.com>
* David Vidal <david.vidal@tecnativa.com> * David Vidal <david.vidal@tecnativa.com>
* Ernesto Tejeda <ernesto.tejeda87@gmail.com> * Ernesto Tejeda <ernesto.tejeda87@gmail.com>
* Carlos Roca
* Bhavesh Odedra <bodedra@opensourceintegrators.com> * Bhavesh Odedra <bodedra@opensourceintegrators.com>
* Dhara Solanki <dhara.solanki@initos.com> (http://www.initos.com) * Dhara Solanki <dhara.solanki@initos.com> (http://www.initos.com)

View File

@ -17,7 +17,10 @@
"license": "AGPL-3", "license": "AGPL-3",
"depends": ["web"], "depends": ["web"],
"assets": { "assets": {
"web.assets_backend": ["web_m2x_options/static/src/js/form.js"], "web.assets_backend": [
"web_m2x_options/static/src/js/form.js",
"web_m2x_options/static/src/js/ir_options.js",
],
"web.assets_qweb": ["web_m2x_options/static/src/xml/base.xml"], "web.assets_qweb": ["web_m2x_options/static/src/xml/base.xml"],
}, },
"installable": True, "installable": True,

View File

@ -12,5 +12,7 @@ class IrConfigParameter(models.Model):
"web_m2x_options.limit", "web_m2x_options.limit",
"web_m2x_options.search_more", "web_m2x_options.search_more",
"web_m2x_options.m2o_dialog", "web_m2x_options.m2o_dialog",
"web_m2x_options.field_limit_entries",
] ]
return self.sudo().search_read([["key", "in", opts]], ["key", "value"]) values = self.sudo().search_read([["key", "in", opts]], ["key", "value"])
return {res["key"]: res["value"] for res in values}

View File

@ -9,5 +9,6 @@
* Jairo Llopis <jairo.llopis@tecnativa.com> * Jairo Llopis <jairo.llopis@tecnativa.com>
* David Vidal <david.vidal@tecnativa.com> * David Vidal <david.vidal@tecnativa.com>
* Ernesto Tejeda <ernesto.tejeda87@gmail.com> * Ernesto Tejeda <ernesto.tejeda87@gmail.com>
* Carlos Roca
* Bhavesh Odedra <bodedra@opensourceintegrators.com> * Bhavesh Odedra <bodedra@opensourceintegrators.com>
* Dhara Solanki <dhara.solanki@initos.com> (http://www.initos.com) * Dhara Solanki <dhara.solanki@initos.com> (http://www.initos.com)

View File

@ -37,7 +37,7 @@ in the field's options dict
``open`` *boolean* (Default: ``False``) ``open`` *boolean* (Default: ``False``)
Makes many2many_tags buttons that open the linked resource Makes many2many_tags and one2many rows buttons that open the linked resource
``no_color_picker`` *boolean* (Default: ``False``) ``no_color_picker`` *boolean* (Default: ``False``)
@ -69,6 +69,10 @@ If you disable one option, you can enable it for particular field by setting "cr
Whether the field should always show "Search more..." entry or not. Whether the field should always show "Search more..." entry or not.
``web_m2x_options.field_limit_entries`` *int*
Number of displayed lines on all One2many fields
To add these parameters go to Configuration -> Technical -> Parameters -> System Parameters and add new parameters like: To add these parameters go to Configuration -> Technical -> Parameters -> System Parameters and add new parameters like:
- web_m2x_options.create: False - web_m2x_options.create: False
@ -76,6 +80,7 @@ To add these parameters go to Configuration -> Technical -> Parameters -> System
- web_m2x_options.m2o_dialog: False - web_m2x_options.m2o_dialog: False
- web_m2x_options.limit: 10 - web_m2x_options.limit: 10
- web_m2x_options.search_more: True - web_m2x_options.search_more: True
- web_m2x_options.field_limit_entries: 5
Example Example

View File

@ -6,19 +6,23 @@ odoo.define("web_m2x_options.web_m2x_options", function (require) {
var core = require("web.core"), var core = require("web.core"),
data = require("web.data"), data = require("web.data"),
Dialog = require("web.Dialog"), Dialog = require("web.Dialog"),
FormView = require("web.FormView"),
view_dialogs = require("web.view_dialogs"), view_dialogs = require("web.view_dialogs"),
relational_fields = require("web.relational_fields"), relational_fields = require("web.relational_fields"),
rpc = require("web.rpc"); ir_options = require("web_m2x_options.ir_options");
var _t = core._t, var _t = core._t,
FieldMany2ManyTags = relational_fields.FieldMany2ManyTags, FieldMany2ManyTags = relational_fields.FieldMany2ManyTags,
FieldMany2One = relational_fields.FieldMany2One, FieldMany2One = relational_fields.FieldMany2One,
FieldOne2Many = relational_fields.FieldOne2Many,
FormFieldMany2ManyTags = relational_fields.FormFieldMany2ManyTags; FormFieldMany2ManyTags = relational_fields.FormFieldMany2ManyTags;
var web_m2x_options = rpc.query({ function is_option_set(option) {
model: "ir.config_parameter", if (_.isUndefined(option)) return false;
method: "get_web_m2x_options", if (typeof option === "string") return option === "true" || option === "True";
}); if (typeof option === "boolean") return option;
return false;
}
var M2ODialog = Dialog.extend({ var M2ODialog = Dialog.extend({
template: "M2ODialog", template: "M2ODialog",
@ -92,43 +96,13 @@ odoo.define("web_m2x_options.web_m2x_options", function (require) {
}); });
FieldMany2One.include({ FieldMany2One.include({
start: function () {
this._super.apply(this, arguments);
return this.get_options();
},
get_options: function () {
var self = this;
if (_.isUndefined(this.ir_options_loaded)) {
this.ir_options_loaded = $.Deferred();
this.ir_options = {};
web_m2x_options.then(function (records) {
_(records).each(function (record) {
self.ir_options[record.key] = record.value;
});
self.ir_options_loaded.resolve();
});
}
return $.when();
},
is_option_set: function (option) {
if (_.isUndefined(option)) return false;
if (typeof option === "string")
return option === "true" || option === "True";
if (typeof option === "boolean") return option;
return false;
},
_onInputFocusout: function () { _onInputFocusout: function () {
var m2o_dialog_opt = var m2o_dialog_opt =
this.is_option_set(this.nodeOptions.m2o_dialog) || is_option_set(this.nodeOptions.m2o_dialog) ||
(_.isUndefined(this.nodeOptions.m2o_dialog) && (_.isUndefined(this.nodeOptions.m2o_dialog) &&
this.is_option_set( is_option_set(ir_options["web_m2x_options.m2o_dialog"])) ||
this.ir_options["web_m2x_options.m2o_dialog"]
)) ||
(_.isUndefined(this.nodeOptions.m2o_dialog) && (_.isUndefined(this.nodeOptions.m2o_dialog) &&
_.isUndefined(this.ir_options["web_m2x_options.m2o_dialog"])); _.isUndefined(ir_options["web_m2x_options.m2o_dialog"]));
if (this.can_create && this.floating && m2o_dialog_opt) { if (this.can_create && this.floating && m2o_dialog_opt) {
new M2ODialog(this, this.string, this.$input.val()).open(); new M2ODialog(this, this.string, this.$input.val()).open();
} }
@ -136,14 +110,12 @@ odoo.define("web_m2x_options.web_m2x_options", function (require) {
_search: function (search_val) { _search: function (search_val) {
var self = this; var self = this;
if (search_val === undefined) {
return this._super.apply(this, arguments);
}
var def = new Promise((resolve) => { var def = new Promise((resolve) => {
// Add options limit used to change number of selections record // Add options limit used to change number of selections record
// returned. // returned.
if (!_.isUndefined(self.ir_options["web_m2x_options.limit"])) { if (!_.isUndefined(ir_options["web_m2x_options.limit"])) {
this.limit = parseInt(self.ir_options["web_m2x_options.limit"], 10); this.limit = parseInt(ir_options["web_m2x_options.limit"], 10);
} }
if (typeof self.nodeOptions.limit === "number") { if (typeof self.nodeOptions.limit === "number") {
@ -221,22 +193,27 @@ odoo.define("web_m2x_options.web_m2x_options", function (require) {
}); });
} }
// Search more... if more results that max // Search more...
var can_search_more = // Resolution order:
self.nodeOptions && // 1- check if "search_more" is set locally in node's options
self.is_option_set(self.nodeOptions.search_more), // 2- if set locally, apply its value
search_more_undef = // 3- if not set locally, check if it's set globally via ir.config_parameter
_.isUndefined(self.nodeOptions.search_more) && // 4- if set globally, apply its value
_.isUndefined( // 5- if not set globally either, check if returned values are more than node's limit
self.ir_options["web_m2x_options.search_more"] if (!_.isUndefined(self.nodeOptions.search_more)) {
), var search_more = is_option_set(self.nodeOptions.search_more);
search_more = self.is_option_set( } else if (
self.ir_options["web_m2x_options.search_more"] !_.isUndefined(ir_options["web_m2x_options.search_more"])
) {
var search_more = is_option_set(
ir_options["web_m2x_options.search_more"]
); );
} else {
var search_more = values.length > self.limit;
}
if (values.length > self.limit) { if (search_more) {
values = values.slice(0, self.limit); values = values.slice(0, self.limit);
if (can_search_more || search_more_undef || search_more) {
values.push({ values.push({
label: _t("Search More..."), label: _t("Search More..."),
action: function () { action: function () {
@ -285,20 +262,19 @@ odoo.define("web_m2x_options.web_m2x_options", function (require) {
classname: "o_m2o_dropdown_option", classname: "o_m2o_dropdown_option",
}); });
} }
}
var create_enabled = self.can_create && !self.nodeOptions.no_create; var create_enabled = self.can_create && !self.nodeOptions.no_create;
// Quick create // Quick create
var raw_result = _.map(result, function (x) { var raw_result = _.map(result, function (x) {
return x[1]; return x[1];
}); });
var quick_create = self.is_option_set(self.nodeOptions.create), var quick_create = is_option_set(self.nodeOptions.create),
quick_create_undef = _.isUndefined(self.nodeOptions.create), quick_create_undef = _.isUndefined(self.nodeOptions.create),
m2x_create_undef = _.isUndefined( m2x_create_undef = _.isUndefined(
self.ir_options["web_m2x_options.create"] ir_options["web_m2x_options.create"]
), ),
m2x_create = self.is_option_set( m2x_create = is_option_set(
self.ir_options["web_m2x_options.create"] ir_options["web_m2x_options.create"]
); );
var show_create = var show_create =
(!self.nodeOptions && (m2x_create_undef || m2x_create)) || (!self.nodeOptions && (m2x_create_undef || m2x_create)) ||
@ -325,16 +301,16 @@ odoo.define("web_m2x_options.web_m2x_options", function (require) {
// Create and edit ... // Create and edit ...
var create_edit = var create_edit =
self.is_option_set(self.nodeOptions.create) || is_option_set(self.nodeOptions.create) ||
self.is_option_set(self.nodeOptions.create_edit), is_option_set(self.nodeOptions.create_edit),
create_edit_undef = create_edit_undef =
_.isUndefined(self.nodeOptions.create) && _.isUndefined(self.nodeOptions.create) &&
_.isUndefined(self.nodeOptions.create_edit), _.isUndefined(self.nodeOptions.create_edit),
m2x_create_edit_undef = _.isUndefined( m2x_create_edit_undef = _.isUndefined(
self.ir_options["web_m2x_options.create_edit"] ir_options["web_m2x_options.create_edit"]
), ),
m2x_create_edit = self.is_option_set( m2x_create_edit = is_option_set(
self.ir_options["web_m2x_options.create_edit"] ir_options["web_m2x_options.create_edit"]
); );
var show_create_edit = var show_create_edit =
(!self.nodeOptions && (!self.nodeOptions &&
@ -374,6 +350,17 @@ odoo.define("web_m2x_options.web_m2x_options", function (require) {
}); });
}); });
this.orderer.add(def); this.orderer.add(def);
// Add options limit used to change number of selections record
// returned.
if (!_.isUndefined(ir_options["web_m2x_options.limit"])) {
this.limit = parseInt(ir_options["web_m2x_options.limit"], 10);
}
if (typeof this.nodeOptions.limit === "number") {
this.limit = this.nodeOptions.limit;
}
return def; return def;
}, },
}); });
@ -389,17 +376,9 @@ odoo.define("web_m2x_options.web_m2x_options", function (require) {
return result; return result;
}, },
is_option_set: function (option) {
if (_.isUndefined(option)) return false;
if (typeof option === "string")
return option === "true" || option === "True";
if (typeof option === "boolean") return option;
return false;
},
_onOpenBadge: function (event) { _onOpenBadge: function (event) {
var self = this; var self = this;
var open = self.nodeOptions && self.is_option_set(self.nodeOptions.open); var open = self.nodeOptions && is_option_set(self.nodeOptions.open);
if (open) { if (open) {
var context = self.record.getContext(self.recordParams); var context = self.record.getContext(self.recordParams);
var id = parseInt($(event.currentTarget).data("id"), 10); var id = parseInt($(event.currentTarget).data("id"), 10);
@ -455,14 +434,37 @@ odoo.define("web_m2x_options.web_m2x_options", function (require) {
}, },
}); });
FieldOne2Many.include({
_onOpenRecord: function (ev) {
var self = this;
var open = this.nodeOptions.open;
if (open && self.mode === "readonly") {
ev.stopPropagation();
var id = ev.data.id;
var res_id = self.record.data[self.name].data.filter(
(line) => line.id === id
)[0].res_id;
self._rpc({
model: self.field.relation,
method: "get_formview_action",
args: [[res_id]],
}).then(function (action) {
return self.do_action(action);
});
} else {
return this._super.apply(this, arguments);
}
},
});
FormFieldMany2ManyTags.include({ FormFieldMany2ManyTags.include({
events: _.extend({}, FormFieldMany2ManyTags.prototype.events, { events: _.extend({}, FormFieldMany2ManyTags.prototype.events, {
"click .badge": "_onOpenBadge", "click .badge": "_onOpenBadge",
}), }),
_onOpenBadge: function (event) { _onOpenBadge: function (event) {
var open = this.is_option_set(this.nodeOptions.open); var open = is_option_set(this.nodeOptions.open);
var no_color_picker = this.is_option_set(this.nodeOptions.no_color_picker); var no_color_picker = is_option_set(this.nodeOptions.no_color_picker);
this._super.apply(this, arguments); this._super.apply(this, arguments);
if (!open && !no_color_picker) { if (!open && !no_color_picker) {
this._onOpenColorPicker(event); this._onOpenColorPicker(event);
@ -472,4 +474,16 @@ odoo.define("web_m2x_options.web_m2x_options", function (require) {
} }
}, },
}); });
// Extending class to allow change the limit of o2m registry entries using the
// system parameter "web_m2x_options.field_limit_entries".
FormView.include({
_setSubViewLimit: function (attrs) {
this._super(attrs);
var limit = ir_options["web_m2x_options.field_limit_entries"];
if (!_.isUndefined(limit)) {
attrs.limit = parseInt(limit);
}
},
});
}); });

View File

@ -0,0 +1,12 @@
/* Copyright 2020 Tecnativa - Carlos Roca
* * License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */
odoo.define("web_m2x_options.ir_options", function (require) {
"use strict";
var rpc = require("web.rpc");
return rpc.query({
model: "ir.config_parameter",
method: "get_web_m2x_options",
});
});

View File

@ -5,7 +5,7 @@
<t t-extend="FieldMany2One"> <t t-extend="FieldMany2One">
<t t-jquery=".o_external_button" t-operation="attributes"> <t t-jquery=".o_external_button" t-operation="attributes">
<attribute name="t-if"> <attribute name="t-if">
!(widget.nodeOptions.no_open || widget.nodeOptions.no_open_edit) !(widget.nodeOptions.no_open || widget.nodeOptions.no_open_edit || widget.noOpen)
</attribute> </attribute>
</t> </t>
</t> </t>

View File

@ -16,15 +16,13 @@ class TestIrConfigParameter(common.TransactionCase):
def test_web_m2x_options_key(self): def test_web_m2x_options_key(self):
web_m2x_options = self.env["ir.config_parameter"].get_web_m2x_options() web_m2x_options = self.env["ir.config_parameter"].get_web_m2x_options()
options = {opt["key"]: opt["value"] for opt in web_m2x_options} self.assertIn("web_m2x_options.limit", web_m2x_options)
self.assertIn("web_m2x_options.limit", options) self.assertNotIn("web_m2x_options.m2o_dialog_test", web_m2x_options)
self.assertNotIn("web_m2x_options.m2o_dialog_test", options)
def test_web_m2x_options_value(self): def test_web_m2x_options_value(self):
web_m2x_options = self.env["ir.config_parameter"].get_web_m2x_options() web_m2x_options = self.env["ir.config_parameter"].get_web_m2x_options()
options = {opt["key"]: opt["value"] for opt in web_m2x_options} self.assertEqual(web_m2x_options["web_m2x_options.limit"], "10")
self.assertEqual(options["web_m2x_options.limit"], "10") self.assertTrue(bool(web_m2x_options["web_m2x_options.create_edit"]))
self.assertTrue(bool(options["web_m2x_options.create_edit"])) self.assertTrue(bool(web_m2x_options["web_m2x_options.create"]))
self.assertTrue(bool(options["web_m2x_options.create"])) self.assertEqual(web_m2x_options["web_m2x_options.search_more"], "False")
self.assertEqual(options["web_m2x_options.search_more"], "False") self.assertTrue(bool(web_m2x_options["web_m2x_options.m2o_dialog"]))
self.assertTrue(bool(options["web_m2x_options.m2o_dialog"]))