Components

Components are the blocks allowing to build a flexible and well decoupled code. They are based on the component addon and the base components of the Connector.

Core Component

class odoo.addons.connector_magento.components.core.BaseMagentoConnectorComponent(work_context)[source]

Bases: odoo.addons.component.core.AbstractComponent

Base Magento Connector Component

All components of this connector should inherit from it.

_name = 'base.magento.connector'
_inherit = 'base.connector'
_collection = 'magento.backend'
_module = 'connector_magento'

Magento Components

Backend Adapter

class odoo.addons.connector_magento.components.backend_adapter.MagentoLocation(location, username, password, use_custom_api_path=False)[source]

Bases: object

location
class odoo.addons.connector_magento.components.backend_adapter.MagentoAPI(location)[source]

Bases: object

api
call(method, arguments)[source]
class odoo.addons.connector_magento.components.backend_adapter.MagentoCRUDAdapter(work_context)[source]

Bases: odoo.addons.component.core.AbstractComponent

External Records Adapter for Magento

_name = 'magento.crud.adapter'
_inherit = ['base.backend.adapter', 'base.magento.connector']
_usage = 'backend.adapter'
search(filters=None)[source]

Search records according to some criterias and returns a list of ids

read(id, attributes=None)[source]

Returns the information of a record

search_read(filters=None)[source]

Search records according to some criterias and returns their information

create(data)[source]

Create a record on the external system

write(id, data)[source]

Update records on the external system

delete(id)[source]

Delete a record on the external system

_call(method, arguments)[source]
_module = 'connector_magento'
class odoo.addons.connector_magento.components.backend_adapter.GenericAdapter(work_context)[source]

Bases: odoo.addons.component.core.AbstractComponent

_name = 'magento.adapter'
_inherit = 'magento.crud.adapter'
_magento_model = None
_admin_path = None
search(filters=None)[source]

Search records according to some criterias and returns a list of ids

Return type:list
read(id, attributes=None)[source]

Returns the information of a record

Return type:dict
search_read(filters=None)[source]

Search records according to some criterias and returns their information

create(data)[source]

Create a record on the external system

write(id, data)[source]

Update records on the external system

delete(id)[source]

Delete a record on the external system

admin_url(id)[source]

Return the URL in the Magento admin for a record

_module = 'connector_magento'

Binder

class odoo.addons.connector_magento.components.binder.MagentoModelBinder(work_context)[source]

Bases: odoo.addons.component.core.Component

Bind records and give odoo/magento ids correspondence

Binding models are models called magento.{normal_model}, like magento.res.partner or magento.product.product. They are _inherits of the normal models and contains the Magento ID, the ID of the Magento Backend and the additional fields belonging to the Magento instance.

_name = 'magento.binder'
_inherit = ['base.binder', 'base.magento.connector']
_apply_on = ['magento.website', 'magento.store', 'magento.storeview', 'magento.res.partner', 'magento.address', 'magento.res.partner.category', 'magento.product.category', 'magento.product.product', 'magento.stock.picking', 'magento.sale.order', 'magento.sale.order.line', 'magento.account.invoice']
_module = 'connector_magento'

Deleter

class odoo.addons.connector_magento.components.deleter.MagentoDeleter(work_context)[source]

Bases: odoo.addons.component.core.AbstractComponent

Base deleter for Magento

_name = 'magento.exporter.deleter'
_inherit = 'base.deleter'
_usage = 'record.exporter.deleter'
run(external_id)[source]

Run the synchronization, delete the record on Magento

Parameters:external_id – identifier of the record to delete
_module = 'connector_magento'

Exporter

odoo.addons.connector_magento.components.exporter._logger = <logging.Logger object>

Exporters for Magento.

In addition to its export job, an exporter has to:

  • check in Magento if the record has been updated more recently than the last sync date and if yes, delay an import
  • call the bind method of the binder to update the last sync date
class odoo.addons.connector_magento.components.exporter.MagentoBaseExporter(working_context)[source]

Bases: odoo.addons.component.core.AbstractComponent

Base exporter for Magento

_name = 'magento.base.exporter'
_inherit = ['base.exporter', 'base.magento.connector']
_usage = 'record.exporter'
_delay_import()[source]

Schedule an import of the record.

Adapt in the sub-classes when the model is not imported using import_record.

_should_import()[source]

