-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FIX] cetmix_tower_server: Server creation #183
Open
GabbasovDinar
wants to merge
3
commits into
14.0-dev
Choose a base branch
from
14.0-t4253-cetmix_tower_server-fix-copy-from-template
base: 14.0-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+223
−73
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,6 +171,10 @@ def create_server_from_template(self, template_reference, server_name, **kwargs) | |
`variable_reference`: `variable_value_char` | ||
eg: | ||
{'branch': 'prod', 'odoo_version': '16.0'} | ||
pick_all_template_variables (bool): This parameter ensures that the server | ||
being created considers existing variables from the template. | ||
If enabled, the template variables will also be included in the server | ||
variables. The default value is True. | ||
|
||
Returns: | ||
cx.tower.server: newly created server record | ||
|
@@ -199,6 +203,10 @@ def _create_new_server(self, name, **kwargs): | |
`variable_reference`: `variable_value_char` | ||
eg: | ||
{'branch': 'prod', 'odoo_version': '16.0'} | ||
pick_all_template_variables (bool): This parameter ensures that the server | ||
being created considers existing variables from the template. | ||
If enabled, the template variables will also be included in the server | ||
variables. The default value is True. | ||
|
||
Returns: | ||
cx.tower.server: newly created server record | ||
|
@@ -251,7 +259,7 @@ def _get_fields_tower_server(self): | |
"server_log_ids", | ||
] | ||
|
||
def _prepare_server_values(self, **kwargs): | ||
def _prepare_server_values(self, pick_all_template_variables=True, **kwargs): | ||
""" | ||
Prepare the server values to create a new server based on | ||
the current template. It reads all fields from the template, copies them, | ||
|
@@ -260,6 +268,10 @@ def _prepare_server_values(self, **kwargs): | |
data. | ||
|
||
Args: | ||
pick_all_template_variables (bool): This parameter ensures that the server | ||
being created considers existing variables from the template. | ||
If enabled, the template variables will also be included in the server | ||
variables. The default value is True. | ||
**kwargs: Additional values to update in the final server record. | ||
|
||
Returns: | ||
|
@@ -299,76 +311,136 @@ def _prepare_server_values(self, **kwargs): | |
|
||
values[field] = new_records | ||
|
||
# custom specific variable values | ||
# Handle configuration variables if provided. | ||
configuration_variables = kwargs.pop("configuration_variables", None) | ||
line_ids_variables = kwargs.pop("line_ids_variables", None) | ||
configuration_variable_options = kwargs.pop( | ||
"configuration_variable_options", {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see this one added to the docstring similar to how |
||
) | ||
|
||
if configuration_variables: | ||
# Validate required variables | ||
self._validate_required_variables(configuration_variables) | ||
|
||
variable_vals_list = [] | ||
variable_obj = self.env["cx.tower.variable"] | ||
# Search for existing variable options. | ||
option_references = list(configuration_variable_options.values()) | ||
existing_options = option_references and self.env[ | ||
"cx.tower.variable.option" | ||
].search([("reference", "in", option_references)]) | ||
missing_options = list( | ||
set(option_references) | ||
- {option.reference for option in existing_options} | ||
) | ||
|
||
for ( | ||
variable_reference, | ||
variable_value, | ||
) in configuration_variables.items(): | ||
variable = variable_obj.search( | ||
[("reference", "=", variable_reference)] | ||
) | ||
if not variable: | ||
variable = variable_obj.create( | ||
{ | ||
"name": variable_reference, | ||
} | ||
if missing_options: | ||
# Map variable references to their corresponding | ||
# invalid option references. | ||
missing_options_to_variables = { | ||
var_ref: opt_ref | ||
for var_ref, opt_ref in configuration_variable_options.items() | ||
if opt_ref in missing_options | ||
} | ||
# Generate a detailed error message for invalid variable options. | ||
detailed_message = "\n".join( | ||
_( | ||
"Variable reference '%(var_ref)s' has an invalid " | ||
"option reference '%(opt_ref)s'.", | ||
var_ref=var_ref, | ||
opt_ref=opt_ref, | ||
) | ||
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, | ||
for var_ref, opt_ref in missing_options_to_variables.items() | ||
) | ||
raise ValidationError( | ||
_( | ||
"Some variable options are invalid:\n%(detailed_message)s", | ||
detailed_message=detailed_message, | ||
) | ||
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) | ||
) | ||
|
||
# Map variable options to their IDs. | ||
configuration_variable_options_dict = { | ||
option.variable_id.id: option for option in existing_options | ||
} | ||
|
||
variable_obj = self.env["cx.tower.variable"] | ||
variable_references = list(configuration_variables.keys()) | ||
|
||
# Search for existing variables or create new ones if missing. | ||
exist_variables = variable_obj.search( | ||
[("reference", "in", variable_references)] | ||
) | ||
missing_references = list( | ||
set(variable_references) | ||
- {variable.reference for variable in exist_variables} | ||
) | ||
variable_vals_list = [ | ||
{"name": reference} for reference in missing_references | ||
] | ||
new_variables = variable_obj.create(variable_vals_list) | ||
all_variables = exist_variables | new_variables | ||
|
||
# Build a dictionary {variable: variable_value}. | ||
configuration_variable_dict = { | ||
variable: configuration_variables[variable.reference] | ||
for variable in all_variables | ||
} | ||
|
||
server_variable_vals_list = [] | ||
for variable, variable_value in configuration_variable_dict.items(): | ||
variable_option = configuration_variable_options_dict.get( | ||
variable.id | ||
) | ||
|
||
variable_vals_list.append( | ||
server_variable_vals_list.append( | ||
( | ||
0, | ||
0, | ||
{ | ||
"variable_id": variable.id or variable_id, | ||
"value_char": variable_value or "", | ||
"option_id": variable_option_id, | ||
"variable_id": variable.id, | ||
"value_char": variable_option | ||
and variable_option.value_char | ||
or variable_value, | ||
"option_id": variable_option and 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] | ||
} | ||
if pick_all_template_variables: | ||
# 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] | ||
} | ||
|
||
# Update exist variable options | ||
for exist_variable_id, index in variable_id_to_index.items(): | ||
option = configuration_variable_options_dict.get( | ||
exist_variable_id | ||
) | ||
if not option: | ||
continue | ||
existing_variable_values[index][2].update( | ||
{ | ||
"option_id": option.id, | ||
"value_char": option.value_char, | ||
} | ||
) | ||
|
||
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 | ||
# Prepare new command values for server variables | ||
for new_command in server_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 | ||
else: | ||
values["variable_value_ids"] = server_variable_vals_list | ||
|
||
# remove the `id` field to ensure a new record is created | ||
# instead of updating the existing one | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with these changes,
prepare_server_values
is not a bit long, should we refactor to make it shorter and easier to understand?