OCA / odoorpc

Python module to pilot your Odoo servers through JSON-RPC.
http://pythonhosted.org/OdooRPC/
GNU Lesser General Public License v3.0
234 stars 124 forks source link

[IMP] allow context manager #82

Closed yajo closed 1 year ago

yajo commented 1 year ago

The simplest possible way to implement an automatic logout with a context manager is to rely on contextlib.closing. The only requirement is to have a close function. In this case, just an alias for logout.

With this patch, we're able now to wrap the logged-in odoo instance with that function and get automatic logout.

See the provided test for usage example.

yajo commented 1 year ago

All attended

sebalix commented 1 year ago

Merging, I'll cancel the jobs for now as I'm fixing them. Need to migrate to GH actions too later!

sebalix commented 1 year ago

Doctest was failing because of this new close attribute. I can't figure out why but I fixed it by creating a well defined close method here: https://github.com/OCA/odoorpc/pull/84/commits/6b10bab1ce1de5d4ab432031bccbc9c241f63dd6

The traceback of the doctest:

Document: ref_odoo
------------------
**********************************************************************
File "../../odoorpc/odoo.py", line ?, in default
Failed example:
    Partner = odoo.env['res.partner']
Exception raised:
    Traceback (most recent call last):
      File "/usr/lib/python3.8/doctest.py", line 1336, in __run
        exec(compile(example.source, filename, "single",
      File "<doctest default[0]>", line 1, in <module>
        Partner = odoo.env['res.partner']
      File "/home/salix/dev/OCA/odoorpc/odoorpc/odoo.py", line 197, in env
        self._check_logged_user()
      File "/home/salix/dev/OCA/odoorpc/odoorpc/odoo.py", line 319, in _check_logged_user
        raise error.InternalError("Login required")
    odoorpc.error.InternalError: Login required