Skip to content

Commit

Permalink
[FIX] cetmix_tower_server: Server creation
Browse files Browse the repository at this point in the history
Fix incorrect creation of server variables when creating a server from a template

Task: 4253
  • Loading branch information
GabbasovDinar committed Jan 15, 2025
1 parent b1e01f1 commit b880fd7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 50 deletions.
47 changes: 10 additions & 37 deletions cetmix_tower_server/models/cx_tower_server_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down

0 comments on commit b880fd7

Please sign in to comment.