mirror of https://github.com/OCA/web.git
[IMP] web_widget_one2many_product_picker: more detailed message error
(cherry picked from commit 8c45c822971f091f9e9055de2c85cafb72489f9c)pull/2828/head
parent
316cb0a0f5
commit
53db71bc65
|
@ -7,70 +7,50 @@ from odoo.exceptions import ValidationError
|
||||||
class BaseModel(models.BaseModel):
|
class BaseModel(models.BaseModel):
|
||||||
_inherit = "base"
|
_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
|
@api.model_create_multi
|
||||||
def create(self, vals_list):
|
def create(self, vals_list):
|
||||||
"""Avoid create lines that have a product currently used when use the product
|
"""Avoid create lines that have a product currently used when use the product picker"""
|
||||||
picker"""
|
self._check_product_picker_duplicated_products(vals_list)
|
||||||
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
|
|
||||||
)
|
|
||||||
return super().create(vals_list)
|
return super().create(vals_list)
|
||||||
|
|
||||||
def write(self, values):
|
def write(self, values):
|
||||||
"""Avoid write lines that have a product currently used when use the product
|
"""Avoid write lines that have a product currently used when use the product picker"""
|
||||||
picker"""
|
self._check_product_picker_duplicated_products([values])
|
||||||
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
|
|
||||||
)
|
|
||||||
return super().write(values)
|
return super().write(values)
|
||||||
|
|
|
@ -1140,16 +1140,12 @@ odoo.define("web_widget_one2many_product_picker.FieldOne2ManyProductPicker", fun
|
||||||
|
|
||||||
_onResumeAutoSave: function() {
|
_onResumeAutoSave: function() {
|
||||||
// Check if can resume
|
// Check if can resume
|
||||||
<<<<<<< HEAD
|
|
||||||
if (this.$('.oe_product_picker_quick_modif_price').is(':visible') || this.$('.oe_search_input').is(':focus') || this.$('.oe_flip_card.active').length) {
|
|
||||||
=======
|
|
||||||
if (
|
if (
|
||||||
!this._is_auto_save_paused ||
|
!this._is_auto_save_paused ||
|
||||||
this.$(".oe_product_picker_quick_modif_price").is(":visible") ||
|
this.$(".oe_product_picker_quick_modif_price").is(":visible") ||
|
||||||
this.$(".oe_search_input").is(":focus") ||
|
this.$(".oe_search_input").is(":focus") ||
|
||||||
this.$(".oe_flip_card.active").length
|
this.$(".oe_flip_card.active").length
|
||||||
) {
|
) {
|
||||||
>>>>>>> aca8ae42 ([FIX] web_widget_one2many_product_picker: Don't save in 'refresh' time)
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue