Skip to content

Instantly share code, notes, and snippets.

@aliomattux
Created October 27, 2016 20:02
Show Gist options
  • Save aliomattux/35b82e6eaa9bd0080913dc1127ab57a9 to your computer and use it in GitHub Desktop.
Save aliomattux/35b82e6eaa9bd0080913dc1127ab57a9 to your computer and use it in GitHub Desktop.
#Decorator function to create global method (Extend the object service)
#add this function outside of any class!
def extend(class_to_extend):
"""
Decorator to use to extend a existing class with a new method
Example :
@extend(osv.osv)
def new_method(self, *args, **kwargs):
print 'I am in the new_method', self._name
return True
Will add the method new_method to the class osv.osv
"""
def decorator(func):
if hasattr(class_to_extend, func.func_name):
raise osv.except_osv(_("Developer Error"),
_("You can extend the class %s with the method %s.",
"This method already exists. Use the decorator 'replace' instead"))
setattr(class_to_extend, func.func_name, func)
return class_to_extend
return decorator
@extend(osv.osv)
def your_method(self, cr, uid, filters, vals):
#do stuff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment