librelist archives

« back to archive

[flask] How to use decorators with pluggable views?

[flask] How to use decorators with pluggable views?

From:
Honza Javorek
Date:
2011-08-25 @ 12:21
Hello again,

is there a simple way how to use view decorators with pluggable views?
I can't find anything about this topic. I solved most of decorators'
agenda in my custom way anyway and view class inheritance is very
helpful here, but I'd like to use also some functionality provided by
decorators prepared in extensions and so on. Moreover, decorators are
sexy.

E.g. will this work properly?

@login_required
def dispatch_request(self):
    pass

Thanks for any suggestions,
Honza

Re: [flask] How to use decorators with pluggable views?

From:
Honza Javorek
Date:
2011-08-25 @ 14:07
Until now, I at least managed how to easily use Werkzeug's routing
with pluggable views:


class Rule(werkzeug.routing.Rule):
    def __init__(self, *args, **kwargs):
        if 'view_func' in kwargs:
            view = kwargs['view_func']
            if not 'endpoint' in kwargs:
                kwargs['endpoint'] = view.__name__
            app.view_functions[kwargs['endpoint']] = view
            del(kwargs['view_func'])
        super(Rule, self).__init__(*args, **kwargs)


...allows me to route like this:


app.url_map.add(Subdomain('app', [
    Rule('/login/', view_func=LoginView.as_view('login')),
    Rule('/logout/', view_func=LogoutView.as_view('logout')),
]))


Could be useful for someone.
Honza


On Thu, Aug 25, 2011 at 14:21, Honza Javorek <honza@javorek.net> wrote:
> Hello again,
>
> is there a simple way how to use view decorators with pluggable views?
> I can't find anything about this topic. I solved most of decorators'
> agenda in my custom way anyway and view class inheritance is very
> helpful here, but I'd like to use also some functionality provided by
> decorators prepared in extensions and so on. Moreover, decorators are
> sexy.
>
> E.g. will this work properly?
>
> @login_required
> def dispatch_request(self):
>    pass
>
> Thanks for any suggestions,
> Honza
>