09-02-08, Chris <[EMAIL PROTECTED]> napisał(a):
>
(cut!)
> I think you get the AssertionError because the authorize function
> accepts arguments.  In that case, I believe it is called a decorator
> factory and you need to call it like @authorize() with parenthesis.
> But obviously, I'm not an expert :)
>
> One other question/comment, it seems a class based decorator is not
> doable on a controller method.
>
> class deco(object):
>     def __init__(self, func):
>         self.func = func
>     def __call__(self, *args, **kwargs):
>         return self.func(*args, **kwargs)
>
> @deco
> def myaction(self):
>   return 'results'
>
> This results in:
> <type 'exceptions.NotImplementedError'>: Action u'myaction' is not
> implemented
>
> THis happens because _dispatch_call in pylons.controllers.core, check
> the type of the func and expects it to be of type 'instancemethod'.
> But a class based decorator will be of some 'class' type, even though
> it is still callable.  Expected, bug?

I didn't test it, but in your example above class deco seems to be a
decorator factory
(because __call__ is an instance method, not a class method, and
calling a class always returns a new instance of this class).
So you should probably call it like this:

class deco(object):
    def __init__(self, func):
        self.func = func
    def __call__(self, *args, **kwargs):
        return self.func(*args, **kwargs)

@deco()
def myaction(self):
  return 'results'

As I said, I haven't tested it :-)


-- 
Marek Stępniowski
email: [EMAIL PROTECTED] || [EMAIL PROTECTED]
gg: 5354504

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to