class ValidationError(object):
pass
class Foo(object):
def clean(self):
print "hey"
if self.cleaned_data["x"] < self.cleaned_data["y"]:
raise ValidationError
class Bar(object):
def clean(self):
print "hoo"
if self.cleaned_data["x"] < self.cleaned_data["z"]:
raise ValidationError
class FooBar(object):
def clean(self):
pass
def deco_from_class(source):
get_real = lambda m : m.__func__ if hasattr(m, "__func__") else m.im_func
def deco(cls):
if not hasattr(cls, "clean"):
cls.clean = get_real(source.clean)
else:
def wrapped(original):
def _wrapped(self):
val = original(self)
get_real(source.clean)(self)
return val
return _wrapped
cls.clean = wrapped(cls.clean)
return cls
return deco
@deco_from_class(Bar)
@deco_from_class(Foo)
class FooBar(object):
cleaned_data = dict(x=10, y=5, z=1)
FooBar().clean()