Skip to content

Instantly share code, notes, and snippets.

@leorochael
Last active February 3, 2017 18:18
Show Gist options
  • Save leorochael/7096f437668bd9280170b51c2449f6ec to your computer and use it in GitHub Desktop.
Save leorochael/7096f437668bd9280170b51c2449f6ec to your computer and use it in GitHub Desktop.
Resolver of the printable representation of Odoo recordsets
# Paste this in ipython_odoo use it like this:
# r.res.users(1, 2, 3,)
#
# the "res.users(1, 2, 3,)" above is the printable representation of a recordset, sometimes output in logging.
class Resolver(object):
_completables = None
def __init__(self, env, name_parts=()):
self._env = env
self._name_parts = name_parts
self._name = '.'.join(name_parts)
def __getattr__(self, name):
if name.endswith('_'): name = name[:-1]
return self.__class__(self._env, self._name_parts + (name,))
def __call__(self, *args):
return self._env[self._name].browse(args)
def _get_prefixed_model_names(self):
if self._completables is None:
prefix = self._name + '.' if self._name else ''
prefix_len = len(prefix)
models_with_prefix = (name for name in self._env.registry.keys() if name.startswith(prefix))
def next_segment(name):
return name[prefix_len:].split('.')[0]
self._completables = set(next_segment(name) for name in models_with_prefix)
return list(self._completables)
def __dir__(self):
result = sorted(dir(Resolver) + self.__dict__.keys() + self._get_prefixed_model_names())
return result
def __repr__(self):
return 'Resolver(session.env, %s)' % (self._name_parts,)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment