librelist archives

« back to archive

dynamically place decorators on all endpoint

dynamically place decorators on all endpoint

From:
Sjoerd Huisman | Congressus
Date:
2013-02-09 @ 16:18
Hi,

 

Is there a way to decorate all endpoint with a function? I want to decorate
all my endpoints dynamically with a permission from Flask-Principle.

Currently this is my setup;

p_news_index = Permission(ActionNeed('news_index'))

@module.route('/')

@p_news_index.require()

def index():

    from congressus.core.models.news import News

    news = News.query.all()

    return render_template('news_index.html', news=news)

 

Is there a way to dynamically place this on all endpoints?

 

Thanks,

 

Sjoerd Huisman

Re: [flask] dynamically place decorators on all endpoint

From:
Trey Long
Date:
2013-02-09 @ 16:34
You could use @before_request to run some code before every request 
instead of trying to decorate every single endpoint.


On Feb 9, 2013, at 11:18 AM, Sjoerd Huisman | Congressus 
<sjoerd@congressus.nl> wrote:

> Hi,
>  
> Is there a way to decorate all endpoint with a function? I want to 
decorate all my endpoints dynamically with a permission from 
Flask-Principle.
> Currently this is my setup;
> p_news_index = Permission(ActionNeed('news_index'))
> @module.route('/')
> @p_news_index.require()
> def index():
>     from congressus.core.models.news import News
>     news = News.query.all()
>     return render_template('news_index.html', news=news)
>  
> Is there a way to dynamically place this on all endpoints?
>  
> Thanks,
> 
>  
> Sjoerd Huisman

Re: [flask] dynamically place decorators on all endpoint

From:
Markus Unterwaditzer
Date:
2013-02-09 @ 16:31
Sjoerd Huisman | Congressus <sjoerd@congressus.nl> wrote:

>Hi,
>
> 
>
>Is there a way to decorate all endpoint with a function? I want to
>decorate
>all my endpoints dynamically with a permission from Flask-Principle.
>
>Currently this is my setup;
>
>p_news_index = Permission(ActionNeed('news_index'))
>
>@module.route('/')
>
>@p_news_index.require()
>
>def index():
>
>    from congressus.core.models.news import News
>
>    news = News.query.all()
>
>    return render_template('news_index.html', news=news)
>
> 
>
>Is there a way to dynamically place this on all endpoints?
>
> 
>
>Thanks,
>
> 
>
>Sjoerd Huisman

You could write a decorator that replaces app.route and additionally does 
whatever you want.
-- Markus