Before the export, compare the update date in Magento and the last sync date in Odoo, if the former is more recent, schedule an import to not miss changes done in Magento.

run(binding, *args, **kwargs)[source]

Run the synchronization

Parameters:binding – binding record to export
_run()[source]

Flow of the synchronization, implemented in inherited classes

_after_export()[source]

Can do several actions after exporting a record on magento

_module = 'connector_magento'
class odoo.addons.connector_magento.components.exporter.MagentoExporter(working_context)[source]

Bases: odoo.addons.component.core.AbstractComponent

A common flow for the exports to Magento

_name = 'magento.exporter'
_inherit = 'magento.base.exporter'
_lock()[source]

Lock the binding record.

Lock the binding record so we are sure that only one export job is running for this record if concurrent jobs have to export the same record.

When concurrent jobs try to export the same record, the first one will lock and proceed, the others will fail to lock and will be retried later.

This behavior works also when the export becomes multilevel with _export_dependencies(). Each level will set its own lock on the binding record it has to export.

_has_to_skip()[source]

Return True if the export can be skipped

_retry_unique_violation(*args, **kwds)[source]

Context manager: catch Unique constraint error and retry the job later.

When we execute several jobs workers concurrently, it happens that 2 jobs are creating the same record at the same time (binding record created by _export_dependency()), resulting in:

IntegrityError: duplicate key value violates unique constraint “magento_product_product_odoo_uniq” DETAIL: Key (backend_id, odoo_id)=(1, 4851) already exists.

In that case, we’ll retry the import just later.

Warning

The unique constraint must be created on the binding record to prevent 2 bindings to be created for the same Magento record.

_export_dependency(relation, binding_model, component_usage='record.exporter', binding_field='magento_bind_ids', binding_extra_vals=None)[source]

Export a dependency. The exporter class is a subclass of MagentoExporter. If a more precise class need to be defined, it can be passed to the exporter_class keyword argument.

Warning

a commit is done at the end of the export of each dependency. The reason for that is that we pushed a record on the backend and we absolutely have to keep its ID.

So you must take care not to modify the Odoo database during an export, excepted when writing back the external ID or eventually to store external data that we have to keep on this side.

You should call this method only at the beginning of the exporter synchronization, in _export_dependencies().

Parameters:
  • relation (odoo.models.BaseModel) – record to export if not already exported
  • binding_model (str | unicode) – name of the binding model for the relation
  • component_usage – ‘usage’ to look for to find the Component to for the export, by default ‘record.exporter’
  • binding_field (str | unicode) – name of the one2many field on a normal record that points to the binding record (default: magento_bind_ids). It is used only when the relation is not a binding but is a normal record.
Binding_extra_vals:
 

In case we want to create a new binding pass extra values for this binding

_export_dependencies()[source]

Export the dependencies for the record

_map_data()[source]

Returns an instance of MapRecord

_validate_create_data(data)[source]

Check if the values to import are correct

Pro-actively check before the Model.create if some fields are missing or invalid

Raise InvalidDataError

_validate_update_data(data)[source]

Check if the values to import are correct

Pro-actively check before the Model.update if some fields are missing or invalid

Raise InvalidDataError

_create_data(map_record, fields=None, **kwargs)[source]

Get the data to pass to _create()

_create(data)[source]

Create the Magento record

_update_data(map_record, fields=None, **kwargs)[source]

Get the data to pass to _update()

_update(data)[source]

Update an Magento record

_module = 'connector_magento'
_run(fields=None)[source]

Flow of the synchronization, implemented in inherited classes

Importer

Importers for Magento.

An import can be skipped if the last sync date is more recent than the last update in Magento.

They should call the bind method if the binder even if the records are already bound, to update the last sync date.

class odoo.addons.connector_magento.components.importer.MagentoImporter(work_context)[source]

Bases: odoo.addons.component.core.AbstractComponent

Base importer for Magento

_name = 'magento.importer'
_inherit = ['base.importer', 'base.magento.connector']
_usage = 'record.importer'
_get_magento_data()[source]

Return the raw Magento data for self.external_id

_before_import()[source]

Hook called before the import, when we have the Magento data

_is_uptodate(binding)[source]

Return True if the import should be skipped because it is already up-to-date in OpenERP

_import_dependency(external_id, binding_model, importer=None, always=False)[source]

Import a dependency.

The importer class is a class or subclass of MagentoImporter. A specific class can be defined.

