skilld-labs / go-odoo

Golang wrapper for Odoo API
Apache License 2.0
86 stars 62 forks source link

Error on update operations with datetime fields #32

Closed bhuisgen closed 2 years ago

bhuisgen commented 2 years ago

I have generated my models for odoo v15. Create and read operations are OK, but there is a problem with update operation about datetime fields:

    id, err := c.CreateResPartner(&odoo.ResPartner{
        Name: odoo.NewString("test"),
    })
    if err != nil {
        log.Print(err)
        return
    }

    resPartner, err := c.GetResPartner(id)
    if err != nil {
        log.Print(err)
        return
    }

    resPartner.Comment = odoo.NewString("test")

    // FIXME update error
    err = c.UpdateResPartner(resPartner)
    if err != nil {
        log.Print(err)
        return
    }
2022/08/10 12:21:51 Fault(1): Traceback (most recent call last):
  File "/usr/local/odoo/odoo/addons/base/controllers/rpc.py", line 93, in xmlrpc_2
    response = self._xmlrpc(service)
  File "/usr/local/odoo/odoo/addons/base/controllers/rpc.py", line 73, in _xmlrpc
    result = dispatch_rpc(service, method, params)
  File "/usr/local/odoo/odoo/http.py", line 141, in dispatch_rpc
    result = dispatch(method, params)
  File "/usr/local/odoo/odoo/service/model.py", line 41, in dispatch
    res = fn(db, uid, *params)
  File "/usr/local/odoo/odoo/service/model.py", line 169, in execute_kw
    return execute(db, uid, obj, method, *args, **kw or {})
  File "/usr/local/odoo/odoo/service/model.py", line 94, in wrapper
    return f(dbname, *args, **kwargs)
  File "/usr/local/odoo/odoo/service/model.py", line 176, in execute
    res = execute_cr(cr, uid, obj, method, *args, **kw)
  File "/usr/local/odoo/odoo/service/model.py", line 160, in execute_cr
    result = odoo.api.call_kw(recs, method, args, kw)
  File "/usr/local/odoo/odoo/api.py", line 464, in call_kw
    result = _call_kw_multi(method, model, args, kwargs)
  File "/usr/local/odoo/odoo/api.py", line 451, in _call_kw_multi
    result = method(recs, *args, **kwargs)
  File "/usr/local/odoo/addons/base_vat/models/res_partner.py", line 616, in write
    return super(ResPartner, self).write(values)
  File "/usr/local/odoo/addons/snailmail/models/res_partner.py", line 26, in write
    return super(ResPartner, self).write(vals)
  File "/usr/local/odoo/addons/partner_autocomplete/models/res_partner.py", line 179, in write
    res = super(ResPartner, self).write(values)
  File "/usr/local/odoo/odoo/addons/base/models/res_partner.py", line 603, in write
    result = result and super(Partner, self).write(vals)
  File "/usr/local/odoo/addons/mail/models/mail_activity_mixin.py", line 243, in write
    return super(MailActivityMixin, self).write(vals)
  File "/usr/local/odoo/addons/mail/models/mail_thread.py", line 323, in write
    result = super(MailThread, self).write(values)
  File "/usr/local/odoo/odoo/models.py", line 3858, in write
    field.write(self, value)
  File "/usr/local/odoo/odoo/fields.py", line 1015, in write
    cache_value = self.convert_to_cache(value, records)
  File "/usr/local/odoo/odoo/fields.py", line 2037, in convert_to_cache
    return self.to_datetime(value)
  File "/usr/local/odoo/odoo/fields.py", line 2017, in to_datetime
    return datetime.strptime(value, DATETIME_FORMAT[:len(value)-2])
TypeError: object of type 'DateTime' has no len()

Any suggestion about this ?

bhuisgen commented 2 years ago

I did a small fix into the function convertFromStaticToDynamicValue in conversion.go:

func convertFromStaticToDynamicValue(staticValue interface{}) interface{} {
    switch sv := staticValue.(type) {
    case *Time:
        // v = sv.v
        v = sv.v.String()

Updates are working now. Maybe a PR in few days :)

ahuret commented 2 years ago

Hey @bhuisgen :wave: Thank you for the catch and nice debugging ! If the fix works well for you, that's fine ! But I think the fundamental issue is about a lack of decoupling between the different "time" odoo types: datetime and date, we probably need two different types with their own encoding/decoding logic, I still need to go deeper to be sure, feel free to give your opinion. I'll keep you in touch !

ahuret commented 2 years ago

resolved in https://github.com/skilld-labs/go-odoo/commit/96d00b8e69e0e66cfc9dabb77167ca67a13d990f !

As I can see, odoo can receive request with the same format for both date and datetime types. However, those two types are different and not stored the same way in the database, we should handle that correctly.

I planned to work on a future release with:

thanks again for the report, feel free to give your opinion / needs / wishes for this library to improve :D