Merge lp:~camptocamp/openerp-connector/7.0-connector-closure-functions into lp:~openerp-connector-core-editors/openerp-connector/7.0
Status: | Merged |
---|---|
Approved by: | Guewen Baconnier @ Camptocamp |
Approved revision: | 603 |
Merged at revision: | 604 |
Proposed branch: | lp:~camptocamp/openerp-connector/7.0-connector-closure-functions |
Merge into: | lp:~openerp-connector-core-editors/openerp-connector/7.0 |
Diff against target: |
566 lines (+393/-30) 6 files modified
connector/CHANGES.rst (+3/-0) connector/connector.py (+5/-1) connector/doc/guides/concepts.rst (+4/-8) connector/tests/test_backend.py (+1/-1) connector/tests/test_mapper.py (+118/-0) connector/unit/mapper.py (+262/-20) |
To merge this branch: | bzr merge lp:~camptocamp/openerp-connector/7.0-connector-closure-functions |
Related bugs: | |
Related blueprints: |
Functions in direct bindings
(Undefined)
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Nicolas Bessi - Camptocamp (community) | Needs Information | ||
Review via email: mp+193389@code.launchpad.net |
Commit message
[ADD] Added the possibility to use modifiers functions in the 'direct' mappings.
It allows to reduce the code in the @mappings methods as common modifiers can be
used. The change also include 3 modifiers: one for converting the type of the source field, one for getting the binding or openerp id of a backend field, one for getting the backend id of an openerp id or binding id.
Description of the change
Implements the blueprint: https:/
Allows to use Modifiers in the direct mappings.
In the mappings, we have the possibility to defined 'direct' mappings. Example:
direct = [('source_field', 'target_field')]
Integers, chars, etc. are copied. IDs of m2o relations are passed to their Binder which returns the external ID from the binding ID (or conversely returns the binding ID from the external ID).
I observed that we often need to apply a similar transformation on the source values, noted examples:
- A backend sends '0000-00-00' for empty dates, we have to replace that by None
- A backend sends dates in ISO 8601 format, we need to convert them to UTC then to a naive timestamp
- A backend sends float as integer (float*100)
- We want a default value when a value is empty
- And in fact the most common operation is to find an external ID for a local ID and conversely, it was automatic but only for IDs of bindings
We could already do the changes using @mapping methods, but it was very repetitive.
That's why I introduced the "modifiers". Example:
direct [(iso8601(
They also resolve an issue with the m2o: they were implicitly trying to search a Binder for the relation of the m2o, so not working if the relation was a "normal record". Now, I recommend to explicit the operation:
# partner_id relation's 'res.partner' (normal record)
# mag_partner_id relation's 'magento.
# s_partner_id is the id on the backend
direct = [(backend_
Example of implementation:
def a_function(field):
''' ``field`` is the name of the source field '''
def transform(self, record, to_attr):
''' self is the current Mapper,
return record[field]
return transform
direct = [
]
Hello,
Good idea, but if you apply a Higher-order function to your mapping is still a "direct"
mapping ? I find it a little bit confusing, but I get the finality of the change.
Maybe we may add a new "middleware or what else" class attribute that will allows to wrap any of the transformation (direct or not) with a wrapping function. Like in ring/wsgi etc webstack handler.
Regards
Nicolas