Parameters:
  • external_id – id of the related binding to import
  • binding_model (str | unicode) – name of the binding model for the relation
  • importer_component (Component) – component to use for import By default: ‘importer’
  • always (boolean) – if True, the record is updated even if it already exists, note that it is still skipped if it has not been modified on Magento since the last update. When False, it will import it only when it does not yet exist.
_import_dependencies()[source]

Import the dependencies for the record

Import of dependencies can be done manually or by calling _import_dependency() for each dependency.

_map_data()[source]

Returns an instance of MapRecord

_validate_data(data)[source]

Check if the values to import are correct

Pro-actively check before the _create or _update if some fields are missing or invalid.

Raise InvalidDataError

_must_skip()[source]

Hook called right after we read the data from the backend.

If the method returns a message giving a reason for the skipping, the import will be interrupted and the message recorded in the job (if the import is called directly by the job, not by dependencies).

If it returns None, the import will continue normally.

Returns:None | str | unicode
_get_binding()[source]
_create_data(map_record, **kwargs)[source]
_create(data)[source]

Create the OpenERP record

_update_data(map_record, **kwargs)[source]
_update(binding, data)[source]

Update an OpenERP record

_after_import(binding)[source]

Hook called at the end of the import

run(external_id, force=False)[source]

Run the synchronization

Parameters:external_id – identifier of the record on Magento
_module = 'connector_magento'
class odoo.addons.connector_magento.components.importer.BatchImporter(work_context)[source]

Bases: odoo.addons.component.core.AbstractComponent

The role of a BatchImporter is to search for a list of items to import, then it can either import them directly or delay the import of each item separately.

_name = 'magento.batch.importer'
_inherit = ['base.importer', 'base.magento.connector']
_usage = 'batch.importer'
run(filters=None)[source]

Run the synchronization

_import_record(external_id)[source]

Import a record directly or delay the import of the record.

Method to implement in sub-classes.

_module = 'connector_magento'
class odoo.addons.connector_magento.components.importer.DirectBatchImporter(work_context)[source]

Bases: odoo.addons.component.core.AbstractComponent

Import the records directly, without delaying the jobs.

_name = 'magento.direct.batch.importer'
_inherit = 'magento.batch.importer'
_import_record(external_id)[source]

Import the record directly

_module = 'connector_magento'
class odoo.addons.connector_magento.components.importer.DelayedBatchImporter(work_context)[source]

Bases: odoo.addons.component.core.AbstractComponent

Delay import of the records

_name = 'magento.delayed.batch.importer'
_inherit = 'magento.batch.importer'
_import_record(external_id, job_options=None, **kwargs)[source]

Delay the import of the records

_module = 'connector_magento'
class odoo.addons.connector_magento.components.importer.SimpleRecordImporter(work_context)[source]

Bases: odoo.addons.component.core.Component

Import one Magento Website

_name = 'magento.simple.record.importer'
_inherit = 'magento.importer'
_apply_on = ['magento.res.partner.category']
_module = 'connector_magento'
class odoo.addons.connector_magento.components.importer.TranslationImporter(work_context)[source]

Bases: odoo.addons.component.core.Component

Import translations for a record.

Usually called from importers, in _after_import. For instance from the products and products’ categories importers.

_name = 'magento.translation.importer'
_inherit = 'magento.importer'
_usage = 'translation.importer'
_get_magento_data(storeview_id=None)[source]

Return the raw Magento data for self.external_id

run(external_id, binding, mapper=None)[source]
_module = 'connector_magento'

Mapper

class odoo.addons.connector_magento.components.mapper.MagentoImportMapper(work_context)[source]

Bases: odoo.addons.component.core.AbstractComponent

_name = 'magento.import.mapper'
_inherit = ['base.magento.connector', 'base.import.mapper']
_usage = 'import.mapper'
_module = 'connector_magento'
class odoo.addons.connector_magento.components.mapper.MagentoExportMapper(work_context)[source]

Bases: odoo.addons.component.core.AbstractComponent

_name = 'magento.export.mapper'
_inherit = ['base.magento.connector', 'base.export.mapper']
_usage = 'export.mapper'
_module = 'connector_magento'
odoo.addons.connector_magento.components.mapper.normalize_datetime(field)[source]

Change a invalid date which comes from Magento, if no real date is set to null for correct import to OpenERP