def __init__(self, fn):\r
self._callable = fn\r
\r
- def decorate(self, fn, needs_origin):\r
+ def _decorate(self, fn, needs_origin):\r
origin = self._callable\r
\r
if needs_origin:\r
else:\r
self._callable = lambda *args, **kwargs: fn(*args, **kwargs)\r
\r
- def __call__(self, *args, **kwargs):\r
- return self._callable(*args, **kwargs)\r
+ def _decorate_method(self, fn, needs_origin):\r
+ origin = self._callable\r
\r
+ if needs_origin:\r
+ self._callable = lambda inst, *args, **kwargs: fn(inst, origin, *args, **kwargs)\r
+ else:\r
+ self._callable = lambda inst, *args, **kwargs: fn(inst, *args, **kwargs)\r
\r
-def decoratable(fn):\r
- return DecoratableObject(fn)\r
\r
-def decoratable_method(fn):\r
- obj = DecoratableObject(fn)\r
- def decorated(self, *args, **kwargs):\r
- return obj(self, *args, **kwargs)\r
+ def __call__(self, *args, **kwargs):\r
+ return self._callable(*args, **kwargs)\r
\r
- decorated.__obj = obj\r
- return decorated\r
\r
-decoratable.method = decoratable_method\r
+def _decorate_method(origin, needs_origin):\r
+ if not hasattr(origin, '_decoratable_obj'):\r
+ name = origin.__name__\r
+ cls = origin.im_class\r
\r
-def decorate(origin, needs_origin=True):\r
- if not isinstance(origin, DecoratableObject):\r
- if hasattr(origin, '__obj'):\r
- def decorator(fn):\r
- origin.__obj.decorate(fn, needs_origin)\r
- return origin\r
- return decorator\r
+ decoratable = DecoratableObject(origin)\r
+\r
+ def decoratable_method(self, *args, **kwargs):\r
+ return decoratable(self, *args, **kwargs)\r
\r
- raise TypeError('Not a decoratable function: %s' % origin.__name__)\r
+ decoratable_method._decoratable_obj = decoratable\r
+ setattr(cls, name, decoratable_method)\r
+ else:\r
+ decoratable = origin._decoratable_obj\r
\r
def decorator(fn):\r
- origin.decorate(fn, needs_origin)\r
- return origin\r
+ decoratable._decorate_method(fn, needs_origin)\r
\r
return decorator\r
\r
+def _decorate_function(origin, needs_origin):\r
+ if not isinstance(origin, DecoratableObject):\r
+ mod = inspect.getmodule(origin)\r
\r
-def decorate_all(module):\r
- [setattr(module, n, decoratable(f)) for n, f in\r
- [(n, getattr(module, n)) for n in dir(module)]\r
- if (callable(f)) and (not inspect.isclass(f)) and (f.__module__ == module.__name__)]\r
-\r
-\r
+ name = origin.__name__\r
+ origin = DecoratableObject(origin)\r
+ setattr(mod, name, DecoratableObject(origin))\r
\r
+ def decorator(fn):\r
+ origin._decorate(fn, needs_origin)\r
\r
+ return decorator\r
\r
\r
+def decorate(origin, needs_origin=True):\r
+ if inspect.ismethod(origin):\r
+ return _decorate_method(origin, needs_origin)\r
\r
+ if inspect.isfunction(origin):\r
+ return _decorate_function(origin, needs_origin)
\ No newline at end of file
from django.core.exceptions import PermissionDenied
from forum.actions import AskAction, AnswerAction, ReviseAction, RollbackAction, RetagAction
-from forum.modules.decorators import decoratable
from forum.forms import *
from forum.models import *
from forum.forms import get_next_url
return HttpResponse(result, mimetype="application/xml")
-@decoratable
def ask(request):
if request.POST and "text" in request.POST:
form = AskForm(request.POST, user=request.user)
'form': form,
}, context_instance=RequestContext(request))
-@decoratable
def answer(request, id):
question = get_object_or_404(Question, id=id)
if request.POST: