Connectors mailing list archives

connectors@odoo-community.org

Avatar

Re: mapping issue with default 'options' attribute

by
Guewen Baconnier
- 18/04/2019 09:03:56
Hello,

It's not difficult to help you without seeing your code. It would be helpful to see the context around 'map_data' (class, other methods, how it's called, ...).

Guewen

On Mon, Apr 8, 2019 at 3:51 PM cyrille <cyrille@lalibrerie.org> wrote:

Hello,

When I try to test this code :


def map_data(self, trello_data):
    map_record = self.map_record(record=trello_data)
    return self._apply(map_record)


It gives me the following error :


ERROR
======================================================================
ERROR: test_read_projects (odoo.addons.connector_trello.tests.test_trello_project_backend.TestTrelloProjectBackend)
Traceback (most recent call last):
`   File "/usr/lib/python3/dist-packages/mock/mock.py", line 1305, in patched
`     return func(*args, **keywargs)
`   File "/home/cyrille/odoo/addons-dev/connector_trello/tests/test_trello_project_backend.py", line 47, in test_read_projects
`     trello_backend.read_projects(mock_trello_client)
`   File "/home/cyrille/odoo/addons-dev/connector_trello/models/backends/trello_project_backend.py", line 35, in read_projects
`     mapped_data = mapper.map_data(project_data)
`   File "/home/cyrille/odoo/addons-dev/connector_trello/components/mappers/import_mappers/trello_abstract_import_mapper.py", line 20, in map_data
`     return self._apply(map_record)
`   File "/home/cyrille/odoo/addons-ext/connector/components/mapper.py", line 754, in _apply
`     return self._apply_with_options(map_record)
`   File "/home/cyrille/odoo/addons-ext/connector/components/mapper.py", line 771, in _apply_with_options
`     fields = self.options.fields
` AttributeError: 'dict' object has no attribute 'fields'


According the documentation, the 'options' dict object is automatically built by a function from the framework. Here after, I copied the source code of the _apply method from the documentation :


def _apply(self, map_record, options=None):
        """ Apply the mappings on a :py:class:`MapRecord`

        :param map_record: source record to convert
        :type map_record: :py:class:`MapRecord`

        """
        if options is None:
            options = {}
        with self._mapping_options(options):
            return self._apply_with_options(map_record)

    def _apply_with_options(self, map_record):
        """ Apply the mappings on a :py:class:`MapRecord` with
        contextual options (the ``options`` given in
        :py:meth:`MapRecord.values()` are accessible in
        ``self.options``)

        :param map_record: source record to convert
        :type map_record: :py:class:`MapRecord`

        """
        assert self.options is not None, (
            "options should be defined with '_mapping_options'")
        _logger.debug('converting record %s to model %s',
                      map_record.source, self.model)

        fields = self.options.fields
        for_create = self.options.for_create
        result = {}
        for from_attr, to_attr in self.direct:
            if isinstance(from_attr, collections.Callable):
                attr_name = self._direct_source_field_name(from_attr)
            else:
                attr_name = from_attr

            if (not fields or attr_name in fields):
                value = self._map_direct(map_record.source,
                                         from_attr,
                                         to_attr)
                result[to_attr] = value

        for meth, definition in self.map_methods:
            mapping_changed_by = definition.changed_by
            if (not fields or not mapping_changed_by or
                    mapping_changed_by.intersection(fields)):
                if definition.only_create and not for_create:
                    continue
                values = meth(map_record.source)
                if not values:
                    continue
                if not isinstance(values, dict):
                    raise ValueError('%s: invalid return value for the '
                                     'mapping method %s' % (values, meth))
                result.update(values)

        for from_attr, to_attr, model_name in self.children:
            if (not fields or from_attr in fields):
                result[to_attr] = self._map_child(map_record, from_attr,
                                                  to_attr, model_name)

        return self.finalize(map_record, result)


Therefore, I don’t understand why the default behavior is causing troubles. Did I miss something ?

Thanks for your help,

Sincerely,


Cyrille (La LibreRie)

lalibrerie-solidaire.org

Python/Odoo developer

_______________________________________________
Mailing-List: https://odoo-community.org/groups/connectors-30
Post to: mailto:connectors@odoo-community.org
Unsubscribe: https://odoo-community.org/groups?unsubscribe

Reference