﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
13879	method_decorator doesn't supports decorators with arguments	Marinho Brandão	Wes P.	"I tried use '''django.utils.decorators.method_decorator''' to support decorator '''permission_required''' and figured out it's not supporting decorators passing arguments like it does:

@permission_required(something_here)

Instead its code waits for you just pass a function as an only one argument, like this:

@login_required

I made my own version of it, that's below:

{{{
from functools import wraps, update_wrapper

def method_decorator(decorator):
    """"""Converts a function decorator into a method decorator.
    
    This works properly for both: decorators with arguments and without them. The Django's version
    of this function just supports decorators with no arguments.""""""

    # For simple decorators, like @login_required, without arguments
    def _dec(func):
        def _wrapper(self, *args, **kwargs):
            def bound_func(*args2, **kwargs2):
                return func(self, *args2, **kwargs2)
            return decorator(bound_func)(*args, **kwargs)
        return wraps(func)(_wrapper)

    # Called everytime
    def _args(*argsx, **kwargsx):
        # Detect a simple decorator and call _dec for it
        if len(argsx) == 1 and callable(argsx[0]) and not kwargsx:
            return _dec(argsx[0])

        # Used for decorators with arguments, like @permission_required('something')
        def _dec2(func):
            def _wrapper(self, *args, **kwargs):
                def bound_func(*args2, **kwargs2):
                    return func(self, *args2, **kwargs2)
                return decorator(*argsx, **kwargsx)(bound_func)(*args, **kwargs)
            return wraps(func)(_wrapper)
        return _dec2

    update_wrapper(_args, decorator)
    # Change the name to aid debugging.
    _args.__name__ = 'method_decorator(%s)' % decorator.__name__
    return _args
}}}

If necessary I can make a patch and attach to this ticket."	New feature	closed	Utilities	1.2	Normal	wontfix	sprintnov13, decorators	Łukasz Rekucki douglas.russell@… toracle@…	Accepted	1	0	0	0	0	0
