From a6baba32045b325fef87bdf63be7c5e939a2e109 Mon Sep 17 00:00:00 2001 From: Urvisha-OSI Date: Thu, 20 Jun 2024 17:16:47 +0530 Subject: [PATCH] [FIX] check product configurater steps and add warnings [FIX] tests fails issue [IMP] --- product_configurator/models/product.py | 43 ++++++++++++++++++- .../models/product_attribute.py | 16 ++++++- product_configurator/tests/test_product.py | 15 ++++--- .../views/product_config_view.xml | 1 + .../wizard/product_configurator.py | 22 ++++++---- 5 files changed, 78 insertions(+), 19 deletions(-) diff --git a/product_configurator/models/product.py b/product_configurator/models/product.py index c491213e51..413360d7de 100644 --- a/product_configurator/models/product.py +++ b/product_configurator/models/product.py @@ -333,8 +333,47 @@ def write(self, vals): configurable_templates = self.filtered(lambda template: template.config_ok) if change_config_ok or configurable_templates: self[:1].check_config_user_access() - - return super().write(vals) + res = super().write(vals) + if "config_step_line_ids" in vals: + miss_attrs = list( + set(self.attribute_line_ids.ids) + - set(self.config_step_line_ids.attribute_line_ids.ids) + ) + if miss_attrs: + attrs = [ + x.attribute_id.name + for x in self.env["product.template.attribute.line"].browse( + miss_attrs + ) + ] + raise ValidationError( + _( + "The following attributes are missing\ + from Configuration Steps: %s", + (attrs), + ) + ) + all_attrs = self.config_step_line_ids.attribute_line_ids.ids + check = False + dupAttrs = [] + for config_step in self.config_step_line_ids[1:]: + check = any(e in all_attrs for e in config_step.attribute_line_ids.ids) + if check: + dupAttrs.append( + [ + attr.attribute_id.name + for attr in config_step.attribute_line_ids + ] + ) + if check and dupAttrs: + raise ValidationError( + _( + "The following attributes have \ + duplicates in Configuration Steps: %s", + (dupAttrs), + ) + ) + return res @api.constrains("config_line_ids") def _check_config_line_domain(self): diff --git a/product_configurator/models/product_attribute.py b/product_configurator/models/product_attribute.py index 1e7938e636..bf485c7b45 100644 --- a/product_configurator/models/product_attribute.py +++ b/product_configurator/models/product_attribute.py @@ -128,7 +128,8 @@ def validate_custom_val(self, val): elif maxv and val > maxv: raise ValidationError( _( - "Selected custom value '%(name)s' must be lower than %(max_value)s" + "Selected custom value '%(name)s'\ + must be lower than %(max_value)s" ) % ({"name": self.name, "max_val": self.max_val + 1}) ) @@ -418,6 +419,19 @@ def name_search(self, name="", args=None, operator="ilike", limit=100): ): args = [("id", "in", wiz_id.domain_attr_2_ids.ids)] + else: + field_prefix = wiz_id._prefixes.get("field_prefix") + if field_prefix and self.env.context.get('field_name') and wiz_id.domain_attr_ids and wiz_id.domain_attr_2_ids: + attrs_split = self.env.context.get('field_name').split(field_prefix) + if attrs_split and len(attrs_split) == 2 and not args[0][2]: + attribute_id = int(attrs_split[1]) + domain_attr_ids = wiz_id.domain_attr_ids.filtered(lambda l:l.attribute_id.id == attribute_id) + domain_attr_2_ids = wiz_id.domain_attr_2_ids.filtered(lambda l:l.attribute_id.id == attribute_id) + if domain_attr_ids.ids: + args[0][2] = domain_attr_ids.ids + if domain_attr_2_ids.ids: + args[0][2] = domain_attr_2_ids.ids + product_tmpl_id = self.env.context.get("_cfg_product_tmpl_id") if product_tmpl_id: # TODO: Avoiding browse here could be a good performance enhancer diff --git a/product_configurator/tests/test_product.py b/product_configurator/tests/test_product.py index cc0e6ad2f7..5d8f8cec2c 100644 --- a/product_configurator/tests/test_product.py +++ b/product_configurator/tests/test_product.py @@ -218,13 +218,14 @@ def test_06_configure_product(self): "attribute_line_ids": [(6, 0, [self.attributeLine3.id])], } ) - self.product_tmpl_id.write( - { - "config_step_line_ids": [ - (6, 0, [self.configStepLine1.id, self.configStepLine2.id]) - ], - } - ) + with self.assertRaises(ValidationError): + self.product_tmpl_id.write( + { + "config_step_line_ids": [ + (6, 0, [self.configStepLine1.id, self.configStepLine2.id]) + ], + } + ) # configure product self.product_tmpl_id.configure_product() diff --git a/product_configurator/views/product_config_view.xml b/product_configurator/views/product_config_view.xml index 9a132f28fc..67e5d51bd1 100644 --- a/product_configurator/views/product_config_view.xml +++ b/product_configurator/views/product_config_view.xml @@ -165,6 +165,7 @@ /> + diff --git a/product_configurator/wizard/product_configurator.py b/product_configurator/wizard/product_configurator.py index 1363393f4f..7b689bc211 100644 --- a/product_configurator/wizard/product_configurator.py +++ b/product_configurator/wizard/product_configurator.py @@ -103,7 +103,6 @@ def get_state_selection(self): return steps open_lines = wiz.config_session_id.get_open_step_lines() - if open_lines: open_steps = open_lines.mapped(lambda x: (str(x.id), x.config_step_id.name)) steps = open_steps if wiz.product_id else steps + open_steps @@ -359,7 +358,6 @@ def apply_onchange_values(self, values, field_name, field_onchange): elif values and value[0][2]: self.dyn_field_2_value = key self.domain_attr_2_ids = [(6, 0, value[0][2])] - vals = self.get_form_vals( dynamic_fields=dynamic_fields, domains=domains, @@ -375,9 +373,10 @@ def onchange(self, values, field_name, field_onchange): onchange_values = self.apply_onchange_values( values=values, field_name=field_name, field_onchange=field_onchange ) - field_prefix = self._prefixes.get("field_prefix") + if field_name == ['product_preset_id']: + self.with_context(preset_values=values)._onchange_product_preset() vals = onchange_values.get("value", {}) for key, val in vals.items(): if isinstance(val, int) and key.startswith(field_prefix): @@ -451,7 +450,11 @@ def _onchange_state(self): @api.onchange("product_preset_id") def _onchange_product_preset(self): """Set value ids as from product preset""" - pta_value_ids = self.product_preset_id.product_template_attribute_value_ids + preset_id = self.product_preset_id + if not preset_id and self.env.context.get('preset_values'): + preset_id = self.env.context.get('preset_values').get('product_preset_id') + preset_id = self.env["product.product"].browse(preset_id) + pta_value_ids = preset_id.product_template_attribute_value_ids attr_value_ids = pta_value_ids.mapped("product_attribute_value_id") self.value_ids = attr_value_ids @@ -871,7 +874,6 @@ def read(self, fields=None, load="_classic_read"): field_prefix = self._prefixes.get("field_prefix") custom_field_prefix = self._prefixes.get("custom_field_prefix") - attr_vals = [f for f in fields if f.startswith(field_prefix)] custom_attr_vals = [f for f in fields if f.startswith(custom_field_prefix)] @@ -1020,10 +1022,12 @@ def action_reset(self): self.config_session_id.unlink() except Exception: session = self.env["product.config.step"] - action = session_product_tmpl_id.with_context({}).create_config_wizard( - click_next=False - ) - return action + if session_product_tmpl_id: + action = session_product_tmpl_id.with_context({}).create_config_wizard( + click_next=False + ) + return action + return False def get_wizard_action(self, view_cache=False, wizard=None): """Return action of wizard