Callbacks module

A callback is a field’s value pre-process. It can be used for various treatments, such as striping, splitting, injecting data relative to the initial value, etc.

Note

Import the callback method and call it properly into the callbacks argument of a Field instanciation.

Concretely, it’s such as simple as that.

from csv2oerp import callbacks as cb

Model('res.parter_address', [
    Field('partner_id', columns=[25], callbacks=[cb.get_id('res.partner', ['name'])]),
]

Creating a callback

class csv2oerp.callbacks.callback_model[source]

Model for the specifics callbacks class signature.

Parameters:
  • session – Current Session
  • model – Current Model
  • field – Current Field
  • line – Current line number

You can use a closure method to add specific arguments to your callback call.

def my_callback(*args, **kwargs):
    """ Add some arguments to your callback

    """
    def _callback(session, model, field, line):
        result = ""
        # Some treatments with arguments in scope
        return result

    return _callback

Understanding the process

Here is the position of callbacks in the mapping hierarchy (Depending of your configuration, of course).

------   +-----+-----+---+--------+--------+---+-------+--------------+
|C       |name |phone|fax|street  |street2 |zip|country|...           |
|S       |-----|-----|---|--------|--------|---|-------|--------------|
|V       |...  |...  |...|...     |...     |...|...    |...           |
------   +-----+-----+---+--------+--------+---+-------+--------------+
|        |  1  |  2  | 3 |   4    |   5    | 6 |   7   |...           |
|        +--+--+--+--+-+-+----+---+--+-----++--+--+----+--------------+
|C          |     |    |      |      |      |     |
|S          |     |    |      |      |      |     |
|V       +------------------------------------------------------------+
|2       |                   GLOBAL PRE-PROCESSES                     |
|O       +------------------------------------------------------------+
|E          |     |    |      |      |      |     |
|R          |     |    |      |      |      |     |
|P          |     |    |      |      +--+   |     |
|           | +-------------+ |         |   |  +-------------+
|B          | |  callbacks  | +-----+   |   |  |  callbacks  |
|I          | +-------------+       |   |   |  +-------------+
|N          |     |    |            |   |   |    | |
|D          |     |    |            |   |   |    | |
|S          |     |    +--------+   |   |   |    | |
|           |     +---------+   |   |   |   |    | |
|           +---------+-+   |   |   |   |   |    | |
------                | |   |   |   |   |   |    | |
|                     | |   |   |   |   |   |    | |   +-----------+
|                     | |   |   |   |   |   |    | |   |res.country|<-+
|O                    | |   |   |   |   |   |    | |   |-----------|  |
|P                    | |   |   |   |   |   |    | +-->|name       |  |
|E                    | |   |   |   |   |   |    +---->|code       |  |
|N                    | |   |   |   |   |   |          +-----------+  |
|E      +-----------+ | |   |   |   |   |   |   +-------------------+ |
|R      |res.partner| | |   |   |   |   |   |   |res.partner.address| |
|P      |-----------| | |   |   |   |   |   |   |-------------------| |
|       |       name|<+ +   |   |   |   |   +-->|zip                | |
|M      |           |   |   |   |   |   +------>|street2            | |
|O      |           |   |   |   |   +---------->|street             | |
|D      |           |   |   |   +-------------->|fax                | |
|E      |           |   |   +------------------>|phone              | |
|L      |           |   +---------------------->|name               | |
|S      |  addresses|-------------------------->|partner_id         | |
|       |           |                           |         country_id|-+
|       |           |                           |                   |
-----   +-----------+                           +-------------------+

Preconfigured callbacks

csv2oerp.callbacks.preconfigured.capitalize(*args)[source]

Return a capitalized string.

Returns:csv2oerp.callbaks.callback_model

Note

This function is a rewriting of string.capitalize, same signature

csv2oerp.callbacks.preconfigured.city_cedex(res=None, cedex_code=False)[source]

Check if a cedex code is in the town name. Return the relative string to res argument

Parameters:
  • res (str) – Relative string returned
  • cedex_code (str) – Returns only code or full code (‘6’, ‘CEDEX 6’)
Returns:

csv2oerp.callbaks.callback_model

csv2oerp.callbacks.preconfigured.from_binary(*args, **kwargs)[source]

Return a string.

Returns:csv2oerp.callbaks.callback_model

Note

This function is a rewriting of base64, same signature

csv2oerp.callbacks.preconfigured.get_field(model, fields, field, op=u'&')[source]

Return model‘s field field which fields‘s value matched value

Parameters:
  • model (str) – The name of the model to search into
  • fields (str) – The name of the model’s field
  • field (str) – The desire field’s value to returned
  • value (str) – The value to compare to
Returns:

csv2oerp.callbaks.callback_model

csv2oerp.callbacks.preconfigured.get_fields(model, fields, op=u'&')[source]

Return model instance which one or more fields matched column value

Parameters:
  • model (str) – The name of the model to search into
  • fields (str) – The name of the model’s field
  • value (str) – The value to compare to
Returns:

csv2oerp.callbaks.callback_model

csv2oerp.callbacks.preconfigured.get_id(model, fields, op=u'&')[source]

Return model which one or more fields matched column value

Parameters:
  • model (str) – The name of the model to search into
  • field (str) – The name of the model’s field
  • value (str) – The value to compare to
Returns:

csv2oerp.callbaks.callback_model

csv2oerp.callbacks.preconfigured.get_ids(model, fields, op=u'&')[source]

Return model which one or more fields matched column value

Parameters:
  • model (str) – The name of the model to search into
  • field (str) – The name of the model’s field
  • value (str) – The value to compare to
Returns:

csv2oerp.callbaks.callback_model

csv2oerp.callbacks.preconfigured.get_objects(model, fields, op=u'&')[source]

Return model instance which fields‘s value matched value

Parameters:
  • model (str) – The name of the model to search into
  • fields (str) – The name of the model’s field
  • value (str) – The value to compare to
Returns:

csv2oerp.callbaks.callback_model

csv2oerp.callbacks.preconfigured.get_phones(type, prefix=None)[source]

Returns numbers contained in the columns values. Numbers must be in a french form such as 0123456789 or ‘+33123456789’ (No matters for spaces). Any other numbers must not be in the columns values.

Parameters:
  • type (str) – The format to returns
  • prefix (str) – The prefix to insert
Returns:

csv2oerp.callbaks.callback_model

Note

Example of a complex working string:

"The number of Mr SMITH is 0123456789, his wife 33 234 567 894, and their son 06 24 568495"

will returns independently of what type but with prefix (+33):

dict = {
    '(field name)': ['(+33) 1 23 45 67 89', '(+33) 2 34 56 78 94'],
    'mobile': ['(+33) 6 24 56 84 95'],
    'all': ['(+33) 1 23 45 67 89', '(+33) 2 34 56 78 94', '(+33) 6 24 56 84 95'],
    'value': "The number of Mr SMITH is 02XXXXXXXX, his wife 33 2XX XXXXXX, and their son 06 XX XX XX XX XX"
}
csv2oerp.callbacks.preconfigured.get_value_from_table(csv_file, col_referer=0, col_return=1, separator=u', ', quote=u'"', header=True)[source]

Search for an equivalence between value from column col_referer index and and value from args[3], and return value from column col_return index at the same line as matching pair col_referer values.

Parameters:
  • csv_file (str) – The name of the CSV file to search into
  • col_referer (int) – The number of the column to compare to args[3]
  • col_return (int) – he number of the column to return if matches
  • separator (str) – The CSV separator
  • quote (str) – The CSV quote
  • header (bool) – CSV file has header
Returns:

csv2oerp.callbaks.callback_model

Note

This function is the csv2oerp.callbacks.search_csv() interface for callback call

csv2oerp.callbacks.preconfigured.lower(*args, **kwargs)[source]

Return a lowered string.

Returns:csv2oerp.callbaks.callback_model

Note

This function is a rewriting of string.lower, same signature

>>> 'address': Column(3, lower())
Returns:callback_model
csv2oerp.callbacks.preconfigured.strip(*args)[source]

Return a stripped string.

Returns:csv2oerp.callbaks.callback_model

Note

This function is a rewriting of string.strip, same signature

csv2oerp.callbacks.preconfigured.strip_accents(normalize=u'NFD', uce=u'Mn')[source]

Return a accents stripped string

Parameters:
  • normalize (str) – Normalize code
  • uce (str) – Unicode category exclusion
Returns:

csv2oerp.callbaks.callback_model

Note

This function is a rewriting of unicodedata.normalize

csv2oerp.callbacks.preconfigured.to_binary(*args, **kwargs)[source]

Return a binary.

Returns:csv2oerp.callbaks.callback_model

Note

This function is a rewriting of base64, same signature

csv2oerp.callbacks.preconfigured.to_boolean(true=u'1', false=u'0', none='')[source]

Return a boolean depending from the input value.

Parameters:
  • mode (str) – First char will be used if value is True and second if False
  • true_values (str) – The name of the model to search into
  • false_values (str) – The name of the model’s field
Returns:

csv2oerp.callbaks.callback_model

csv2oerp.callbacks.preconfigured.to_date(formatted=u'%Y-%m-%d')[source]

Check for a valid date and convert if needed before returning the value

Parameters:formatted (str) – Format of this date
Returns:csv2oerp.callbaks.callback_model
csv2oerp.callbacks.preconfigured.upper(*args)[source]

Return a uppered string.

Returns:csv2oerp.callbaks.callback_model

Note

This function is a rewriting of string.upper, same signature

csv2oerp.callbacks.preconfigured.zfill(*args)[source]

Return a zfilled string.

Returns:csv2oerp.callbaks.callback_model

Note

This function is a rewriting of string.zfill, same signature

Table Of Contents

Previous topic

Command line Interface

Next topic

Some examples

This Page