Merge PR #2828 into 12.0

Signed-off-by dreispt
This commit is contained in:
OCA-git-bot
2024-05-18 14:56:48 +00:00
3 changed files with 69 additions and 64 deletions

View File

@@ -7,70 +7,56 @@ from odoo.exceptions import ValidationError
class BaseModel(models.BaseModel):
_inherit = "base"
@api.model
def _check_product_picker_duplicated_products(self, vals_list):
relation = self.env.context.get("product_picker_relation")
if relation != self._name or not len(vals_list):
return
product_field = self.env.context.get("product_picker_product_field")
product_ids = [
values[product_field] for values in vals_list if product_field in values
]
num_products = len(product_ids)
if not num_products:
return
elif num_products != len(set(product_ids)):
raise ValidationError(
_("Can't create the %s: Duplicated product! (Inside query)") % relation
)
relation_field = self.env.context.get("product_picker_relation_field")
# All records have the same 'relation id' when created with the product picker
relation_id = vals_list[0][relation_field]
# When write maybe need get the value from the record
if not relation_id:
field_obj = self[relation_field]
if field_obj:
relation_id = relation_id.id
db_sol = self.search(
[
(relation_field, "=", relation_id),
(product_field, "in", product_ids),
],
limit=1,
)
if db_sol:
raise ValidationError(
_("Can't create the %s: Duplicated product (%s)! (Already in database)")
% (relation, db_sol[product_field].display_name)
)
@api.model_create_multi
def create(self, vals_list):
"""Avoid create lines that have a product currently used when use the product
picker"""
relation = self.env.context.get("product_picker_relation")
if relation == self._name and len(vals_list):
product_field = self.env.context.get("product_picker_product_field")
product_ids = [
values[product_field] for values in vals_list if product_field in values
]
if len(product_ids) != len(set(product_ids)):
raise ValidationError(
_("Can't create the %s: Duplicated product! (Inside query)")
% relation
)
relation_field = self.env.context.get("product_picker_relation_field")
# All records have the same 'relation id' when created with the product
# picker
relation_id = vals_list[0][relation_field]
has_product = (
self.search(
[
(relation_field, "=", relation_id),
(product_field, "in", product_ids),
],
count=True,
limit=1,
)
!= 0
)
if has_product:
raise ValidationError(
_("Can't create the %s: Duplicated product! (Already in database)")
% relation
)
"""
Avoid create lines that have a product currently used
when use the product picker
"""
self._check_product_picker_duplicated_products(vals_list)
return super().create(vals_list)
def write(self, values):
"""Avoid write lines that have a product currently used when use the product
picker"""
relation = self.env.context.get("product_picker_relation")
product_field = self.env.context.get("product_picker_product_field")
if relation == self._name and product_field in values:
relation_field = self.env.context.get("product_picker_relation_field")
relation_id = (
values[relation_field]
if relation_field in values
else self.get(relation_field)
)
product_id = values[product_field]
has_product = (
self.search(
[
(relation_field, "=", relation_id),
(product_field, "=", product_id),
],
count=True,
limit=1,
)
!= 0
)
if has_product:
raise ValidationError(
_("Can't write the %s: Duplicated product! (Already in database)")
% relation
)
"""
Avoid write lines that have a product currently used
when use the product picker
"""
self._check_product_picker_duplicated_products([values])
return super().write(values)

View File

@@ -98,6 +98,9 @@ odoo.define(
},
canBeUpdated: function() {
if (!this.getParent()) {
return false;
}
var model = this.getParent().getBasicFieldParams().model;
for (var widget of this.widgets) {
if (!widget.state) {
@@ -148,7 +151,10 @@ odoo.define(
});
},
getWidgetsWithoutOnchange: function(product_id) {
getWidgetsWithoutOnchange: function() {
if (!this.getParent()) {
return false;
}
var model = this.getParent().getBasicFieldParams().model;
return _.filter(this.widgets, function(item) {
return (

View File

@@ -809,7 +809,12 @@ odoo.define("web_widget_one2many_product_picker.FieldOne2ManyProductPicker", fun
*/
_saveDocument: function(ids_to_update) {
var widgets = this.renderer.getWidgetsWithoutOnchange();
if (!_.isEmpty(this._ids_to_wait) || _.isEmpty(ids_to_update) || !_.isEmpty(widgets)) {
if (
widgets === false ||
!_.isEmpty(this._ids_to_wait) ||
_.isEmpty(ids_to_update) ||
!_.isEmpty(widgets)
) {
return this._saveChain;
}
var self = this;
@@ -1135,11 +1140,19 @@ odoo.define("web_widget_one2many_product_picker.FieldOne2ManyProductPicker", fun
_onResumeAutoSave: function() {
// Check if can resume
if (this.$('.oe_product_picker_quick_modif_price').is(':visible') || this.$('.oe_search_input').is(':focus') || this.$('.oe_flip_card.active').length) {
if (
!this._is_auto_save_paused ||
this.$(".oe_product_picker_quick_modif_price").is(":visible") ||
this.$(".oe_search_input").is(":focus") ||
this.$(".oe_flip_card.active").length
) {
return;
}
if (this._need_resume_auto_save) {
if (this._auto_save_timeout) {
clearTimeout(this._auto_save_timeout);
}
this._auto_save_timeout = setTimeout(
this._saveDocument.bind(this),
this.options.auto_save_delay,