Comment 6 for bug 1210471

Revision history for this message
Guewen Baconnier @ Camptocamp (gbaconnier-c2c) wrote : Re: [Bug 1210471] Re: datatime should be NULL not '000-00-00 00:00:00'

Le 9 août 2013 17:35, "Guewen Baconnier @ Camptocamp" <
<email address hidden>> a écrit :
>
> Thanks for the patch!
> I think that's the responsibility of the mappers and not of the
synchronizer. Although, not all models have those fields.
>
> In the mappers, these fields are actually in direct mappings:
>
> direct = [
> ('created_at', 'created_at'),
> ('updated_at', 'updated_at'),
> ]
>
> The simple but a bit tedious solution is to transform all of them in
> method mappings:
>
> @mapping
> def created_at(self, record):
> return normalize_date(record['created_at'])
>
> Where normalize_date would be a simple helper which clean the invalid
> dates:
>
> def normalize_date(value):
> if value == '0000-00-00 00:00:00':
> return None
> return value
>
> The solution I would prefer is to add the possibility (directly in the
`connector` module) to give a transformation method on the direct mappings.
> Either in this form:
>
> direct = [
> ('created_at', normalize_date('created_at')),
> ('updated_at', normalize_date('updated_at')),
> ]
>
> (using a closure like:
> def normalize_date(field):
> def transform(self, record):
> if record[field] == '0000-00-00 00:00:00':
> return None
> return record[field]
> return transform
> )
>

With a bit more of thinking that's definitely my preferred form as we can
write a generic converter for types or have special arguments that the
3-items tuple below would not permit. Example

    direct = [
            ('created_at', normalize_date('created_at')),
            ('updated_at', normalize_date('updated_at')),
             ('myfield', convert('origin_field', float)),
    ]

> Or in this form:
>
> direct = [
> ('created_at', 'created_at', normalize_date),
> ('updated_at', 'updated_at', normalize_date),
> ]
>
> The latter form would allow to use builtin types like str but is maybe
> less readable.
>