Skip to content

Commit

Permalink
Merge pull request #123 from OCA/16.0
Browse files Browse the repository at this point in the history
Syncing from upstream OCA/connector-interfaces (16.0)
  • Loading branch information
bt-admin authored Oct 11, 2024
2 parents 86316a9 + 6aa5cae commit d4651f4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Available addons
----------------
addon | version | maintainers | summary
--- | --- | --- | ---
[connector_importer](connector_importer/) | 16.0.1.1.1 | [![simahawk](https://github.com/simahawk.png?size=30px)](https://github.com/simahawk) | This module takes care of import sessions.
[connector_importer](connector_importer/) | 16.0.1.1.2 | [![simahawk](https://github.com/simahawk.png?size=30px)](https://github.com/simahawk) | This module takes care of import sessions.
[connector_importer_product](connector_importer_product/) | 16.0.1.0.0 | | Ease definition of product imports using `connector_importer`.

[//]: # (end addons)
Expand Down
2 changes: 1 addition & 1 deletion connector_importer/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Connector Importer
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:f7b849e3f22195c7a91d6e07ba38d714b8d54fe2c58d9815d59d6281f823bc3c
!! source digest: sha256:4b15d9b33afdd2a1f269995f9b6c81701e595a81da9441e8bc2c931eb17bafe7
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand Down
2 changes: 1 addition & 1 deletion connector_importer/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
"name": "Connector Importer",
"summary": """This module takes care of import sessions.""",
"version": "16.0.1.1.1",
"version": "16.0.1.1.2",
"depends": ["connector", "queue_job"],
"author": "Camptocamp, Odoo Community Association (OCA)",
"license": "AGPL-3",
Expand Down
2 changes: 1 addition & 1 deletion connector_importer/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ <h1 class="title">Connector Importer</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:f7b849e3f22195c7a91d6e07ba38d714b8d54fe2c58d9815d59d6281f823bc3c
!! source digest: sha256:4b15d9b33afdd2a1f269995f9b6c81701e595a81da9441e8bc2c931eb17bafe7
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/connector-interfaces/tree/16.0/connector_importer"><img alt="OCA/connector-interfaces" src="https://img.shields.io/badge/github-OCA%2Fconnector--interfaces-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/connector-interfaces-16-0/connector-interfaces-16-0-connector_importer"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/connector-interfaces&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module allows to import / update records from files using the connector
Expand Down
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, value_handler, field)
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, value_handler, field):
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

0 comments on commit d4651f4

Please sign in to comment.