From b880fd7d84186adcc46af184bfac8fa63821f549 Mon Sep 17 00:00:00 2001 From: GabbasovDinar Date: Wed, 15 Jan 2025 12:43:49 +0500 Subject: [PATCH] [FIX] cetmix_tower_server: Server creation Fix incorrect creation of server variables when creating a server from a template Task: 4253 --- .../models/cx_tower_server_template.py | 47 ++++--------------- .../cx_tower_server_template_create_wizard.py | 18 ++----- 2 files changed, 15 insertions(+), 50 deletions(-) diff --git a/cetmix_tower_server/models/cx_tower_server_template.py b/cetmix_tower_server/models/cx_tower_server_template.py index 54487437..78cb92a2 100644 --- a/cetmix_tower_server/models/cx_tower_server_template.py +++ b/cetmix_tower_server/models/cx_tower_server_template.py @@ -301,7 +301,10 @@ def _prepare_server_values(self, **kwargs): # custom specific variable values configuration_variables = kwargs.pop("configuration_variables", None) - line_ids_variables = kwargs.pop("line_ids_variables", None) + configuration_variable_options = kwargs.pop( + "configuration_variable_options", {} + ) + if configuration_variables: # Validate required variables self._validate_required_variables(configuration_variables) @@ -322,53 +325,23 @@ def _prepare_server_values(self, **kwargs): "name": variable_reference, } ) - variable_option_id = variable_id = False - - if not variable_value and line_ids_variables: - val_found = next( - ( - v - for v in line_ids_variables.values() - if v.get("variable_reference") == variable_reference - ), - None, - ) - if val_found: - variable_value = val_found.get("value_char") - variable_option_id = val_found.get("option_id", False) - variable_id = val_found.get("variable_id", False) + variable_option_id = configuration_variable_options.get( + variable_reference + ) variable_vals_list.append( ( 0, 0, { - "variable_id": variable.id or variable_id, - "value_char": variable_value or "", + "variable_id": variable.id, + "value_char": variable_value, "option_id": variable_option_id, }, ) ) - # update or add variable values - existing_variable_values = values.get("variable_value_ids", []) - variable_id_to_index = { - cmd[2]["variable_id"]: idx - for idx, cmd in enumerate(existing_variable_values) - if cmd[0] == 0 and "variable_id" in cmd[2] - } - - for new_command in variable_vals_list: - variable_id = new_command[2]["variable_id"] - if variable_id in variable_id_to_index: - idx = variable_id_to_index[variable_id] - # update exist command - existing_variable_values[idx] = new_command - else: - # add new command - existing_variable_values.append(new_command) - - values["variable_value_ids"] = existing_variable_values + values["variable_value_ids"] = variable_vals_list # remove the `id` field to ensure a new record is created # instead of updating the existing one diff --git a/cetmix_tower_server/wizards/cx_tower_server_template_create_wizard.py b/cetmix_tower_server/wizards/cx_tower_server_template_create_wizard.py index 5ae6a12f..397395c8 100644 --- a/cetmix_tower_server/wizards/cx_tower_server_template_create_wizard.py +++ b/cetmix_tower_server/wizards/cx_tower_server_template_create_wizard.py @@ -110,7 +110,11 @@ def _prepare_server_parameters(self): "configuration_variables": { line.variable_reference: line.value_char for line in self.line_ids - } + }, + "configuration_variable_options": { + line.variable_reference: line.option_id.id + for line in self.line_ids + }, } ) return res @@ -125,18 +129,6 @@ def action_confirm(self): raise ValidationError(self.missing_required_variables_message) kwargs = self._prepare_server_parameters() - kwargs["line_ids_variables"] = { - line.id: { - "variable_id": line.id, - "variable_reference": line.variable_reference, - "value_char": line.option_id.value_char - if line.option_id - else line.value_char, - "option_id": line.option_id.id if line.option_id else None, - "variable_type": line.variable_type, - } - for line in self.line_ids.wizard_id.line_ids - } server = self.server_template_id._create_new_server(self.name, **kwargs) action = self.env["ir.actions.actions"]._for_xml_id( "cetmix_tower_server.action_cx_tower_server"