Created
November 9, 2011 15:11
-
-
Save nbessi/1351717 to your computer and use it in GitHub Desktop.
Decorator with boilerplate for OSV methods.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Thanks to Florent for the function. | |
#Really useful. | |
def osv_method(f): | |
"""Decorator with boilerplate for OSV methods.""" | |
args, varargs, kwargs, defaults = getargspec(f) | |
len_argspec = len(args) - 4 | |
# Sanity check: no *args or **kwargs parameters | |
assert varargs is kwargs is None | |
# Sanity check: last argument is 'context' | |
assert len_argspec > 0 and args[-1] == 'context' | |
del args, varargs, kwargs, defaults | |
@wraps(f) | |
def wrapper(self, cr, uid, ids, *args, **kwargs): | |
if isinstance(ids, (int, long)): | |
ids = [ids] | |
elif not ids: | |
ids = [] | |
if len(args) == len_argspec: | |
# 'context' is not a keyword argument | |
if not kwargs and args[-1] is None: | |
args = args[:-1] | |
kwargs['context'] = {} | |
elif kwargs.get('context') is None: | |
kwargs['context'] = {} | |
return f(self, cr, uid, ids, *args, **kwargs) | |
return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment