Skip to content
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

[16.0][IMP] connector_importer: fix modifier method is too complex #147

Draft
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 36 additions & 20 deletions connector_importer/utils/mapper_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,20 +238,12 @@ def backend_to_rel( # noqa: C901
"""

def modifier(self, record, to_attr):
search_value = record.get(field)

if search_value and value_handler:
search_value = value_handler(self, record, search_value)

# get the real column and the model
column = self.model._fields[to_attr]
rel_model = self.env[column.comodel_name].with_context(active_test=False)
search_value = _get_search_value(self, record)
column, rel_model = _get_column_and_model(self, to_attr)

# handle defaults if no search value here
if not search_value and default_search_value:
search_value = default_search_value
if default_search_field:
modifier.search_field = default_search_field
if not search_value:
search_value = _handle_default_search_value()

# Support Odoo studio fields dynamically.
# When a model is created automatically from Odoo studio
Expand All @@ -277,21 +269,47 @@ def modifier(self, record, to_attr):
if column.type.endswith("2many"):
# we need multiple values
search_operator = "in"
if not isinstance(search_value, (list, tuple)):
if not isinstance(search_value, (list | tuple)):
search_value = [search_value]

if modifier.search_operator:
# override by param
search_operator = modifier.search_operator

# finally search it
search_args = [(modifier.search_field, search_operator, search_value)]

value = rel_model.search(search_args)

value = _handle_missing_values(
self, column, value, search_value, rel_model, record, to_attr
)

# handle the final value based on col type
return _handle_final_value(column, value)

def _get_search_value(self, record):
search_value = record.get(field)
if search_value and value_handler:
search_value = value_handler(self, record, search_value)
return search_value

def _get_column_and_model(self, to_attr):
column = self.model._fields[to_attr]
rel_model = self.env[column.comodel_name].with_context(active_test=False)
return column, rel_model

def _handle_default_search_value():
if default_search_value:
search_value = default_search_value
if default_search_field:
modifier.search_field = default_search_field
return search_value

def _handle_missing_values(
self, column, value, search_value, rel_model, record, to_attr
):
if (
column.type.endswith("2many")
and isinstance(search_value, (list, tuple))
and isinstance(search_value, (list | tuple))
and not len(search_value) == len(value or [])
):
# make sure we consider all the values and related records
Expand All @@ -305,7 +323,6 @@ def modifier(self, record, to_attr):
# using a `create_missing_handler`.
value = None

# create if missing
if not value and create_missing:
try:
if create_missing_handler:
Expand All @@ -319,18 +336,17 @@ def modifier(self, record, to_attr):
"Error: %s"
)
logger.error(msg, rel_model._name, record["_line_nr"], to_attr, str(e))
# raise error to make importer's savepoint ctx manager catch it
raise
return value

# handle the final value based on col type
def _handle_final_value(column, value):
if value:
if column.type == "many2one":
value = value[0].id
if column.type in ("one2many", "many2many"):
value = [(6, 0, [x.id for x in value])]
else:
return None

return value

# use method attributes to not mess up the variables' scope.
Expand Down
Loading